-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes_cue.go
155 lines (142 loc) · 3.26 KB
/
types_cue.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 toast
import (
"fmt"
"sort"
"strings"
)
func (f *File) CUE() string {
var imports, code string
impSlice := make([]string, 0, len(f.Imports))
for _, i := range f.Imports {
impSlice = append(impSlice, i.CUE())
}
sort.Strings(impSlice)
for _, imp := range impSlice {
imports += imp
}
if len(imports) > 0 {
imports = fmt.Sprintf("import (\n%s)\n\n", imports)
}
for _, t := range f.Code {
code += t.GetDocs() + t.CUE() + "\n"
}
src := []byte(fmt.Sprintf("package %s\n\n%s%s", f.cuePkgName, imports, code))
return string(src)[:len(src)-1]
}
func (i *Import) CUE() string {
if i.Name == "" {
return fmt.Sprintf(" \"%s\"\n", i.Path)
}
return fmt.Sprintf(" %s \"%s\"\n", i.Name, i.Path)
}
func (p *PlainType) CUE() string {
return fmt.Sprintf("#%s: %s\n", p.Name, fmtToCUE(p.Type))
}
func (a *ArrayType) CUE() string {
if a.Type == "byte" {
return fmt.Sprintf("#%s: bytes\n", a.Name)
}
return fmt.Sprintf("#%s: [...%s]\n", a.Name, fmtToCUE(a.Type))
}
func (m *MapType) CUE() string {
keyTyp := fmtToCUE(m.KeyType)
valTyp := fmtToCUE(m.ValueType)
return fmt.Sprintf("#%s: [%s]: %s\n", m.Name, keyTyp, valTyp)
}
func (s *StructType) CUE() string {
var fields string
for _, f := range s.Fields {
fields += f.CUE()
}
return fmt.Sprintf("#%s: {\n%s}\n", s.Name, fields)
}
func (et *EnumType) CUE() string {
values := make([]string, 0, len(et.Values))
for _, v := range et.Values {
values = append(values, `"`+v+`"`)
}
str := "#" + et.Name + ": " + strings.Join(values, " | ") + "\n\n"
for _, v := range et.Values {
str += fmt.Sprintf("%s_%s: \"%s\"\n", et.Name, v, v)
}
return str
}
func (f *Field) CUE() string {
ts := f.Tags.Tags()
if len(ts) == 0 {
return ""
}
jsonTag, _ := f.Tags.Get("json")
if jsonTag == nil {
return ""
}
name := jsonTag.Name
if jsonTag.HasOption("omitempty") {
name += "?"
}
var str string
switch ft := f.Type.(type) {
case *PlainType:
str = fmtToCUE(ft.Type)
case *ArrayType:
if ft.Type == "byte" {
str = "bytes"
} else {
str = "[..." + fmtToCUE(ft.Type) + "]"
}
case *MapType:
keyTyp := fmtToCUE(ft.KeyType)
valTyp := fmtToCUE(ft.ValueType)
str = fmt.Sprintf("[%s]: %s", keyTyp, valTyp)
case *StructType:
var fields string
for _, f := range ft.Fields {
fields += f.CUE()
}
str = fmt.Sprintf("{\n%s}", fields)
}
if docs := f.Type.GetDocs(); docs != "" {
return fmt.Sprintf("%s%s: %s\n", docs, name, str)
}
return fmt.Sprintf("%s: %s\n", name, str)
}
var basicTypes = map[string]bool{
"bool": true,
"string": true,
"int": true,
"int8": true,
"int16": true,
"int32": true,
"int64": true,
"uint": true,
"uint8": true,
"uint16": true,
"uint32": true,
"uint64": true,
"uintptr": true,
"byte": true,
"rune": true,
"float32": true,
"float64": true,
"complex64": true,
}
func fmtToCUE(typ string) string {
typ = strings.Replace(typ, "*", "", 1)
if typ == "interface{}" || typ == "error" {
return "_"
}
if typ == "struct{}" {
return "{}"
}
if strings.HasPrefix(typ, "[]") {
typ = typ[2:]
return fmt.Sprintf("[...%s]", fmtToCUE(typ))
}
if strings.Contains(typ, ".") {
return strings.Replace(typ, ".", ".#", 1)
}
if !basicTypes[typ] && !strings.HasPrefix(typ, "#") {
return "#" + typ
}
return typ
}