github.com/99designs/gqlgen@v0.17.45/codegen/config/package.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "go/types" 6 "path/filepath" 7 "strings" 8 9 "github.com/99designs/gqlgen/internal/code" 10 ) 11 12 type PackageConfig struct { 13 Filename string `yaml:"filename,omitempty"` 14 Package string `yaml:"package,omitempty"` 15 Version int `yaml:"version,omitempty"` 16 ModelTemplate string `yaml:"model_template,omitempty"` 17 Options map[string]bool `yaml:"options,omitempty"` 18 } 19 20 func (c *PackageConfig) ImportPath() string { 21 if !c.IsDefined() { 22 return "" 23 } 24 return code.ImportPathForDir(c.Dir()) 25 } 26 27 func (c *PackageConfig) Dir() string { 28 if !c.IsDefined() { 29 return "" 30 } 31 return filepath.Dir(c.Filename) 32 } 33 34 func (c *PackageConfig) Pkg() *types.Package { 35 if !c.IsDefined() { 36 return nil 37 } 38 return types.NewPackage(c.ImportPath(), c.Package) 39 } 40 41 func (c *PackageConfig) IsDefined() bool { 42 return c.Filename != "" 43 } 44 45 func (c *PackageConfig) Check() error { 46 if strings.ContainsAny(c.Package, "./\\") { 47 return fmt.Errorf("package should be the output package name only, do not include the output filename") 48 } 49 if c.Filename == "" { 50 return fmt.Errorf("filename must be specified") 51 } 52 if !strings.HasSuffix(c.Filename, ".go") { 53 return fmt.Errorf("filename should be path to a go source file") 54 } 55 56 c.Filename = abs(c.Filename) 57 58 // If Package is not set, first attempt to load the package at the output dir. If that fails 59 // fallback to just the base dir name of the output filename. 60 if c.Package == "" { 61 c.Package = code.NameForDir(c.Dir()) 62 } 63 64 return nil 65 }