-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from loomnetwork/enum_abilities_template
C#, Go, Solidity Enum Generator
- Loading branch information
Showing
6 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/iancoleman/strcase" | ||
) | ||
|
||
type Ability struct { | ||
Name string | ||
Types []string | ||
} | ||
|
||
//FileStruct abilities dataset from json | ||
type FileStruct struct { | ||
Abilities []Ability | ||
Actions []Ability | ||
StaticConfigs []Ability | ||
} | ||
|
||
var fns = template.FuncMap{ | ||
"last": func(x int, a []string) bool { | ||
fmt.Printf("x -%d len-%v\n", x, len(a)) | ||
return x == len(a)-1 | ||
}, | ||
"ToUpper": strings.ToUpper, | ||
"ToLower": strings.ToLower, | ||
"ToCamel": strcase.ToCamel, | ||
} | ||
|
||
func outputTemple(f io.Writer, ab *FileStruct, templateBaseName, templatefile string) { | ||
t, err := template.New(templateBaseName).Funcs(fns).ParseFiles(templatefile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = t.Execute(f, ab) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func outputTemplate(outputfile string, ab *FileStruct, templateBaseName, templatefile string) { | ||
filename := outputfile | ||
|
||
if filename == "-" { | ||
outputTemple(os.Stdout, ab, templateBaseName, templatefile) | ||
} | ||
|
||
f, err := os.Create(filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
fmt.Printf("\noutputfile file -%s\n", filename) | ||
outputTemple(f, ab, templateBaseName, templatefile) | ||
f.Sync() | ||
} | ||
|
||
func main() { | ||
ab := &FileStruct{} | ||
|
||
byteValue, err := ioutil.ReadFile("tools/cmd/templates/inputdata.json") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
json.Unmarshal(byteValue, &ab) | ||
|
||
outputTemplate("Enumerators.cs", ab, "csharpabilities.parse", "tools/cmd/templates/csharpabilities.parse") | ||
outputTemplate("enum.sol", ab, "soliditybilities.parse", "tools/cmd/templates/soliditybilities.parse") | ||
outputTemplate("ability.go", ab, "goabilities.parse", "tools/cmd/templates/goabilities.parse") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//THIS FILE IS AUTO GENERATED IN THE BACKEND REPO | ||
//TO MAKE CHANGES YOU NEED TO EDIT THE GAMECHAIN REPO | ||
|
||
namespace Loom.ZombieBattleground.Common | ||
{ | ||
public class Enumerators | ||
{ | ||
|
||
public const string JSONVersion = '0.1' //constant for what json file generated this | ||
|
||
{{ with .Abilities }} | ||
{{ range . }} | ||
public enum Ability{{.Name}}Type | ||
{ | ||
{{ with .Types }} | ||
{{range $i, $e := . }} | ||
{{- if $i}},{{end}} | ||
{{$e}} | ||
{{- end }} | ||
{{ end -}} | ||
} | ||
{{ end }} | ||
{{ end }} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ability | ||
|
||
{{ with .Abilities }} | ||
{{ range . }} | ||
const ( {{- $name := .Name}} | ||
{{- range $i, $e := .Types }} | ||
{{$name | ToUpper }}_{{$e }} = iota | ||
{{- end }} | ||
) | ||
|
||
{{ end }} | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"abilities": [ | ||
{ | ||
"name": "Activity", | ||
"types": [ | ||
"PASSIVE", | ||
"ACTIVE" | ||
] | ||
}, | ||
{ | ||
"name": "Call", | ||
"types": [ | ||
"TURN", | ||
"ENTRY", | ||
"END", | ||
"ATTACK", | ||
"DEATH", | ||
"PERMANENT", | ||
"GOT_DAMAGE", | ||
"AT_DEFENCE", | ||
"IN_HAND" | ||
] | ||
} | ||
|
||
], | ||
"actions": [ | ||
{ | ||
"name": "Player", | ||
"types": [ | ||
"StartGame", | ||
"EndTurn", | ||
"DrawCardPlayer", | ||
"PlayCard", | ||
"CardAttack", | ||
"UseCardAbility" | ||
] | ||
} | ||
|
||
], | ||
"staticConfigs": [ | ||
{ | ||
"name": "GameConfig", | ||
"types": [ | ||
"Health", | ||
"CustomDeck", | ||
"AddCard", | ||
"SetDeck" | ||
] | ||
} | ||
|
||
] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//THIS FILE IS AUTO GENERATED IN THE GAMECHAIN REPO | ||
//TO MAKE CHANGES YOU NEED TO EDIT THE GAMECHAIN REPO | ||
|
||
pragma solidity ^0.4.24; | ||
|
||
contract ZUBGEnum { | ||
{{ with .Abilities }} | ||
{{ range . }} | ||
enum Ability{{.Name}} { | ||
{{- with .Types -}} | ||
{{range $i, $e := . }} | ||
{{- if $i}},{{end -}} | ||
{{$e | ToLower | ToCamel }} | ||
{{- end -}} | ||
{{- end -}} | ||
} | ||
Ability{{.Name}} {{.Name | ToLower }}; | ||
{{ end }} | ||
{{ end }} | ||
|
||
|
||
{{- with .Actions }} | ||
{{ range . }} | ||
enum Action{{.Name}} { | ||
{{- with .Types -}} | ||
{{range $i, $e := . }} | ||
{{- if $i}},{{end -}} | ||
{{$e | ToLower | ToCamel }} | ||
{{- end -}} | ||
{{- end -}} | ||
} | ||
Action{{.Name}} {{.Name | ToLower }}; | ||
{{ end }} | ||
{{ end }} | ||
|
||
{{- with .StaticConfigs }} | ||
{{ range . }} | ||
enum Static{{.Name}} { | ||
{{- with .Types -}} | ||
{{range $i, $e := . }} | ||
{{- if $i}},{{end -}} | ||
{{$e | ToLower | ToCamel }} | ||
{{- end -}} | ||
{{- end -}} | ||
} | ||
Static{{.Name}} {{.Name | ToLower }}; | ||
{{ end }} | ||
{{ end }} | ||
|
||
} |