From 446c3df37f727fd638442669f2e86b12fa688c3a Mon Sep 17 00:00:00 2001 From: zdebra Date: Sun, 7 Jul 2019 22:36:40 +0200 Subject: [PATCH] fixed generating a description to golang comments for enum type --- plugin/modelgen/models.gotpl | 2 +- plugin/modelgen/models_test.go | 17 +++++++ plugin/modelgen/out/generated.go | 59 +++++++++++++++++++++++++ plugin/modelgen/testdata/schema.graphql | 18 ++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/plugin/modelgen/models.gotpl b/plugin/modelgen/models.gotpl index 6df200ee064..a4579f87612 100644 --- a/plugin/modelgen/models.gotpl +++ b/plugin/modelgen/models.gotpl @@ -36,7 +36,7 @@ {{- end}} {{ range $enum := .Enums }} - {{ with .Description|go }} {{.|prefixLines "// "}} {{end}} + {{ with .Description }} {{.|prefixLines "// "}} {{end}} type {{.Name|go }} string const ( {{- range $value := .Values}} diff --git a/plugin/modelgen/models_test.go b/plugin/modelgen/models_test.go index a91c4c1d47a..619157625c0 100644 --- a/plugin/modelgen/models_test.go +++ b/plugin/modelgen/models_test.go @@ -1,7 +1,10 @@ package modelgen import ( + "go/parser" + "go/token" "io/ioutil" + "strings" "testing" "github.com/99designs/gqlgen/codegen/config" @@ -19,10 +22,24 @@ func TestModelGeneration(t *testing.T) { require.True(t, cfg.Models.UserDefined("MissingEnum")) require.True(t, cfg.Models.UserDefined("MissingUnion")) require.True(t, cfg.Models.UserDefined("MissingInterface")) + require.True(t, cfg.Models.UserDefined("TypeWithDescription")) + require.True(t, cfg.Models.UserDefined("EnumWithDescription")) + require.True(t, cfg.Models.UserDefined("InterfaceWithDescription")) + require.True(t, cfg.Models.UserDefined("UnionWithDescription")) t.Run("no pointer pointers", func(t *testing.T) { generated, err := ioutil.ReadFile("./out/generated.go") require.NoError(t, err) require.NotContains(t, string(generated), "**") }) + + t.Run("description is generated", func(t *testing.T) { + node, err := parser.ParseFile(token.NewFileSet(), "./out/generated.go", nil, parser.ParseComments) + require.NoError(t, err) + for _, commentGroup := range node.Comments { + text := commentGroup.Text() + words := strings.Split(text, " ") + require.True(t, len(words) > 1, "expected description %q to have more than one word", text) + } + }) } diff --git a/plugin/modelgen/out/generated.go b/plugin/modelgen/out/generated.go index 26974db5e76..4914aa7c528 100644 --- a/plugin/modelgen/out/generated.go +++ b/plugin/modelgen/out/generated.go @@ -8,6 +8,11 @@ import ( "strconv" ) +// InterfaceWithDescription is an interface with a description +type InterfaceWithDescription interface { + IsInterfaceWithDescription() +} + type MissingInterface interface { IsMissingInterface() } @@ -16,6 +21,11 @@ type MissingUnion interface { IsMissingUnion() } +// UnionWithDescription is an union with a description +type UnionWithDescription interface { + IsUnionWithDescription() +} + type MissingInput struct { Name *string `json:"name"` Enum *MissingEnum `json:"enum"` @@ -47,6 +57,55 @@ func (MissingTypeNullable) IsExistingInterface() {} func (MissingTypeNullable) IsMissingUnion() {} func (MissingTypeNullable) IsExistingUnion() {} +// TypeWithDescription is a type with a description +type TypeWithDescription struct { + Name *string `json:"name"` +} + +func (TypeWithDescription) IsUnionWithDescription() {} + +// EnumWithDescription is an enum with a description +type EnumWithDescription string + +const ( + EnumWithDescriptionCat EnumWithDescription = "CAT" + EnumWithDescriptionDog EnumWithDescription = "DOG" +) + +var AllEnumWithDescription = []EnumWithDescription{ + EnumWithDescriptionCat, + EnumWithDescriptionDog, +} + +func (e EnumWithDescription) IsValid() bool { + switch e { + case EnumWithDescriptionCat, EnumWithDescriptionDog: + return true + } + return false +} + +func (e EnumWithDescription) String() string { + return string(e) +} + +func (e *EnumWithDescription) UnmarshalGQL(v interface{}) error { + str, ok := v.(string) + if !ok { + return fmt.Errorf("enums must be strings") + } + + *e = EnumWithDescription(str) + if !e.IsValid() { + return fmt.Errorf("%s is not a valid EnumWithDescription", str) + } + return nil +} + +func (e EnumWithDescription) MarshalGQL(w io.Writer) { + fmt.Fprint(w, strconv.Quote(e.String())) +} + type MissingEnum string const ( diff --git a/plugin/modelgen/testdata/schema.graphql b/plugin/modelgen/testdata/schema.graphql index b14d1ee858b..c896edae197 100644 --- a/plugin/modelgen/testdata/schema.graphql +++ b/plugin/modelgen/testdata/schema.graphql @@ -65,3 +65,21 @@ interface ExistingInterface { union ExistingUnion = MissingTypeNotNull | MissingTypeNullable | ExistingType +"TypeWithDescription is a type with a description" +type TypeWithDescription { + name: String +} + +"EnumWithDescription is an enum with a description" +enum EnumWithDescription { + CAT + DOG +} + +"InterfaceWithDescription is an interface with a description" +interface InterfaceWithDescription { + name: String +} + +"UnionWithDescription is an union with a description" +union UnionWithDescription = TypeWithDescription | ExistingType