Skip to content

Commit 38229b0

Browse files
authored
Merge pull request #17 from hasenj/embedded
Embed fields from anonymous/embedded structs
2 parents c4c0ddb + 4380d7f commit 38229b0

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

field.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"go/ast"
66
"io"
7-
"log"
87
"reflect"
98
"strings"
109
)
@@ -135,10 +134,9 @@ func (f *Field) setProps(sf reflect.StructField, sft reflect.Type) (ignore bool)
135134
return true
136135
}
137136

138-
if sf.Anonymous {
139-
log.Println("anonymous structs aren't supported, yet.")
140-
return true
141-
}
137+
// if sf.Anonymous {
138+
// log.Println("anonymous:", sf.Name, sf.Type.Kind())
139+
// }
142140

143141
var (
144142
jsonTag = strings.Split(sf.Tag.Get("json"), ",")
@@ -151,7 +149,6 @@ func (f *Field) setProps(sf reflect.StructField, sft reflect.Type) (ignore bool)
151149

152150
if f.Name = sf.Name; len(jsonTag) > 0 && jsonTag[0] != "" {
153151
f.Name = jsonTag[0]
154-
155152
}
156153

157154
f.IsDate = isDate(sft) || len(tsTag) > 0 && tsTag[0] == "date" || sft.Kind() == reflect.Int64 && strings.HasSuffix(f.Name, "TS")

s2ts.go

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,15 @@ func (s *StructToTS) AddWithName(v interface{}, name string) *Struct {
6868
t = reflect.TypeOf(v)
6969
}
7070

71-
return s.addType(t, name, "")
71+
return s.addType(t, name)
7272
}
7373

74-
func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct) {
75-
t = indirect(t)
76-
77-
if out = s.seen[t]; out != nil {
78-
return out
79-
}
80-
81-
if name == "" {
82-
name = t.Name()
83-
if !s.opts.NoCapitalize {
84-
name = capitalize(name)
85-
}
86-
}
87-
88-
out = &Struct{
89-
Name: prefix + name,
90-
Fields: make([]*Field, 0, t.NumField()),
91-
92-
t: t,
93-
}
94-
95-
s.seen[t] = out
96-
74+
func (s *StructToTS) addTypeFields(out *Struct, t reflect.Type) {
9775
for i := 0; i < t.NumField(); i++ {
98-
var (
99-
sf = t.Field(i)
100-
sft = sf.Type
101-
tf Field
102-
k = sft.Kind()
103-
)
76+
sf := t.Field(i)
77+
sft := sf.Type
78+
k := sft.Kind()
79+
var tf Field
10480

10581
if k == reflect.Ptr {
10682
tf.CanBeNull = true
@@ -112,13 +88,19 @@ func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct)
11288
continue
11389
}
11490

91+
if sf.Anonymous && k == reflect.Struct && !tf.IsDate {
92+
// log.Println("trying anonymous field:", sft, k)
93+
s.addTypeFields(out, sft)
94+
continue
95+
}
96+
11597
switch {
11698
case k == reflect.Map:
11799
tf.TsType, tf.KeyType, tf.ValType = "map", stripType(sft.Key()), stripType(sft.Elem())
118100

119101
switch {
120102
case isStruct(sft.Elem()):
121-
tf.ValType = s.addType(sft.Elem(), "", out.Name).Name
103+
tf.ValType = s.addType(sft.Elem(), "").Name
122104
case sft.Elem().Kind() == reflect.Interface:
123105
tf.ValType = "any"
124106
}
@@ -127,15 +109,15 @@ func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct)
127109
tf.TsType, tf.ValType = "array", stripType(sft.Elem())
128110

129111
if isStruct(sft.Elem()) {
130-
tf.ValType = s.addType(sft.Elem(), "", out.Name).Name
112+
tf.ValType = s.addType(sft.Elem(), "").Name
131113
}
132114

133115
case k == reflect.Struct:
134-
if isDate(sft) {
116+
if isDate(sft) || tf.IsDate {
135117
break
136118
}
137119
tf.TsType = "object"
138-
tf.ValType = s.addType(sft, "", out.Name).Name
120+
tf.ValType = s.addType(sft, "").Name
139121

140122
case k == reflect.Interface:
141123
tf.TsType, tf.ValType = "object", ""
@@ -147,8 +129,34 @@ func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct)
147129

148130
out.Fields = append(out.Fields, &tf)
149131
}
132+
}
133+
134+
func (s *StructToTS) addType(t reflect.Type, name string) (out *Struct) {
135+
t = indirect(t)
136+
137+
if out = s.seen[t]; out != nil {
138+
return out
139+
}
140+
141+
if name == "" {
142+
name = t.Name()
143+
if !s.opts.NoCapitalize {
144+
name = capitalize(name)
145+
}
146+
}
150147

148+
out = &Struct{
149+
Name: name,
150+
Fields: make([]*Field, 0, t.NumField()),
151+
152+
t: t,
153+
}
154+
155+
// log.Println("building struct:", out.Name)
156+
s.addTypeFields(out, t)
157+
s.seen[t] = out
151158
s.structs = append(s.structs, out)
159+
// log.Println("/building struct:", out.Name)
152160
return
153161
}
154162

0 commit comments

Comments
 (0)