diff --git a/langs/go/go.go b/langs/go/go.go index f8f2607..2bd4abd 100644 --- a/langs/go/go.go +++ b/langs/go/go.go @@ -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") diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_enum.firemodel.go b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_enum.firemodel.go index f07ee63..5911a60 100644 --- a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_enum.firemodel.go +++ b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_enum.firemodel.go @@ -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] +}