-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
307 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"go/types" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/99designs/gqlgen/internal/code" | ||
) | ||
|
||
type PackageConfig struct { | ||
Filename string `yaml:"filename,omitempty"` | ||
Package string `yaml:"package,omitempty"` | ||
Type string `yaml:"type,omitempty"` | ||
} | ||
|
||
func (c *PackageConfig) normalize() error { | ||
if c.Filename != "" { | ||
c.Filename = abs(c.Filename) | ||
} | ||
// If Package is not set, first attempt to load the package at the output dir. If that fails | ||
// fallback to just the base dir name of the output filename. | ||
if c.Package == "" { | ||
c.Package = code.NameForDir(c.Dir()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *PackageConfig) ImportPath() string { | ||
return code.ImportPathForDir(c.Dir()) | ||
} | ||
|
||
func (c *PackageConfig) Dir() string { | ||
return filepath.Dir(c.Filename) | ||
} | ||
|
||
func (c *PackageConfig) Pkg() *types.Package { | ||
return types.NewPackage(c.ImportPath(), c.Dir()) | ||
} | ||
|
||
func (c *PackageConfig) IsDefined() bool { | ||
return c.Filename != "" | ||
} | ||
|
||
func (c *PackageConfig) Check(filesMap map[string]bool, pkgConfigsByDir map[string]*PackageConfig) error { | ||
if err := c.normalize(); err != nil { | ||
return err | ||
} | ||
if strings.ContainsAny(c.Package, "./\\") { | ||
return fmt.Errorf("package should be the output package name only, do not include the output filename") | ||
} | ||
if c.Filename != "" && !strings.HasSuffix(c.Filename, ".go") { | ||
return fmt.Errorf("filename should be path to a go source file") | ||
} | ||
|
||
_, fileFound := filesMap[c.Filename] | ||
if fileFound { | ||
return fmt.Errorf("filename %s defined more than once", c.Filename) | ||
} | ||
filesMap[c.Filename] = true | ||
previous, inSameDir := pkgConfigsByDir[c.Dir()] | ||
if inSameDir && c.Package != previous.Package { | ||
return fmt.Errorf("filenames %s and %s are in the same directory but have different package definitions (%s vs %s)", stripPath(c.Filename), stripPath(previous.Filename), c.Package, previous.Package) | ||
} | ||
pkgConfigsByDir[c.Dir()] = c | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"go/types" | ||
"path/filepath" | ||
|
||
"github.com/99designs/gqlgen/internal/code" | ||
) | ||
|
||
type ResolverConfig struct { | ||
PackageConfig `yaml:",inline"` | ||
Layout ResolverLayout `yaml:"layout,omitempty"` | ||
DirName string `yaml:"dir"` | ||
} | ||
|
||
type ResolverLayout string | ||
|
||
var ( | ||
LayoutSingleFile ResolverLayout = "single-file" | ||
LayoutFollowSchema ResolverLayout = "follow-schema" | ||
) | ||
|
||
func (r *ResolverConfig) Check(filesMap map[string]bool, pkgConfigsByDir map[string]*PackageConfig) error { | ||
if r.DirName != "" { | ||
r.DirName = abs(r.DirName) | ||
} | ||
|
||
if r.Layout == "" { | ||
r.Layout = "single-file" | ||
} | ||
if r.Layout != LayoutFollowSchema && r.Layout != LayoutSingleFile { | ||
return fmt.Errorf("invalid layout %s. must be single-file or follow-schema", r.Layout) | ||
} | ||
|
||
if r.Layout == "follow-schema" && r.DirName == "" { | ||
return fmt.Errorf("must specify dir when using laout:follow-schema") | ||
} | ||
|
||
return r.PackageConfig.Check(filesMap, pkgConfigsByDir) | ||
} | ||
|
||
func (r *ResolverConfig) ImportPath() string { | ||
return code.ImportPathForDir(r.Dir()) | ||
} | ||
|
||
func (r *ResolverConfig) Dir() string { | ||
switch r.Layout { | ||
case LayoutSingleFile: | ||
return filepath.Dir(r.Filename) | ||
case LayoutFollowSchema: | ||
return r.DirName | ||
default: | ||
panic("invalid layout " + r.Layout) | ||
} | ||
} | ||
|
||
func (r *ResolverConfig) Pkg() *types.Package { | ||
return types.NewPackage(r.ImportPath(), r.Dir()) | ||
} | ||
|
||
func (r *ResolverConfig) IsDefined() bool { | ||
switch r.Layout { | ||
case LayoutSingleFile, "": | ||
return r.Filename != "" | ||
case LayoutFollowSchema: | ||
return r.DirName != "" | ||
default: | ||
panic(fmt.Errorf("invalid layout %s", r.Layout)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.