Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to generate int enums #1347

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions codegen/testserver/models-gen.go

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

14 changes: 6 additions & 8 deletions example/starwars/models/generated.go

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

7 changes: 3 additions & 4 deletions example/todo/models_gen.go

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

7 changes: 3 additions & 4 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: 6 additions & 8 deletions integration/models-go/generated.go

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

2 changes: 2 additions & 0 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Enum struct {
Description string
Name string
Values []*EnumValue
IntValues bool // Whether generated types should be integers
}

type EnumValue struct {
Expand Down Expand Up @@ -175,6 +176,7 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
it := &Enum{
Name: schemaType.Name,
Description: schemaType.Description,
IntValues: schemaType.Directives.ForName("intEnum") != nil,
}

for _, v := range schemaType.EnumValues {
Expand Down
61 changes: 45 additions & 16 deletions plugin/modelgen/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,35 @@

{{ range $enum := .Enums }}
{{ with .Description }} {{.|prefixLines "// "}} {{end}}
type {{.Name|go }} string
type {{.Name|go }} {{if $enum.IntValues }}int{{else}}string{{end}}
const (
{{- range $value := .Values}}
{{- range $index, $value := .Values}}
{{- with .Description}}
{{.|prefixLines "// "}}
{{- end}}
{{ $enum.Name|go }}{{ .Name|go }} {{$enum.Name|go }} = {{.Name|quote}}
{{ $enum.Name|go }}{{ .Name|go }} {{$enum.Name|go }} = {{if $enum.IntValues }}{{$index}}{{else}}{{.Name|quote}}{{end}}
{{- end }}
)

var All{{.Name|go }} = []{{ .Name|go }}{
{{- range $value := .Values}}
{{$enum.Name|go }}{{ .Name|go }},
{{- end }}
}
{{if $enum.IntValues }}
var {{.Name|go }}Name = map[int]string{
{{- range $index, $value := .Values}}
{{$index}}: {{$value.Name|quote}},
{{- end }}
}

var {{.Name|go }}Value = map[string]int{
{{- range $index, $value := .Values}}
{{$value.Name|quote}}: {{$index}},
{{- end }}
}
{{else}}
var All{{.Name|go }} = []{{ .Name|go }}{
{{- range $value := .Values}}
{{$enum.Name|go }}{{ .Name|go }},
{{- end }}
}
{{end}}

func (e {{.Name|go }}) IsValid() bool {
switch e {
Expand All @@ -62,24 +76,39 @@
}

func (e {{.Name|go }}) String() string {
return string(e)
{{ if $enum.IntValues -}}
return {{.Name|go }}Name[int(e)]
{{ else -}}
return string(e)
{{ end -}}
}

func (e *{{.Name|go }}) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}
{{ if $enum.IntValues -}}
value, ok := v.(int)
if !ok {
return fmt.Errorf("enums must be integers")
}
{{ else -}}
value, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}
{{ end -}}

*e = {{ .Name|go }}(str)
*e = {{ .Name|go }}(value)
if !e.IsValid() {
return fmt.Errorf("%s is not a valid {{ .Name }}", str)
return fmt.Errorf("%v is not a valid {{ .Name }}", value)
}
return nil
}

func (e {{.Name|go }}) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(e.String()))
{{ if $enum.IntValues -}}
fmt.Fprint(w, e)
{{ else -}}
fmt.Fprint(w, strconv.Quote(e.String()))
{{ end -}}
}

{{- end }}
1 change: 1 addition & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestModelGeneration(t *testing.T) {
require.True(t, cfg.Models.UserDefined("MissingTypeNotNull"))
require.True(t, cfg.Models.UserDefined("MissingTypeNullable"))
require.True(t, cfg.Models.UserDefined("MissingEnum"))
require.True(t, cfg.Models.UserDefined("MissingIntEnum"))
require.True(t, cfg.Models.UserDefined("MissingUnion"))
require.True(t, cfg.Models.UserDefined("MissingInterface"))
require.True(t, cfg.Models.UserDefined("TypeWithDescription"))
Expand Down
2 changes: 2 additions & 0 deletions plugin/modelgen/out/existing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type ExistingInput struct {

type ExistingEnum string

type ExistingIntEnum int

type ExistingInterface interface {
IsExistingInterface()
}
Expand Down
59 changes: 51 additions & 8 deletions plugin/modelgen/out/generated.go

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

2 changes: 2 additions & 0 deletions plugin/modelgen/testdata/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ models:
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingInput
ExistingEnum:
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingEnum
ExistingIntEnum:
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingIntEnum
ExistingInterface:
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingInterface
ExistingUnion:
Expand Down
12 changes: 12 additions & 0 deletions plugin/modelgen/testdata/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
directive @intEnum on ENUM

type Query {
thisShoudlntGetGenerated: Boolean
}
Expand Down Expand Up @@ -36,6 +38,11 @@ enum MissingEnum {
Goodbye
}

enum MissingIntEnum @intEnum {
Hello
Goodbye
}

interface MissingInterface {
name: String
}
Expand All @@ -59,6 +66,11 @@ enum ExistingEnum {
Goodbye
}

enum ExistingIntEnum @intEnum {
Hello
Goodbye
}

interface ExistingInterface {
name: String
}
Expand Down