Skip to content

Commit

Permalink
cleanup new config
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jul 5, 2018
1 parent 6a4c354 commit cb859cd
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 113 deletions.
12 changes: 6 additions & 6 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func (cfg *Config) models() (*ModelBuild, error) {
if err != nil {
return nil, errors.Wrap(err, "loading failed")
}
imports := buildImports(namedTypes, cfg.modelDir)
imports := buildImports(namedTypes, cfg.Model.Dir())

cfg.bindTypes(imports, namedTypes, cfg.modelDir, prog)
cfg.bindTypes(imports, namedTypes, cfg.Model.Dir(), prog)

models, err := cfg.buildModels(namedTypes, prog)
if err != nil {
return nil, err
}
return &ModelBuild{
PackageName: cfg.ModelPackageName,
PackageName: cfg.Model.Package,
Models: models,
Enums: cfg.buildEnums(namedTypes),
Imports: imports.finalize(),
Expand All @@ -62,8 +62,8 @@ func (cfg *Config) bind() (*Build, error) {
return nil, errors.Wrap(err, "loading failed")
}

imports := buildImports(namedTypes, cfg.execDir)
cfg.bindTypes(imports, namedTypes, cfg.execDir, prog)
imports := buildImports(namedTypes, cfg.Exec.Dir())
cfg.bindTypes(imports, namedTypes, cfg.Exec.Dir(), prog)

objects, err := cfg.buildObjects(namedTypes, prog, imports)
if err != nil {
Expand All @@ -76,7 +76,7 @@ func (cfg *Config) bind() (*Build, error) {
}

b := &Build{
PackageName: cfg.ExecPackageName,
PackageName: cfg.Exec.Package,
Objects: objects,
Interfaces: cfg.buildInterfaces(namedTypes, prog),
Inputs: inputs,
Expand Down
109 changes: 19 additions & 90 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,95 +17,38 @@ import (
"golang.org/x/tools/imports"
)

type Config struct {
SchemaFilename string `yaml:"schema,omitempty"`
SchemaStr string `yaml:"-"`
Typemap TypeMap `yaml:"models,omitempty"`

schema *schema.Schema `yaml:"-"`

ExecFilename string `yaml:"output,omitempty"`
ExecPackageName string `yaml:"package,omitempty"`
execPackagePath string `yaml:"-"`
execDir string `yaml:"-"`

ModelFilename string `yaml:"modeloutput,omitempty"`
ModelPackageName string `yaml:"modelpackage,omitempty"`
modelPackagePath string `yaml:"-"`
modelDir string `yaml:"-"`
}

func (cfg *Config) Check() error {
err := cfg.Typemap.Check()
if err != nil {
return fmt.Errorf("config: %s", err.Error())
}
return nil
}

type TypeMap map[string]TypeMapEntry

func (tm TypeMap) Exists(typeName string) bool {
return tm.Get(typeName) != nil
}

func (tm TypeMap) Get(typeName string) *TypeMapEntry {
entry, ok := tm[typeName]
if !ok {
return nil
}
return &entry
}

func (tm TypeMap) Check() error {
for typeName, entry := range tm {
if entry.Model == "" {
return fmt.Errorf("model %s: entityPath is not defined", typeName)
}
}
return nil
}

type TypeMapEntry struct {
Model string `yaml:"model"`
Fields map[string]TypeMapField `yaml:"fields,omitempty"`
}

type TypeMapField struct {
}

func Generate(cfg Config) error {
if err := cfg.normalize(); err != nil {
return err
}

_ = syscall.Unlink(cfg.ExecFilename)
_ = syscall.Unlink(cfg.ModelFilename)
_ = syscall.Unlink(cfg.Exec.Filename)
_ = syscall.Unlink(cfg.Model.Filename)

modelsBuild, err := cfg.models()
if err != nil {
return errors.Wrap(err, "model plan failed")
}
if len(modelsBuild.Models) > 0 || len(modelsBuild.Enums) > 0 {
modelsBuild.PackageName = cfg.ModelPackageName
modelsBuild.PackageName = cfg.Model.Package
var buf *bytes.Buffer
buf, err = templates.Run("models.gotpl", modelsBuild)
if err != nil {
return errors.Wrap(err, "model generation failed")
}

if err = write(cfg.ModelFilename, buf.Bytes()); err != nil {
if err = write(cfg.Model.Filename, buf.Bytes()); err != nil {
return err
}
for _, model := range modelsBuild.Models {
cfg.Typemap[model.GQLType] = TypeMapEntry{
Model: cfg.modelPackagePath + "." + model.GoType,
cfg.Models[model.GQLType] = TypeMapEntry{
Model: cfg.Model.ImportPath() + "." + model.GoType,
}
}

for _, enum := range modelsBuild.Enums {
cfg.Typemap[enum.GQLType] = TypeMapEntry{
Model: cfg.modelPackagePath + "." + enum.GoType,
cfg.Models[enum.GQLType] = TypeMapEntry{
Model: cfg.Model.ImportPath() + "." + enum.GoType,
}
}
}
Expand All @@ -115,15 +58,15 @@ func Generate(cfg Config) error {
return errors.Wrap(err, "exec plan failed")
}
build.SchemaRaw = cfg.SchemaStr
build.PackageName = cfg.ExecPackageName
build.PackageName = cfg.Exec.Package

var buf *bytes.Buffer
buf, err = templates.Run("generated.gotpl", build)
if err != nil {
return errors.Wrap(err, "exec codegen failed")
}

if err = write(cfg.ExecFilename, buf.Bytes()); err != nil {
if err = write(cfg.Exec.Filename, buf.Bytes()); err != nil {
return err
}

Expand All @@ -135,27 +78,13 @@ func Generate(cfg Config) error {
}

func (cfg *Config) normalize() error {
if cfg.ModelFilename == "" {
return errors.New("ModelFilename is required")
}
cfg.ModelFilename = abs(cfg.ModelFilename)
cfg.modelDir = filepath.ToSlash(filepath.Dir(cfg.ModelFilename))
if cfg.ModelPackageName == "" {
cfg.ModelPackageName = filepath.Base(cfg.modelDir)
if err := cfg.Model.normalize(); err != nil {
return errors.Wrap(err, "model")
}
cfg.ModelPackageName = sanitizePackageName(cfg.ModelPackageName)
cfg.modelPackagePath = fullPackageName(cfg.modelDir, cfg.ModelPackageName)

if cfg.ExecFilename == "" {
return errors.New("ModelFilename is required")
}
cfg.ExecFilename = abs(cfg.ExecFilename)
cfg.execDir = filepath.ToSlash(filepath.Dir(cfg.ExecFilename))
if cfg.ExecPackageName == "" {
cfg.ExecPackageName = filepath.Base(cfg.execDir)
if err := cfg.Exec.normalize(); err != nil {
return errors.Wrap(err, "exec")
}
cfg.ExecPackageName = sanitizePackageName(cfg.ExecPackageName)
cfg.execPackagePath = fullPackageName(cfg.execDir, cfg.ExecPackageName)

builtins := TypeMap{
"__Directive": {Model: "github.com/vektah/gqlgen/neelance/introspection.Directive"},
Expand All @@ -173,12 +102,12 @@ func (cfg *Config) normalize() error {
"Map": {Model: "github.com/vektah/gqlgen/graphql.Map"},
}

if cfg.Typemap == nil {
cfg.Typemap = TypeMap{}
if cfg.Models == nil {
cfg.Models = TypeMap{}
}
for typeName, entry := range builtins {
if !cfg.Typemap.Exists(typeName) {
cfg.Typemap[typeName] = entry
if !cfg.Models.Exists(typeName) {
cfg.Models[typeName] = entry
}
}

Expand All @@ -200,7 +129,7 @@ func abs(path string) string {
return filepath.ToSlash(absPath)
}

func fullPackageName(dir string, pkgName string) string {
func importPath(dir string, pkgName string) string {
fullPkgName := filepath.Join(filepath.Dir(dir), pkgName)

for _, gopath := range filepath.SplitList(build.Default.GOPATH) {
Expand Down
4 changes: 2 additions & 2 deletions codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Test_fullPackageName(t *testing.T) {
t.Run("gopath longer than package name", func(t *testing.T) {
build.Default.GOPATH = "/a/src/xxxxxxxxxxxxxxxxxxxxxxxx:/b/src/y"
var got string
ok := assert.NotPanics(t, func() { got = fullPackageName("/b/src/y/foo/bar", "bar") })
ok := assert.NotPanics(t, func() { got = importPath("/b/src/y/foo/bar", "bar") })
if ok {
assert.Equal(t, "/b/src/y/foo/bar", got)
}
Expand All @@ -23,7 +23,7 @@ func Test_fullPackageName(t *testing.T) {
build.Default.GOPATH = "/a/src/x:/b/src/y"

var got string
ok := assert.NotPanics(t, func() { got = fullPackageName("/a/src/x/foo/bar", "bar") })
ok := assert.NotPanics(t, func() { got = importPath("/a/src/x/foo/bar", "bar") })
if ok {
assert.Equal(t, "/a/src/x/foo/bar", got)
}
Expand Down
76 changes: 76 additions & 0 deletions codegen/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package codegen

import (
"fmt"
"path/filepath"

"github.com/pkg/errors"
"github.com/vektah/gqlgen/neelance/schema"
)

type Config struct {
SchemaFilename string `yaml:"schema,omitempty"`
SchemaStr string `yaml:"-"`
Exec PackageConfig `yaml:"exec"`
Model PackageConfig `yaml:"model"`
Models TypeMap `yaml:"models,omitempty"`

schema *schema.Schema `yaml:"-"`
}

type PackageConfig struct {
Filename string `yaml:"filename,omitempty"`
Package string `yaml:"package,omitempty"`
}

type TypeMapEntry struct {
Model string `yaml:"model"`
Fields map[string]TypeMapField `yaml:"fields,omitempty"`
}

type TypeMapField struct {
}

func (c *PackageConfig) normalize() error {
if c.Filename == "" {
return errors.New("Filename is required")
}
c.Filename = abs(c.Filename)
if c.Package == "" {
c.Package = filepath.Base(c.Dir())
}
c.Package = sanitizePackageName(c.Package)
return nil
}

func (c *PackageConfig) ImportPath() string {
return importPath(c.Dir(), c.Package)
}

func (c *PackageConfig) Dir() string {
return filepath.ToSlash(filepath.Dir(c.Filename))
}

func (cfg *Config) Check() error {
err := cfg.Models.Check()
if err != nil {
return fmt.Errorf("config: %s", err.Error())
}
return nil
}

type TypeMap map[string]TypeMapEntry

func (tm TypeMap) Exists(typeName string) bool {
_, ok := tm[typeName]
return ok
}

func (tm TypeMap) Check() error {
for typeName, entry := range tm {
if entry.Model == "" {
return fmt.Errorf("model %s: entityPath is not defined", typeName)
}
}
return nil
}
8 changes: 4 additions & 4 deletions codegen/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func TestShapes(t *testing.T) {

func generate(name string, schema string, typemap ...TypeMap) error {
cfg := Config{
SchemaStr: schema,
ExecFilename: "testdata/gen/" + name + "/exec.go",
ModelFilename: "testdata/gen/" + name + "/model.go",
SchemaStr: schema,
Exec: PackageConfig{Filename: "testdata/gen/" + name + "/exec.go"},
Model: PackageConfig{Filename: "testdata/gen/" + name + "/model.go"},
}
if len(typemap) > 0 {
cfg.Typemap = typemap[0]
cfg.Models = typemap[0]
}
err := Generate(cfg)
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion codegen/type_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (cfg *Config) buildNamedTypes() NamedTypes {
t := namedTypeFromSchema(schemaType)

userType := ""
if userEntry := cfg.Typemap.Get(t.GQLType); userEntry != nil {
if userEntry, ok := cfg.Models[t.GQLType]; ok {
userType = userEntry.Model
}
t.IsUserDefined = userType != ""
Expand Down
17 changes: 8 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ func main() {
config.SchemaFilename = *schemaFilename
}
if *models != "" {
config.ModelFilename = *models
config.Model.Filename = *models
}
if *output != "" {
config.ExecFilename = *output
config.Exec.Filename = *output
}
if *packageName != "" {
config.ExecPackageName = *packageName
config.Exec.Package = *packageName
}
if *modelPackageName != "" {
config.ModelPackageName = *modelPackageName
config.Model.Package = *modelPackageName
}
if *typemap != "" {
config.Typemap = loadModelMap()
config.Models = loadModelMap()
emitYamlGuidance = true
}

Expand All @@ -75,7 +75,7 @@ func main() {
os.Exit(1)
}

fmt.Fprintf(os.Stderr, "you should use .gqlgen.yml with below content.\n\n%s\n", string(b))
fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: we are moving away from the json typemap, instead create a .gqlgen.yml with the following content:\n\n%s\n", string(b))
}

err = codegen.Generate(*config)
Expand All @@ -86,11 +86,10 @@ func main() {
}

func loadConfig() *codegen.Config {

config := &codegen.Config{
SchemaFilename: "schema.graphql",
ModelFilename: "models_gen.go",
ExecFilename: "generated.go",
Model: codegen.PackageConfig{Filename: "models_gen.go"},
Exec: codegen.PackageConfig{Filename: "generated.go"},
}

b, err := ioutil.ReadFile(*configFilename)
Expand Down
2 changes: 1 addition & 1 deletion test/resolvers_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate gorunpkg github.com/vektah/gqlgen
//go:generate gorunpkg github.com/vektah/gqlgen -models models/generated.go

package test

Expand Down

0 comments on commit cb859cd

Please sign in to comment.