Skip to content

Commit

Permalink
Merge pull request 99designs#486 from nicovogelaar/feature/list-of-enums
Browse files Browse the repository at this point in the history
add list of enums
  • Loading branch information
vektah authored Jan 5, 2019
2 parents 315315c + 210eede commit fc94356
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
30 changes: 30 additions & 0 deletions codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func TestGenerateServer(t *testing.T) {
id: Int
fist_name: String
}
enum Status {
OK
ERROR
}
`
serverFilename := "gen/" + name + "/server/server.go"
cfg := Config{
Expand All @@ -42,4 +46,30 @@ func TestGenerateServer(t *testing.T) {

_, err = conf.Load()
require.NoError(t, err)

t.Run("list of enums", func(t *testing.T) {
conf = loader.Config{}
conf.CreateFromFilenames("gen/"+name, "gen/"+name+"/model.go")

program, err := conf.Load()
require.NoError(t, err)

found := false

for _, c := range program.Created {
for ident := range c.Defs {
if ident.Name == "AllStatus" {
found = true
break
}
}
if found {
break
}
}

if !found {
t.Fail()
}
})
}
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions codegen/templates/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ import (
{{- end }}
)

var All{{.GoType}} = []{{.GoType}}{
{{- range $value := .Values}}
{{$enum.GoType}}{{ .Name|toCamel }},
{{- end }}
}

func (e {{.GoType}}) IsValid() bool {
switch e {
case {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:
Expand Down
11 changes: 11 additions & 0 deletions example/starwars/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions example/todo/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions example/type-system-extension/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions integration/models-go/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc94356

Please sign in to comment.