Skip to content

Commit 4380d7f

Browse files
committed
Don't duplicate types and don't create types for date fields
1 parent c9b5374 commit 4380d7f

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

field.go

Lines changed: 3 additions & 5 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,9 +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:", sf.Name, sf.Type.Kind())
140-
}
137+
// if sf.Anonymous {
138+
// log.Println("anonymous:", sf.Name, sf.Type.Kind())
139+
// }
141140

142141
var (
143142
jsonTag = strings.Split(sf.Tag.Get("json"), ",")
@@ -150,7 +149,6 @@ func (f *Field) setProps(sf reflect.StructField, sft reflect.Type) (ignore bool)
150149

151150
if f.Name = sf.Name; len(jsonTag) > 0 && jsonTag[0] != "" {
152151
f.Name = jsonTag[0]
153-
154152
}
155153

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

s2ts.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ 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

7474
func (s *StructToTS) addTypeFields(out *Struct, t reflect.Type) {
@@ -84,13 +84,13 @@ func (s *StructToTS) addTypeFields(out *Struct, t reflect.Type) {
8484
k = sft.Kind()
8585
}
8686

87-
if sf.Anonymous && k == reflect.Struct {
88-
log.Println("trying anonymous field:", sft, k)
89-
s.addTypeFields(out, sft)
87+
if tf.setProps(sf, sft) {
9088
continue
9189
}
9290

93-
if tf.setProps(sf, sft) {
91+
if sf.Anonymous && k == reflect.Struct && !tf.IsDate {
92+
// log.Println("trying anonymous field:", sft, k)
93+
s.addTypeFields(out, sft)
9494
continue
9595
}
9696

@@ -100,7 +100,7 @@ func (s *StructToTS) addTypeFields(out *Struct, t reflect.Type) {
100100

101101
switch {
102102
case isStruct(sft.Elem()):
103-
tf.ValType = s.addType(sft.Elem(), "", out.Name).Name
103+
tf.ValType = s.addType(sft.Elem(), "").Name
104104
case sft.Elem().Kind() == reflect.Interface:
105105
tf.ValType = "any"
106106
}
@@ -109,15 +109,15 @@ func (s *StructToTS) addTypeFields(out *Struct, t reflect.Type) {
109109
tf.TsType, tf.ValType = "array", stripType(sft.Elem())
110110

111111
if isStruct(sft.Elem()) {
112-
tf.ValType = s.addType(sft.Elem(), "", out.Name).Name
112+
tf.ValType = s.addType(sft.Elem(), "").Name
113113
}
114114

115115
case k == reflect.Struct:
116-
if isDate(sft) {
116+
if isDate(sft) || tf.IsDate {
117117
break
118118
}
119119
tf.TsType = "object"
120-
tf.ValType = s.addType(sft, "", out.Name).Name
120+
tf.ValType = s.addType(sft, "").Name
121121

122122
case k == reflect.Interface:
123123
tf.TsType, tf.ValType = "object", ""
@@ -131,7 +131,7 @@ func (s *StructToTS) addTypeFields(out *Struct, t reflect.Type) {
131131
}
132132
}
133133

134-
func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct) {
134+
func (s *StructToTS) addType(t reflect.Type, name string) (out *Struct) {
135135
t = indirect(t)
136136

137137
if out = s.seen[t]; out != nil {
@@ -146,16 +146,17 @@ func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct)
146146
}
147147

148148
out = &Struct{
149-
Name: prefix + name,
149+
Name: name,
150150
Fields: make([]*Field, 0, t.NumField()),
151151

152152
t: t,
153153
}
154154

155-
log.Println("building struct:", out.Name)
155+
// log.Println("building struct:", out.Name)
156156
s.addTypeFields(out, t)
157157
s.seen[t] = out
158158
s.structs = append(s.structs, out)
159+
// log.Println("/building struct:", out.Name)
159160
return
160161
}
161162

0 commit comments

Comments
 (0)