-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstruct.go
155 lines (131 loc) · 2.97 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Package easyparser provides a Go module parser for AST
package easyparser
import (
"go/types"
"reflect"
"sort"
"strings"
tstypes "github.com/go-generalize/go-easyparser/types"
)
const (
jsonTagOmitempty = "omitempty"
)
func (p *pkgParser) parseStruct(strct *types.Struct) tstypes.Type {
type entryPair struct {
key string
value tstypes.ObjectEntry
}
entries := make([][]entryPair, strct.NumFields())
// embedding
for i := 0; i < strct.NumFields(); i++ {
v := strct.Field(i)
tag := strct.Tag(i)
if !v.Exported() || !v.Embedded() {
continue
}
jsonTag := strings.SplitN(reflect.StructTag(tag).Get("json"), ",", 2)
field := ""
if len(jsonTag) >= 1 {
field = jsonTag[0]
}
optional := len(jsonTag) >= 2 && jsonTag[1] == jsonTagOmitempty
if field == "-" && !p.IgnoreOmittedJSONField {
continue
}
tst := p.parseType(v.Type(), true)
if len(field) == 0 {
if o, ok := tst.(*tstypes.Object); ok {
fieldEntries := make([]entryPair, 0, len(o.Entries))
for k, v := range o.Entries {
fieldEntries = append(fieldEntries, entryPair{k, v})
}
sort.Slice(fieldEntries, func(i, j int) bool {
return fieldEntries[i].value.FieldIndex < fieldEntries[j].value.FieldIndex
})
entries[i] = fieldEntries
continue
}
field = v.Name()
}
entries[i] = []entryPair{
{
key: field,
value: tstypes.ObjectEntry{
RawName: v.Name(),
RawTag: tag,
Type: tst,
Optional: optional,
FieldIndex: i,
},
},
}
}
// not embedding
for i := 0; i < strct.NumFields(); i++ {
v := strct.Field(i)
tag := strct.Tag(i)
if !v.Exported() || v.Embedded() {
continue
}
obj, _, _ := types.LookupFieldOrMethod(strct, true, p.pkg, v.Name())
pos := p.fset.Position(obj.Pos())
jsonTag := strings.SplitN(reflect.StructTag(tag).Get("json"), ",", 2)
field := ""
if len(jsonTag) >= 1 {
field = jsonTag[0]
}
optional := len(jsonTag) >= 2 && jsonTag[1] == jsonTagOmitempty
if field == "-" && !p.IgnoreOmittedJSONField {
continue
}
if len(field) == 0 {
field = v.Name()
}
tst := p.parseType(v.Type(), true)
if optional {
entries[i] = []entryPair{
{
key: field,
value: tstypes.ObjectEntry{
RawName: v.Name(),
RawTag: tag,
Type: p.removeNullable(tst),
Optional: true,
Position: &pos,
},
},
}
} else {
entries[i] = []entryPair{
{
key: field,
value: tstypes.ObjectEntry{
RawName: v.Name(),
RawTag: tag,
Type: tst,
Optional: false,
Position: &pos,
},
},
}
}
}
obj := tstypes.Object{
Entries: map[string]tstypes.ObjectEntry{},
}
idx := 0
for _, entry := range entries {
for _, e := range entry {
e.value.FieldIndex = idx
obj.Entries[e.key] = e.value
idx++
}
}
return &obj
}
func (p *Parser) removeNullable(typ tstypes.Type) tstypes.Type {
if nullable, ok := typ.(*tstypes.Nullable); ok {
return nullable.Inner
}
return typ
}