Skip to content

Commit

Permalink
Merge pull request 99designs#1034 from 99designs/strip-underscores-fr…
Browse files Browse the repository at this point in the history
…om-entity-interfaces

Trim underscores from around go identifiers
  • Loading branch information
vektah committed Feb 17, 2020
2 parents 8c14d61 + 7f042bb commit 0cae03d
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 16 deletions.
8 changes: 7 additions & 1 deletion codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ func Call(p *types.Func) string {
}

func ToGo(name string) string {
if name == "_" {
return "_"
}
runes := make([]rune, 0, len(name))

wordWalker(name, func(info *wordInfo) {
Expand All @@ -296,6 +299,9 @@ func ToGo(name string) string {
}

func ToGoPrivate(name string) string {
if name == "_" {
return "_"
}
runes := make([]rune, 0, len(name))

first := true
Expand Down Expand Up @@ -331,7 +337,7 @@ type wordInfo struct {
// This function is based on the following code.
// https://github.com/golang/lint/blob/06c8688daad7faa9da5a0c2f163a3d14aac986ca/lint.go#L679
func wordWalker(str string, f func(*wordInfo)) {
runes := []rune(str)
runes := []rune(strings.TrimFunc(str, isDelimiter))
w, i := 0, 0 // index of start of word, scan
hasCommonInitial := false
for i+1 <= len(runes) {
Expand Down
4 changes: 4 additions & 0 deletions codegen/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func TestToGo(t *testing.T) {
require.Equal(t, "ToCamel", ToGo("toCamel"))
require.Equal(t, "ToCamel", ToGo("ToCamel"))
require.Equal(t, "ToCamel", ToGo("to-camel"))
require.Equal(t, "ToCamel", ToGo("-to-camel"))
require.Equal(t, "ToCamel", ToGo("_to-camel"))
require.Equal(t, "_", ToGo("_"))

require.Equal(t, "RelatedURLs", ToGo("RelatedURLs"))
require.Equal(t, "ImageIDs", ToGo("ImageIDs"))
Expand Down Expand Up @@ -63,6 +66,7 @@ func TestToGoPrivate(t *testing.T) {
require.Equal(t, "id", ToGoPrivate("ID"))
require.Equal(t, "id", ToGoPrivate("id"))
require.Equal(t, "", ToGoPrivate(""))
require.Equal(t, "_", ToGoPrivate("_"))
}

func Test_wordWalker(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/generated.go

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

6 changes: 3 additions & 3 deletions example/federation/accounts/graph/generated/generated.go

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

2 changes: 1 addition & 1 deletion example/federation/accounts/graph/model/models_gen.go

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

6 changes: 3 additions & 3 deletions example/federation/products/graph/generated/generated.go

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

2 changes: 1 addition & 1 deletion example/federation/products/graph/model/models_gen.go

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

6 changes: 3 additions & 3 deletions example/federation/reviews/graph/generated/generated.go

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

4 changes: 2 additions & 2 deletions example/federation/reviews/graph/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Product struct {
Upc string `json:"upc"`
}

func (Product) Is_Entity() {}
func (Product) IsEntity() {}

type Review struct {
Body string
Expand All @@ -16,4 +16,4 @@ type User struct {
ID string `json:"id"`
}

func (User) Is_Entity() {}
func (User) IsEntity() {}
2 changes: 1 addition & 1 deletion plugin/federation/fedruntime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type Service struct {

// Everything with a @key implements this
type Entity interface {
Is_Entity()
IsEntity()
}
5 changes: 5 additions & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin/modelgen/out"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -63,6 +64,10 @@ func TestModelGeneration(t *testing.T) {
require.True(t, strings.Contains(fileText, tag))
}
})

t.Run("concrete types implement interface", func(t *testing.T) {
var _ out.FooBarer = out.FooBarr{}
})
}

func mutateHook(b *ModelBuild) *ModelBuild {
Expand Down
10 changes: 10 additions & 0 deletions plugin/modelgen/out/generated.go

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

9 changes: 9 additions & 0 deletions plugin/modelgen/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ interface InterfaceWithDescription {

"UnionWithDescription is an union with a description"
union UnionWithDescription = TypeWithDescription | ExistingType


interface Foo_Barer {
name: String!
}

type _Foo_Barr implements Foo_Barer {
name: String!
}

0 comments on commit 0cae03d

Please sign in to comment.