-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Consider go type name when autobinding #2812
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,6 +206,31 @@ func TestAutobinding(t *testing.T) { | |
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.Message", cfg.Models["Message"].Model[0]) | ||
}) | ||
|
||
t.Run("normalized type names", func(t *testing.T) { | ||
cfg := Config{ | ||
Models: TypeMap{}, | ||
AutoBind: []string{ | ||
"github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat", | ||
"github.com/99designs/gqlgen/codegen/config/testdata/autobinding/scalars/model", | ||
}, | ||
Packages: code.NewPackages(), | ||
} | ||
|
||
cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: ` | ||
scalar Banned | ||
type Message { id: ID } | ||
enum ProductSKU { ProductSkuTrial } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one here is an example that wouldn't had worked sans the change; SKU is not a default initialism, let's imagine the user didn't add it to their config - gqlgen will generate the type as Now imagine this type goes to a shared library which is autobound in another place - ProductSku will be regenerated, as the schema defines it as |
||
type ChatAPI { id: ID } | ||
`}) | ||
|
||
require.NoError(t, cfg.autobind()) | ||
|
||
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/scalars/model.Banned", cfg.Models["Banned"].Model[0]) | ||
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.Message", cfg.Models["Message"].Model[0]) | ||
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.ProductSku", cfg.Models["ProductSKU"].Model[0]) | ||
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.ChatAPI", cfg.Models["ChatAPI"].Model[0]) | ||
}) | ||
|
||
t.Run("with file path", func(t *testing.T) { | ||
cfg := Config{ | ||
Models: TypeMap{}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,10 @@ | ||
package config | ||
|
||
import "strings" | ||
import ( | ||
"strings" | ||
|
||
// commonInitialisms is a set of common initialisms. | ||
// Only add entries that are highly unlikely to be non-initialisms. | ||
// For instance, "ID" is fine (Freudian code is rare), but "AND" is not. | ||
var commonInitialisms = map[string]bool{ | ||
"ACL": true, | ||
"API": true, | ||
"ASCII": true, | ||
"CPU": true, | ||
"CSS": true, | ||
"CSV": true, | ||
"DNS": true, | ||
"EOF": true, | ||
"GUID": true, | ||
"HTML": true, | ||
"HTTP": true, | ||
"HTTPS": true, | ||
"ICMP": true, | ||
"ID": true, | ||
"IP": true, | ||
"JSON": true, | ||
"KVK": true, | ||
"LHS": true, | ||
"PDF": true, | ||
"PGP": true, | ||
"QPS": true, | ||
"QR": true, | ||
"RAM": true, | ||
"RHS": true, | ||
"RPC": true, | ||
"SLA": true, | ||
"SMTP": true, | ||
"SQL": true, | ||
"SSH": true, | ||
"SVG": true, | ||
"TCP": true, | ||
"TLS": true, | ||
"TTL": true, | ||
"UDP": true, | ||
"UI": true, | ||
"UID": true, | ||
"URI": true, | ||
"URL": true, | ||
"UTF8": true, | ||
"UUID": true, | ||
"VM": true, | ||
"XML": true, | ||
"XMPP": true, | ||
"XSRF": true, | ||
"XSS": true, | ||
} | ||
|
||
// GetInitialisms returns the initialisms to capitalize in Go names. If unchanged, default initialisms will be returned | ||
var GetInitialisms = func() map[string]bool { | ||
return commonInitialisms | ||
} | ||
"github.com/99designs/gqlgen/codegen/templates" | ||
) | ||
|
||
// GoInitialismsConfig allows to modify the default behavior of naming Go methods, types and properties | ||
type GoInitialismsConfig struct { | ||
|
@@ -69,7 +17,7 @@ type GoInitialismsConfig struct { | |
// setInitialisms adjustes GetInitialisms based on its settings. | ||
func (i GoInitialismsConfig) setInitialisms() { | ||
toUse := i.determineGoInitialisms() | ||
GetInitialisms = func() map[string]bool { | ||
templates.GetInitialisms = func() map[string]bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved due to cyclic dep, only ref is in templates, so thought it fits there well |
||
return toUse | ||
} | ||
} | ||
|
@@ -82,8 +30,8 @@ func (i GoInitialismsConfig) determineGoInitialisms() (initialismsToUse map[stri | |
initialismsToUse[strings.ToUpper(initialism)] = true | ||
} | ||
} else { | ||
initialismsToUse = make(map[string]bool, len(commonInitialisms)+len(i.Initialisms)) | ||
for initialism, value := range commonInitialisms { | ||
initialismsToUse = make(map[string]bool, len(templates.CommonInitialisms)+len(i.Initialisms)) | ||
for initialism, value := range templates.CommonInitialisms { | ||
initialismsToUse[strings.ToUpper(initialism)] = value | ||
} | ||
for _, initialism := range i.Initialisms { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved due to cyclic dep, only ref is in templates, so thought it fits there well