-
Notifications
You must be signed in to change notification settings - Fork 19
/
go-util.go
236 lines (215 loc) · 5.21 KB
/
go-util.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2015 Yahoo Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/ardielle/ardielle-go/rdl"
"os"
"os/exec"
"path/filepath"
"strings"
"unicode"
)
//default imports for go code generation. Gets rewritten when vendoring.
const HttpTreeMuxGoImport = "github.com/dimfeld/httptreemux"
const RdlGoImport = "github.com/ardielle/ardielle-go/rdl"
func SnakeToCamel(name string) string {
// "THIS_IS_IT" -> "ThisIsIt"
result := make([]rune, 0)
newWord := true
for _, c := range name {
if c == '_' {
newWord = true
} else if newWord {
result = append(result, unicode.ToUpper(c))
newWord = false
} else {
result = append(result, unicode.ToLower(c))
}
}
s := string(result)
return strings.Replace(strings.Replace(s, "Uuid", "UUID", -1), "Uri", "URI", -1)
}
func optionalAnyToString(any interface{}) string {
if any == nil {
return "null"
}
switch v := any.(type) {
case *bool:
return fmt.Sprintf("%v", *v)
case *int8:
return fmt.Sprintf("%d", *v)
case *int16:
return fmt.Sprintf("%d", *v)
case *int32:
return fmt.Sprintf("%d", *v)
case *int64:
return fmt.Sprintf("%d", *v)
case *float32:
return fmt.Sprintf("%g", *v)
case *float64:
return fmt.Sprintf("%g", *v)
case *string:
return *v
case bool:
return fmt.Sprintf("%v", v)
case int8:
return fmt.Sprintf("%d", v)
case int16:
return fmt.Sprintf("%d", v)
case int32:
return fmt.Sprintf("%d", v)
case int64:
return fmt.Sprintf("%d", v)
case float32:
return fmt.Sprintf("%g", v)
case float64:
return fmt.Sprintf("%g", v)
case string:
return fmt.Sprintf("%v", v)
default:
panic("optionalAnyToString")
}
}
func formatBlock(s string, leftCol int, rightCol int, prefix string) string {
if s == "" {
return ""
}
tab := spaces(leftCol)
var buf bytes.Buffer
max := 80
col := leftCol
lines := 1
tokens := strings.Split(s, " ")
for _, tok := range tokens {
toklen := len(tok)
if col+toklen >= max {
buf.WriteString("\n")
lines++
buf.WriteString(tab)
buf.WriteString(prefix)
buf.WriteString(tok)
col = leftCol + 3 + toklen
} else {
if col == leftCol {
col += len(prefix)
buf.WriteString(prefix)
} else {
buf.WriteString(" ")
}
buf.WriteString(tok)
col += toklen + 1
}
}
buf.WriteString("\n")
emptyPrefix := strings.Trim(prefix, " ")
pad := tab + emptyPrefix + "\n"
return pad + buf.String() + pad
}
func formatComment(s string, leftCol int, rightCol int) string {
return formatBlock(s, leftCol, rightCol, "// ")
}
func spaces(count int) string {
return stringOfChar(count, ' ')
}
func stringOfChar(count int, b byte) string {
buf := make([]byte, 0, count)
for i := 0; i < count; i++ {
buf = append(buf, b)
}
return string(buf)
}
func addFields(reg rdl.TypeRegistry, dst []*rdl.StructFieldDef, t *rdl.Type) []*rdl.StructFieldDef {
switch t.Variant {
case rdl.TypeVariantStructTypeDef:
st := t.StructTypeDef
if st.Type != "Struct" {
dst = addFields(reg, dst, reg.FindType(st.Type))
}
for _, f := range st.Fields {
dst = append(dst, f)
}
}
return dst
}
func flattenedFields(reg rdl.TypeRegistry, t *rdl.Type) []*rdl.StructFieldDef {
return addFields(reg, make([]*rdl.StructFieldDef, 0), t)
}
func capitalize(text string) string {
return strings.ToUpper(text[0:1]) + text[1:]
}
func uncapitalize(text string) string {
return strings.ToLower(text[0:1]) + text[1:]
}
func leftJustified(text string, width int) string {
return text + spaces(width-len(text))
}
func fileExists(filepath string) bool {
if _, err := os.Stat(filepath); os.IsNotExist(err) {
return false
}
return true
}
func outputWriter(outdir string, name string, ext string) (*bufio.Writer, *os.File, string, error) {
sname := "anonymous"
if strings.HasSuffix(outdir, ext) {
name = filepath.Base(outdir)
sname = name[:len(name)-len(ext)]
outdir = filepath.Dir(outdir)
}
if name != "" {
sname = name
}
if outdir == "" {
return bufio.NewWriter(os.Stdout), nil, sname, nil
}
outfile := sname
if !strings.HasSuffix(outfile, ext) {
outfile += ext
}
path := filepath.Join(outdir, outfile)
f, err := os.Create(path)
if err != nil {
return nil, nil, "", err
}
writer := bufio.NewWriter(f)
return writer, f, sname, nil
}
func generationHeader(banner string) string {
// Matches the auto-generated code header structure defined at
// https://github.com/golang/go/issues/13560#issuecomment-288457920
return fmt.Sprintf("//\n// Code generated by %s DO NOT EDIT.\n//", banner)
}
func generationPackage(schema *rdl.Schema, ns string) string {
pkg := "main"
if ns != "" {
pkg = ns
} else if schema.Name != "" {
pkg = strings.ToLower(string(schema.Name))
}
return pkg
}
/*
// generate common runtime code for client and server
func generateUtil(schema *rdl.Schema, writer io.Writer) error {
basenameFunc := func(s string) string {
i := strings.LastIndex(s, ".")
if i >= 0 {
s = s[i+1:]
}
return s
}
funcMap := template.FuncMap{
"basename": basenameFunc,
}
t := template.Must(template.New("util").Funcs(funcMap).Parse(utilTemplate))
return t.Execute(writer, schema)
}
const utilTemplate = `
`
*/
func goFmt(filename string) error {
return exec.Command("go", "fmt", filename).Run()
}