-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnippet_basic.go
193 lines (159 loc) · 4.01 KB
/
snippet_basic.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
package codegen
import (
"bytes"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
)
func Stringify(snippet Snippet) string {
return string(snippet.Bytes())
}
type Snippet interface {
Bytes() []byte
}
func Block(bodies ...Snippet) Body {
return Body(bodies)
}
type Body []Snippet
func (body Body) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteRune('{')
for _, stmt := range body {
if stmt == nil {
continue
}
buf.WriteRune('\n')
buf.Write(stmt.Bytes())
}
buf.WriteRune('\n')
buf.WriteRune('}')
return buf.Bytes()
}
type SnippetBuiltIn string
func (tpe SnippetBuiltIn) Bytes() []byte {
return []byte(string(tpe))
}
const (
Iota SnippetBuiltIn = "iota"
True SnippetBuiltIn = "true"
False SnippetBuiltIn = "false"
Nil SnippetBuiltIn = "nil"
Break SnippetBuiltIn = "break"
Continue SnippetBuiltIn = "continue"
Fallthrough SnippetBuiltIn = "fallthrough"
)
func Comments(lines ...string) SnippetComments {
finalLines := make([]string, 0)
for _, line := range lines {
if line != "" {
finalLines = append(finalLines, strings.Split(line, "\n")...)
}
}
return SnippetComments(finalLines)
}
type SnippetComments []string
func (comments SnippetComments) Bytes() []byte {
buf := &bytes.Buffer{}
for _, n := range comments {
buf.WriteString("// ")
buf.WriteString(n)
buf.WriteRune('\n')
}
return buf.Bytes()
}
var Val = createVal(LowerSnakeCase)
func createVal(aliaser ImportPathAliaser) func(v interface{}) Snippet {
return func(v interface{}) Snippet {
rv := reflect.ValueOf(v)
tpe := reflect.TypeOf(v)
val := createVal(aliaser)
typeof := createTypeOf(aliaser)
switch rv.Kind() {
case reflect.Ptr:
return Unary(Paren(val(rv.Elem().Interface())))
case reflect.Struct:
values := make([]Snippet, 0)
for i := 0; i < rv.NumField(); i++ {
f := rv.Field(i)
ft := tpe.Field(i)
if !IsEmptyValue(f) {
values = append(values, KeyValue(Id(ft.Name), val(f.Interface())))
}
}
return Compose(typeof(tpe), values...)
case reflect.Map:
values := make([]Snippet, 0)
for _, key := range rv.MapKeys() {
values = append(values, KeyValue(val(key.Interface()), val(rv.MapIndex(key).Interface())))
}
sort.Slice(values, func(i, j int) bool {
return string(values[i].(*SnippetKeyValueExpr).Key.Bytes()) < string(values[j].(*SnippetKeyValueExpr).Key.Bytes())
})
return Compose(typeof(tpe), values...)
case reflect.Slice, reflect.Array:
values := make([]Snippet, 0)
for i := 0; i < rv.Len(); i++ {
values = append(values, val(rv.Index(i).Interface()))
}
return Compose(typeof(tpe), values...)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int64,
reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
return Lit(fmt.Sprintf("%d", v))
case reflect.Int32:
if b, ok := v.(rune); ok {
r := strconv.QuoteRune(b)
if len(r) == 3 {
return Lit(r)
}
}
return Lit(fmt.Sprintf("%d", v))
case reflect.Bool:
return Lit(strconv.FormatBool(v.(bool)))
case reflect.Float32:
return Lit(strconv.FormatFloat(float64(v.(float32)), 'f', -1, 32))
case reflect.Float64:
return Lit(strconv.FormatFloat(v.(float64), 'f', -1, 64))
case reflect.String:
return Lit(strconv.Quote(v.(string)))
case reflect.Invalid:
return Nil
default:
panic(fmt.Errorf("%v is an unsupported type", v))
}
}
}
func Compose(tpe SnippetType, elts ...Snippet) *SnippetCompositeLit {
return &SnippetCompositeLit{
Type: tpe,
Elts: elts,
}
}
type SnippetCompositeLit struct {
Type SnippetType
Elts []Snippet
}
func (lit *SnippetCompositeLit) Bytes() []byte {
buf := &bytes.Buffer{}
if lit.Type != nil {
buf.Write(lit.Type.Bytes())
}
buf.WriteRune('{')
for _, n := range lit.Elts {
buf.WriteRune('\n')
buf.Write(n.Bytes())
buf.WriteRune(',')
}
buf.WriteRune('\n')
buf.WriteRune('}')
return buf.Bytes()
}
func Lit(s string) *SnippetLit {
lit := SnippetLit(s)
return &lit
}
type SnippetLit string
func (lit SnippetLit) Bytes() []byte {
return []byte(lit)
}