github.com/senomas/gqlgen@v0.17.11-0.20220626120754-9aee61b0716a/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 } 17 18 func (c *PackageConfig) ImportPath() string { 19 if !c.IsDefined() { 20 return "" 21 } 22 return code.ImportPathForDir(c.Dir()) 23 } 24 25 func (c *PackageConfig) Dir() string { 26 if !c.IsDefined() { 27 return "" 28 } 29 return filepath.Dir(c.Filename) 30 } 31 32 func (c *PackageConfig) Pkg() *types.Package { 33 if !c.IsDefined() { 34 return nil 35 } 36 return types.NewPackage(c.ImportPath(), c.Package) 37 } 38 39 func (c *PackageConfig) IsDefined() bool { 40 return c.Filename != "" 41 } 42 43 func (c *PackageConfig) Check() error { 44 if strings.ContainsAny(c.Package, "./\\") { 45 return fmt.Errorf("package should be the output package name only, do not include the output filename") 46 } 47 if c.Filename == "" { 48 return fmt.Errorf("filename must be specified") 49 } 50 if !strings.HasSuffix(c.Filename, ".go") { 51 return fmt.Errorf("filename should be path to a go source file") 52 } 53 54 c.Filename = abs(c.Filename) 55 56 // If Package is not set, first attempt to load the package at the output dir. If that fails 57 // fallback to just the base dir name of the output filename. 58 if c.Package == "" { 59 c.Package = code.NameForDir(c.Dir()) 60 } 61 62 return nil 63 }