github.com/loomnetwork/gamechain@v0.0.0-20200406110549-36c47eb97a92/templates (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "strings" 10 "text/template" 11 12 "github.com/iancoleman/strcase" 13 ) 14 15 type Ability struct { 16 Name string 17 Types []string 18 } 19 20 //FileStruct abilities dataset from json 21 type FileStruct struct { 22 Abilities []Ability 23 Actions []Ability 24 StaticConfigs []Ability 25 } 26 27 var fns = template.FuncMap{ 28 "last": func(x int, a []string) bool { 29 fmt.Printf("x -%d len-%v\n", x, len(a)) 30 return x == len(a)-1 31 }, 32 "ToUpper": strings.ToUpper, 33 "ToLower": strings.ToLower, 34 "ToCamel": strcase.ToCamel, 35 } 36 37 func outputTemple(f io.Writer, ab *FileStruct, templateBaseName, templatefile string) { 38 t, err := template.New(templateBaseName).Funcs(fns).ParseFiles(templatefile) 39 if err != nil { 40 panic(err) 41 } 42 err = t.Execute(f, ab) 43 if err != nil { 44 panic(err) 45 } 46 } 47 48 func outputTemplate(outputfile string, ab *FileStruct, templateBaseName, templatefile string) { 49 filename := outputfile 50 51 if filename == "-" { 52 outputTemple(os.Stdout, ab, templateBaseName, templatefile) 53 } 54 55 f, err := os.Create(filename) 56 if err != nil { 57 panic(err) 58 } 59 defer f.Close() 60 fmt.Printf("\noutputfile file -%s\n", filename) 61 outputTemple(f, ab, templateBaseName, templatefile) 62 f.Sync() 63 } 64 65 func main() { 66 ab := &FileStruct{} 67 68 byteValue, err := ioutil.ReadFile("tools/cmd/templates/inputdata.json") 69 if err != nil { 70 panic(err) 71 } 72 73 json.Unmarshal(byteValue, &ab) 74 75 outputTemplate("Enumerators.cs", ab, "csharpabilities.parse", "tools/cmd/templates/csharpabilities.parse") 76 outputTemplate("enum.sol", ab, "soliditybilities.parse", "tools/cmd/templates/soliditybilities.parse") 77 outputTemplate("ability.go", ab, "goabilities.parse", "tools/cmd/templates/goabilities.parse") 78 }