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