github.com/machinebox/remoto@v0.1.2-0.20191024144331-eff21a7d321f/generator/template_helpers.go (about) 1 package generator 2 3 import ( 4 "sort" 5 "strings" 6 7 "github.com/machinebox/remoto/generator/definition" 8 "github.com/markbates/inflect" 9 ) 10 11 var defaultRuleset = inflect.NewDefaultRuleset() 12 13 // Setter may have data set on it, usually a plush context. 14 type Setter interface { 15 Set(name string, value interface{}) 16 } 17 18 // AddTemplateHelpers adds all the built-in template helpers. 19 func AddTemplateHelpers(s Setter) { 20 s.Set("unique_structures", uniqueStructures) 21 s.Set("print_comment", printComment) 22 s.Set("go_type_string", goTypeString) 23 s.Set("underscore", underscore) 24 s.Set("camelize_down", camelizeDownFirst) 25 26 // experimental (undocumented) 27 s.Set("replace", replace) 28 } 29 30 // underscore converts a type name or other string into an underscored 31 // version. For example, "ModelID" becomes "model_id". 32 func underscore(s string) string { 33 return defaultRuleset.Underscore(s) 34 } 35 36 // camelizeDownFirst converts a name or other string into a camel case 37 // version with the first letter lowercase. "ModelID" becomes "modelID". 38 func camelizeDownFirst(s string) string { 39 if s == "ID" { 40 return "id" 41 // note: not sure why I need this, there's a lot that deals with 42 // accronyms in the dependency packages but they don't seem to behave 43 // as expected in this case. 44 } 45 return defaultRuleset.CamelizeDownFirst(s) 46 } 47 48 // uniqueStructures gets all unique Structure types from all services. 49 // Structures with the same name are considered the same. 50 // Use unique_structures(def) in templates. 51 func uniqueStructures(def definition.Definition) []definition.Structure { 52 structures := make(map[string]definition.Structure) 53 for _, service := range def.Services { 54 for _, structure := range service.Structures { 55 if structure.IsImported { 56 continue 57 } 58 structures[structure.Name] = structure 59 } 60 } 61 s := make([]definition.Structure, 0, len(structures)) 62 for _, structure := range structures { 63 s = append(s, structure) 64 } 65 sort.Slice(s, func(i, j int) bool { 66 return s[i].Name < s[j].Name 67 }) 68 return s 69 } 70 71 // printComment prints a comment with // prefix, unless the comment 72 // is empty. 73 // Use print_comment(s) in templates. 74 func printComment(comment string) string { 75 if comment == "" { 76 return "" 77 } 78 var out string 79 for _, line := range strings.Split(comment, "\n") { 80 out += "// " + line + "\n" 81 } 82 return out 83 } 84 85 // goTypeString gets the Type as a Go string. 86 // Use go_type_string(type) in templates. 87 func goTypeString(typ definition.Type) string { 88 if typ.IsMultiple { 89 return "[]" + typ.Name 90 } 91 return typ.Name 92 } 93 94 // replace is a string replacement function. 95 func replace(s, old, new string) string { 96 return strings.Replace(s, old, new, -1) 97 }