Skip to content

Commit

Permalink
resolver: fix case-insensitive file name collision (#2829)
Browse files Browse the repository at this point in the history
for compatibility with Windows/Mac OS X, Go does not allow files with
identical case-insensitive names.
this commit changes the key of the 'files' map to use the lower case
file name, keeping the original file name as a property on the File
object.
  • Loading branch information
erankor authored Oct 15, 2023
1 parent 9c5fc30 commit e5c17d6
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {

for _, o := range objects {
if o.HasResolvers() {
fn := gqlToResolverName(data.Config.Resolver.Dir(), o.Position.Src.Name, data.Config.Resolver.FilenameTemplate)
fnCase := gqlToResolverName(data.Config.Resolver.Dir(), o.Position.Src.Name, data.Config.Resolver.FilenameTemplate)
fn := strings.ToLower(fnCase)
if files[fn] == nil {
files[fn] = &File{}
files[fn] = &File{
name: fnCase,
}
}

caser := cases.Title(language.English, cases.NoLower)
Expand Down Expand Up @@ -150,25 +153,28 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
}

resolver := Resolver{o, f, rewriter.GetPrevDecl(structName, f.GoFieldName), comment, implementation}
fn := gqlToResolverName(data.Config.Resolver.Dir(), f.Position.Src.Name, data.Config.Resolver.FilenameTemplate)
fnCase := gqlToResolverName(data.Config.Resolver.Dir(), f.Position.Src.Name, data.Config.Resolver.FilenameTemplate)
fn := strings.ToLower(fnCase)
if files[fn] == nil {
files[fn] = &File{}
files[fn] = &File{
name: fnCase,
}
}

files[fn].Resolvers = append(files[fn].Resolvers, &resolver)
}
}

for filename, file := range files {
file.imports = rewriter.ExistingImports(filename)
file.RemainingSource = rewriter.RemainingSource(filename)
for _, file := range files {
file.imports = rewriter.ExistingImports(file.name)
file.RemainingSource = rewriter.RemainingSource(file.name)
}
newResolverTemplate := resolverTemplate
if data.Config.Resolver.ResolverTemplate != "" {
newResolverTemplate = readResolverTemplate(data.Config.Resolver.ResolverTemplate)
}

for filename, file := range files {
for _, file := range files {
resolverBuild := &ResolverBuild{
File: file,
PackageName: data.Config.Resolver.Package,
Expand All @@ -192,7 +198,7 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
err := templates.Render(templates.Options{
PackageName: data.Config.Resolver.Package,
FileNotice: fileNotice.String(),
Filename: filename,
Filename: file.name,
Data: resolverBuild,
Packages: data.Config.Packages,
Template: newResolverTemplate,
Expand Down Expand Up @@ -230,6 +236,7 @@ type ResolverBuild struct {
}

type File struct {
name string
// These are separated because the type definition of the resolver object may live in a different file from the
// resolver method implementations, for example when extending a type in a different graphql schema file
Objects []*codegen.Object
Expand Down

0 comments on commit e5c17d6

Please sign in to comment.