Skip to content

Commit

Permalink
Support map[string]interface{}.
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
OneOfOne committed Feb 13, 2019
1 parent 6a8719e commit 28366b7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67
golang.org/x/tools v0.0.0-20190213135902-6bedcd10978a
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67 h1:bPP/rGuN1LUM0eaEwo6vnP6OfIWJzJBulzGUiKLjjSY=
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
golang.org/x/tools v0.0.0-20190213135902-6bedcd10978a h1:ncPOGSo3avrTTUKHvDmwoS5E5of95qqNwftSXoxX+Wk=
golang.org/x/tools v0.0.0-20190213135902-6bedcd10978a/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
5 changes: 4 additions & 1 deletion s2ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct)
case k == reflect.Map:
tf.TsType, tf.KeyType, tf.ValType = "map", stripType(sft.Key()), stripType(sft.Elem())

if isStruct(sft.Elem()) {
switch {
case isStruct(sft.Elem()):
tf.ValType = s.addType(sft.Elem(), "", out.Name).Name
case sft.Elem().Kind() == reflect.Interface:
tf.ValType = "any"
}

case k == reflect.Slice, k == reflect.Array:
Expand Down
24 changes: 17 additions & 7 deletions s2ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ type ComplexStruct struct {
T time.Time `json:"t,omitempty"` // automatically handled
NullOther *OtherStruct `json:"o,omitempty"`
NoNullOther *OtherStruct `json:"nno,omitempty" ts:",no-null"`
Data Data `json:"d"`
DataPtr *Data `json:"dp"`
}

type Data map[string]interface{}

func ExampleComplexStruct() {
s2ts := struct2ts.New(nil)
s2ts.Add(ComplexStruct{})
Expand Down Expand Up @@ -74,7 +78,7 @@ func ExampleComplexStruct() {
// for (const k of Object.keys(o)) {
// const v: any = o[k];
// if (!v) continue;
// d[k] = ToObject(v, typeOrCfg[k] || '', true);
// d[k] = ToObject(v, typeOrCfg[k] || {}, true);
// }
//
// return d;
Expand Down Expand Up @@ -106,6 +110,8 @@ func ExampleComplexStruct() {
// t: Date;
// o: ComplexStructOtherStruct | null;
// nno: ComplexStructOtherStruct;
// d: { [key: string]: any };
// dp: { [key: string]: any } | null;
//
// constructor(data?: any) {
// const d: any = (data && typeof data === 'object') ? ToObject(data) : {};
Expand All @@ -116,6 +122,8 @@ func ExampleComplexStruct() {
// this.t = ('t' in d) ? ParseDate(d.t) : new Date();
// this.o = ('o' in d) ? new ComplexStructOtherStruct(d.o) : null;
// this.nno = new ComplexStructOtherStruct(d.nno);
// this.d = ('d' in d) ? d.d as { [key: string]: any } : {};
// this.dp = ('dp' in d) ? d.dp as { [key: string]: any } : null;
// }
//
// toObject(): any {
Expand All @@ -128,10 +136,12 @@ func ExampleComplexStruct() {
// }
//
// // exports
// export ComplexStructOtherStruct;
// export ComplexStruct;
// export ParseDate;
// export ParseNumber;
// export FromArray;
// export ToObject;
// export {
// ComplexStructOtherStruct,
// ComplexStruct,
// ParseDate,
// ParseNumber,
// FromArray,
// ToObject,
// };
}

0 comments on commit 28366b7

Please sign in to comment.