diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-14 14:48:41 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-14 14:48:41 +0200 |
commit | 5c568a362b73cf163b06bc7371982f9a7ceaaf29 (patch) | |
tree | 755b61f969913b5290f3d56f4cfba4070b8155bc /vendor/github.com/vektah/gqlgen/codegen/templates/templates.go | |
parent | ef0d8fa108fbdef24997a84a3c6ecbf21cbc0a9e (diff) | |
download | git-bug-5c568a362b73cf163b06bc7371982f9a7ceaaf29.tar.gz |
gqlgen: add a small program to go:generate the code
Diffstat (limited to 'vendor/github.com/vektah/gqlgen/codegen/templates/templates.go')
-rw-r--r-- | vendor/github.com/vektah/gqlgen/codegen/templates/templates.go | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/vendor/github.com/vektah/gqlgen/codegen/templates/templates.go b/vendor/github.com/vektah/gqlgen/codegen/templates/templates.go new file mode 100644 index 00000000..3d29b403 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/templates/templates.go @@ -0,0 +1,139 @@ +//go:generate go run ./inliner/inliner.go + +package templates + +import ( + "bytes" + "fmt" + "sort" + "strconv" + "strings" + "text/template" + "unicode" +) + +func Run(name string, tpldata interface{}) (*bytes.Buffer, error) { + t := template.New("").Funcs(template.FuncMap{ + "ucFirst": ucFirst, + "lcFirst": lcFirst, + "quote": strconv.Quote, + "rawQuote": rawQuote, + "toCamel": ToCamel, + "dump": dump, + "prefixLines": prefixLines, + }) + + for filename, data := range data { + _, err := t.New(filename).Parse(data) + if err != nil { + panic(err) + } + } + + buf := &bytes.Buffer{} + err := t.Lookup(name).Execute(buf, tpldata) + if err != nil { + return nil, err + } + + return buf, nil +} + +func ucFirst(s string) string { + if s == "" { + return "" + } + r := []rune(s) + r[0] = unicode.ToUpper(r[0]) + return string(r) +} + +func lcFirst(s string) string { + if s == "" { + return "" + } + + r := []rune(s) + r[0] = unicode.ToLower(r[0]) + return string(r) +} + +func isDelimiter(c rune) bool { + return c == '-' || c == '_' || unicode.IsSpace(c) +} + +func ToCamel(s string) string { + buffer := make([]rune, 0, len(s)) + upper := true + lastWasUpper := false + + for _, c := range s { + if isDelimiter(c) { + upper = true + continue + } + if !lastWasUpper && unicode.IsUpper(c) { + upper = true + } + + if upper { + buffer = append(buffer, unicode.ToUpper(c)) + } else { + buffer = append(buffer, unicode.ToLower(c)) + } + upper = false + lastWasUpper = unicode.IsUpper(c) + } + + return string(buffer) +} + +func rawQuote(s string) string { + return "`" + strings.Replace(s, "`", "`+\"`\"+`", -1) + "`" +} + +func dump(val interface{}) string { + switch val := val.(type) { + case int: + return strconv.Itoa(val) + case float64: + return fmt.Sprintf("%f", val) + case string: + return strconv.Quote(val) + case bool: + return strconv.FormatBool(val) + case nil: + return "nil" + case []interface{}: + var parts []string + for _, part := range val { + parts = append(parts, dump(part)) + } + return "[]interface{}{" + strings.Join(parts, ",") + "}" + case map[string]interface{}: + buf := bytes.Buffer{} + buf.WriteString("map[string]interface{}{") + var keys []string + for key := range val { + keys = append(keys, key) + } + sort.Strings(keys) + + for _, key := range keys { + data := val[key] + + buf.WriteString(strconv.Quote(key)) + buf.WriteString(":") + buf.WriteString(dump(data)) + buf.WriteString(",") + } + buf.WriteString("}") + return buf.String() + default: + panic(fmt.Errorf("unsupported type %T", val)) + } +} + +func prefixLines(prefix, s string) string { + return prefix + strings.Replace(s, "\n", "\n"+prefix, -1) +} |