Skip to content

Commit 28366b7

Browse files
committed
Support map[string]interface{}.
Fixes #13
1 parent 6a8719e commit 28366b7

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ require (
66
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
77
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
88
github.com/stretchr/testify v1.3.0 // indirect
9-
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67
9+
golang.org/x/tools v0.0.0-20190213135902-6bedcd10978a
1010
gopkg.in/alecthomas/kingpin.v2 v2.2.6
1111
)

go.sum

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
21
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
3-
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
42
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
5-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
63
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
84
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
95
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10-
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
116
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
12-
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67 h1:bPP/rGuN1LUM0eaEwo6vnP6OfIWJzJBulzGUiKLjjSY=
13-
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
14-
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
7+
golang.org/x/tools v0.0.0-20190213135902-6bedcd10978a h1:ncPOGSo3avrTTUKHvDmwoS5E5of95qqNwftSXoxX+Wk=
8+
golang.org/x/tools v0.0.0-20190213135902-6bedcd10978a/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
159
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=

s2ts.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct)
116116
case k == reflect.Map:
117117
tf.TsType, tf.KeyType, tf.ValType = "map", stripType(sft.Key()), stripType(sft.Elem())
118118

119-
if isStruct(sft.Elem()) {
119+
switch {
120+
case isStruct(sft.Elem()):
120121
tf.ValType = s.addType(sft.Elem(), "", out.Name).Name
122+
case sft.Elem().Kind() == reflect.Interface:
123+
tf.ValType = "any"
121124
}
122125

123126
case k == reflect.Slice, k == reflect.Array:

s2ts_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ type ComplexStruct struct {
1919
T time.Time `json:"t,omitempty"` // automatically handled
2020
NullOther *OtherStruct `json:"o,omitempty"`
2121
NoNullOther *OtherStruct `json:"nno,omitempty" ts:",no-null"`
22+
Data Data `json:"d"`
23+
DataPtr *Data `json:"dp"`
2224
}
2325

26+
type Data map[string]interface{}
27+
2428
func ExampleComplexStruct() {
2529
s2ts := struct2ts.New(nil)
2630
s2ts.Add(ComplexStruct{})
@@ -74,7 +78,7 @@ func ExampleComplexStruct() {
7478
// for (const k of Object.keys(o)) {
7579
// const v: any = o[k];
7680
// if (!v) continue;
77-
// d[k] = ToObject(v, typeOrCfg[k] || '', true);
81+
// d[k] = ToObject(v, typeOrCfg[k] || {}, true);
7882
// }
7983
//
8084
// return d;
@@ -106,6 +110,8 @@ func ExampleComplexStruct() {
106110
// t: Date;
107111
// o: ComplexStructOtherStruct | null;
108112
// nno: ComplexStructOtherStruct;
113+
// d: { [key: string]: any };
114+
// dp: { [key: string]: any } | null;
109115
//
110116
// constructor(data?: any) {
111117
// const d: any = (data && typeof data === 'object') ? ToObject(data) : {};
@@ -116,6 +122,8 @@ func ExampleComplexStruct() {
116122
// this.t = ('t' in d) ? ParseDate(d.t) : new Date();
117123
// this.o = ('o' in d) ? new ComplexStructOtherStruct(d.o) : null;
118124
// this.nno = new ComplexStructOtherStruct(d.nno);
125+
// this.d = ('d' in d) ? d.d as { [key: string]: any } : {};
126+
// this.dp = ('dp' in d) ? d.dp as { [key: string]: any } : null;
119127
// }
120128
//
121129
// toObject(): any {
@@ -128,10 +136,12 @@ func ExampleComplexStruct() {
128136
// }
129137
//
130138
// // exports
131-
// export ComplexStructOtherStruct;
132-
// export ComplexStruct;
133-
// export ParseDate;
134-
// export ParseNumber;
135-
// export FromArray;
136-
// export ToObject;
139+
// export {
140+
// ComplexStructOtherStruct,
141+
// ComplexStruct,
142+
// ParseDate,
143+
// ParseNumber,
144+
// FromArray,
145+
// ToObject,
146+
// };
137147
}

0 commit comments

Comments
 (0)