Skip to content

Add test for map key behaviour #80

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions typescriptify/typescriptify.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,13 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str
}
strippedFieldName := strings.ReplaceAll(fieldName, "?", "")

keyTypeStr := keyType.Name()
// Key should always be string, no need for this:
// _, isSimple := t.types[keyType.Kind()]
// if !isSimple {
// keyTypeStr = t.prefix + keyType.Name() + t.suffix
// }
var keyTypeStr string
mappedType, isSimple := t.types[keyType.Kind()]
if !isSimple {
keyTypeStr = t.prefix + keyType.Name() + t.suffix
} else {
keyTypeStr = t.prefix + mappedType + t.suffix
}

if valueType.Kind() == reflect.Struct {
t.fields = append(t.fields, fmt.Sprintf("%s%s: {[key: %s]: %s};", t.indent, fieldName, keyTypeStr, t.prefix+valueTypeName))
Expand Down
24 changes: 24 additions & 0 deletions typescriptify/typescriptify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,3 +1076,27 @@ func TestTypescriptifyCustomJsonTag(t *testing.T) {
}`
testConverter(t, converter, false, desiredResult, nil)
}

func TestTypescriptifyCustomTypeInMap(t *testing.T) {
converter := New()
converter.CreateConstructor = false

type Foo uint
converter.ManageType(reflect.TypeOf(Foo(0)), TypeOptions{TSType: "number"})

type Bar struct {
Foo Foo `json:"foo"`
FooInKey map[Foo]bool `json:"fooInKey"`
FooInValue map[string]Foo `json:"fooInValue"`
}

converter.AddType(reflect.TypeOf(Bar{}))

desiredResult := `export class Bar {
foo: number;
fooInKey: {[key: number]: boolean};
fooInValue: {[key: string]: number};
}`

testConverter(t, converter, false, desiredResult, nil)
}
Loading