Skip to content

Commit

Permalink
Added Enum Maps (#37)
Browse files Browse the repository at this point in the history
Adds Enum Maps to allow converting between strings and enums
  • Loading branch information
omzouai-visor authored and mickeyreiss committed Oct 16, 2018
1 parent ac4a718 commit 610ff11
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 13 additions & 1 deletion langs/go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,19 @@ func (m *GoModeler) writeEnum(enum *firemodel.SchemaEnum, sourceCoder firemodel.
Lit(strcase.ToScreamingSnake(val.Name))
}
})

f.Var().Id(enumName + "_Strings").Op("=").Map(jen.Id(enumName)).String().ValuesFunc(func(g *jen.Group) {
for _, val := range enum.Values {
enumValName := fmt.Sprintf("%s_%s", enumName, strcase.ToScreamingSnake(val.Name))
g.Id(enumValName).Op(":").Lit(enumValName)
}
})
f.Var().Id(enumName + "_Values").Op("=").Map(jen.String()).Id(enumName).ValuesFunc(func(g *jen.Group) {
for _, val := range enum.Values {
enumValName := fmt.Sprintf("%s_%s", enumName, strcase.ToScreamingSnake(val.Name))
g.Lit(enumValName).Op(":").Id(enumValName)
}
})
f.Func().Params(jen.Id("e").Id(enumName)).Id("String").Params().String().Block(jen.Return(jen.Id(enumName + "_Strings").Index(jen.Id("e"))))
w, err := sourceCoder.NewFile(fmt.Sprint(strcase.ToSnake(enum.Name), fileExtension))
if err != nil {
return errors.Wrap(err, "firemodel/go: open source code file")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ const (
// TODO: Add comment to TestEnum_DOWN in firemodel schema.
TestEnum_DOWN TestEnum = "DOWN"
)

var TestEnum_Strings = map[TestEnum]string{TestEnum_LEFT: "TestEnum_LEFT", TestEnum_RIGHT: "TestEnum_RIGHT", TestEnum_UP: "TestEnum_UP", TestEnum_DOWN: "TestEnum_DOWN"}
var TestEnum_Values = map[string]TestEnum{"TestEnum_LEFT": TestEnum_LEFT, "TestEnum_RIGHT": TestEnum_RIGHT, "TestEnum_UP": TestEnum_UP, "TestEnum_DOWN": TestEnum_DOWN}

func (e TestEnum) String() string {
return TestEnum_Strings[e]
}

0 comments on commit 610ff11

Please sign in to comment.