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

fixed model gen for multiple implemented type #2021

Merged
merged 1 commit into from
Mar 2, 2022
Merged
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
17 changes: 16 additions & 1 deletion plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,23 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
Description: schemaType.Description,
Name: schemaType.Name,
}

// If Interface A implements interface B, and Interface C also implements interface B
// then both A and C have methods of B.
// The reason for checking unique is to prevent the same method B from being generated twice.
uniqueMap := map[string]bool{}
for _, implementor := range cfg.Schema.GetImplements(schemaType) {
it.Implements = append(it.Implements, implementor.Name)
if !uniqueMap[implementor.Name] {
it.Implements = append(it.Implements, implementor.Name)
uniqueMap[implementor.Name] = true
}
// for interface implements
for _, iface := range implementor.Interfaces {
if !uniqueMap[iface] {
it.Implements = append(it.Implements, iface)
uniqueMap[iface] = true
}
}
}

for _, field := range schemaType.Fields {
Expand Down
33 changes: 33 additions & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/token"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -177,6 +178,38 @@ func TestModelGeneration(t *testing.T) {
})
}
})

t.Run("implemented interfaces type CDImplemented", func(t *testing.T) {
pkg, err := parseAst("out")
require.NoError(t, err)

path := filepath.Join("out", "generated.go")
generated := pkg.Files[path]

wantMethods := []string{
"IsA",
"IsB",
"IsC",
"IsD",
}

gots := make([]string, 0, len(wantMethods))
for _, decl := range generated.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
switch funcDecl.Name.Name {
case "IsA", "IsB", "IsC", "IsD":
gots = append(gots, funcDecl.Name.Name)
require.Len(t, funcDecl.Recv.List, 1)
recvIdent, ok := funcDecl.Recv.List[0].Type.(*ast.Ident)
require.True(t, ok)
require.Equal(t, "CDImplemented", recvIdent.Name)
}
}
}

sort.Strings(gots)
require.Equal(t, wantMethods, gots)
})
}

func mutateHook(b *ModelBuild) *ModelBuild {
Expand Down
12 changes: 12 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.

7 changes: 7 additions & 0 deletions plugin/modelgen/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,11 @@ interface D implements A & B {
a: String!
b: Int!
d: String
}

type CDImplemented implements C & D {
a: String!
b: Int!
c: Boolean!
d: String
}