-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
194 lines (163 loc) · 4.75 KB
/
types.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package toast
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/fatih/structtag"
)
type Type interface {
Node
GetName() string
GetTypeNames() []string
SetTypeNames([]string)
GetDocs() string
}
type Node interface {
// Renders JSON representation of the node.
Reflect() json.RawMessage
// Renders Go type definitions.
Go() string
// Renders CUE definitions.
CUE() string
}
type File struct {
pkgName string
cuePkgName string
Imports map[string]Import
Code []Type
trans []Transform
copies []*CopyIntoStruct
eximports []*ExcludeImport
modimports []*ModifyImport
genEnumTrans []*GenEnumTypeTransform
mkEnums []*PromoteToEnumType
debug bool
}
func (f *File) Reflect() json.RawMessage {
imports := make([]string, 0, len(f.Imports))
for _, imp := range f.Imports {
imports = append(imports, string(imp.Reflect()))
}
code := make([]string, len(f.Code))
for i, t := range f.Code {
code[i] = string(t.Reflect())
}
raw := fmt.Sprintf(
`{"package":"%s","imports":[%s],"code":[%s]}`,
f.pkgName, strings.Join(imports, ","), strings.Join(code, ","),
)
if f.debug {
fmt.Println(raw)
return json.RawMessage{}
}
return json.RawMessage(raw)
}
type Import struct {
Name string `json:"name,omitempty"`
Path string `json:"path"`
oldPath string
used bool
}
func (i *Import) Reflect() json.RawMessage {
raw, _ := json.Marshal(i)
return injectKind(string(raw), "import")
}
func (i *Import) GetOldPath() string {
return i.oldPath
}
type PlainType struct {
Name string `json:"name"`
Type string `json:"type"`
Docs string `json:"-"`
}
func (p *PlainType) Reflect() json.RawMessage {
raw, _ := json.Marshal(p)
return injectKind(string(raw), "plain")
}
type ArrayType struct {
Name string `json:"name"`
Type string `json:"type"`
Length int `json:"length,omitempty"`
Docs string `json:"-"`
}
func (a *ArrayType) Reflect() json.RawMessage {
raw, _ := json.Marshal(a)
return injectKind(string(raw), "array")
}
type MapType struct {
Name string `json:"name"`
KeyType string `json:"key_type"`
ValueType string `json:"value_type"`
Docs string `json:"-"`
}
func (m *MapType) Reflect() json.RawMessage {
raw, _ := json.Marshal(m)
return injectKind(string(raw), "map")
}
type StructType struct {
Name string `json:"name"`
Fields []*Field `json:"fields"`
Docs string `json:"-"`
}
func (s *StructType) Reflect() json.RawMessage {
fields := make([]string, len(s.Fields))
for i, f := range s.Fields {
fields[i] = string(f.Reflect())
}
return json.RawMessage(
fmt.Sprintf(
`{"kind":"struct","name":"%s","fields":[%s]}`,
s.Name, strings.Join(fields, ","),
),
)
}
type EnumType struct {
Name string `json:"name"`
Values []string `json:"values"`
Docs string `json:"-"`
}
func (et *EnumType) Reflect() json.RawMessage {
raw, _ := json.Marshal(et)
return injectKind(string(raw), "enum")
}
func (p *PlainType) GetName() string { return p.Name }
func (a *ArrayType) GetName() string { return a.Name }
func (m *MapType) GetName() string { return m.Name }
func (s *StructType) GetName() string { return s.Name }
func (et *EnumType) GetName() string { return et.Name }
func (p *PlainType) GetTypeNames() []string { return []string{p.Type} }
func (a *ArrayType) GetTypeNames() []string { return []string{a.Type} }
func (m *MapType) GetTypeNames() []string { return []string{m.KeyType, m.ValueType} }
func (s *StructType) GetTypeNames() []string { panic("not implemented") }
func (et *EnumType) GetTypeNames() []string { return []string{et.Name} }
func (p *PlainType) SetTypeNames(tt []string) { p.Type = tt[0] }
func (a *ArrayType) SetTypeNames(tt []string) { a.Type = tt[0] }
func (m *MapType) SetTypeNames(tt []string) { m.KeyType = tt[0]; m.ValueType = tt[1] }
func (s *StructType) SetTypeNames(tt []string) { panic("not implemented") }
func (et *EnumType) SetTypeNames(typs []string) { et.Name = typs[0] }
func (p *PlainType) GetDocs() string { return p.Docs }
func (a *ArrayType) GetDocs() string { return a.Docs }
func (m *MapType) GetDocs() string { return m.Docs }
func (s *StructType) GetDocs() string { return s.Docs }
func (et *EnumType) GetDocs() string { return et.Docs }
type Field struct {
Type
Tags *structtag.Tags
}
func (f *Field) Reflect() json.RawMessage {
raw := f.Type.Reflect()
var tags []string
for _, t := range f.Tags.Tags() {
tags = append(tags, fmt.Sprintf(`"%s":"%s"`, t.Key, t.Name))
}
return json.RawMessage(fmt.Sprintf(`%s,"tags":{%s}}`, raw[:len(raw)-1], strings.Join(tags, ",")))
}
func injectKind(raw string, kind string) json.RawMessage {
return json.RawMessage(fmt.Sprintf(`{"kind":"%s",%s`, kind, raw[1:]))
}
func printJSON(raw json.RawMessage) {
b := new(bytes.Buffer)
json.Indent(b, raw, "", " ")
fmt.Println(b.String())
}