Skip to content

feat: golang map types unioned with possible 'null' #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions config/mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,39 @@ func (v *nullUnionVisitor) Visit(node bindings.Node) walk.Visitor {

return v
}

// NotNullMaps assumes all maps will not be null.
// Example:
// GolangType: map[string]string
// TsType: Record<string,string> | null --> Record<string,string>
func NotNullMaps(ts *guts.Typescript) {
ts.ForEach(func(key string, node bindings.Node) {
walk.Walk(&notNullMaps{}, node)
})
}

type notNullMaps struct{}

func (v *notNullMaps) Visit(node bindings.Node) walk.Visitor {
if union, ok := node.(*bindings.UnionType); ok && len(union.Types) == 2 {
hasNull := slices.ContainsFunc(union.Types, func(t bindings.ExpressionType) bool {
_, isNull := t.(*bindings.Null)
return isNull
})

var record bindings.ExpressionType
index := slices.IndexFunc(union.Types, func(t bindings.ExpressionType) bool {
ref, isRef := t.(*bindings.ReferenceType)
if !isRef {
return false
}
return ref.Name.Name == "Record"
})
if hasNull && index != -1 {
record = union.Types[index]
union.Types = []bindings.ExpressionType{record}
}
}

return v
}
3 changes: 2 additions & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,8 @@ func (ts *Typescript) typescriptType(ty types.Type) (parsedType, error) {
return parsedType{}, xerrors.Errorf("simplify generics in map: %w", err)
}
parsed := parsedType{
Value: RecordReference(keyType.Value, valueType.Value),
// Golang `map` can be marshaled to `null` in json.
Value: bindings.Union(RecordReference(keyType.Value, valueType.Value), &bindings.Null{}),
TypeParameters: tp,
RaisedComments: append(keyType.RaisedComments, valueType.RaisedComments...),
}
Expand Down
24 changes: 24 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,27 @@ func TestGeneration(t *testing.T) {
})
}
}

func TestNotNullMaps(t *testing.T) {
gen, err := guts.NewGolangParser()
require.NoError(t, err, "new convert")

dir := filepath.Join(".", "testdata", "maps")
err = gen.IncludeGenerate("./" + dir)
require.NoErrorf(t, err, "include %q", dir)

gen.IncludeCustomDeclaration(config.StandardMappings())

ts, err := gen.ToTypescript()
require.NoError(t, err, "to typescript")

ts.ApplyMutations(
config.NotNullMaps,
)

output, err := ts.Serialize()
require.NoErrorf(t, err, "generate %q", dir)

// Not perfect, this asserts if the record is a nullable type.
require.Contains(t, output, "SimpleMap: Record<string, string>;", "no nullable Record")
}
2 changes: 1 addition & 1 deletion testdata/anyreference/anyreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// From anyreference/anyreference.go
export interface Example {
readonly Value: Record<string, string>;
readonly Value: Record<string, string> | null;
}

// From anyreference/anyreference.go
Expand Down
14 changes: 6 additions & 8 deletions testdata/genericmap/genericmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ type Custom interface {
Foo | Buzz
}

// Not yet supported
//type FooBuzzMap[R Custom] struct {
// Something map[string]R `json:"something"`
//}
type FooBuzzMap[R Custom] struct {
Something map[string]R `json:"something"`
}

// Not yet supported
//type FooBuzzAnonymousUnion[R Foo | Buzz] struct {
// Something []R `json:"something"`
//}
type FooBuzzAnonymousUnion[R Foo | Buzz] struct {
Something []R `json:"something"`
}
10 changes: 10 additions & 0 deletions testdata/genericmap/genericmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ export interface Foo {
export interface FooBuzz<R extends Custom> {
readonly something: readonly R[];
}

// From codersdk/genericmap.go
export interface FooBuzzAnonymousUnion<R extends Foo | Buzz> {
readonly something: readonly R[];
}

// From codersdk/genericmap.go
export interface FooBuzzMap<R extends Custom> {
readonly something: Record<string, R> | null;
}
6 changes: 3 additions & 3 deletions testdata/maps/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// From maps/map.go
export interface Bar<T extends any> {
readonly SimpleMap: Record<string, string>;
readonly NumberMap: Record<string, number>;
readonly GenericMap: Record<string, T>;
readonly SimpleMap: Record<string, string> | null;
readonly NumberMap: Record<string, number> | null;
readonly GenericMap: Record<string, T> | null;
}