Skip to content

Commit

Permalink
Merge pull request #542 from Elgarni/add-more-validation-checks-on-ym…
Browse files Browse the repository at this point in the history
…l-config-file

Add more validation checks on .yml config file
  • Loading branch information
vektah authored Feb 18, 2019
2 parents 66120d8 + dc925c4 commit 015d02e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
5 changes: 0 additions & 5 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ var genCmd = cli.Command{
config.SchemaStr[filename] = string(schemaRaw)
}

if err = config.Check(); err != nil {
fmt.Fprintln(os.Stderr, "invalid config format: "+err.Error())
os.Exit(1)
}

err = codegen.Generate(*config)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
Expand Down
5 changes: 0 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ func GenerateGraphServer(config *codegen.Config, serverFilename string) {
config.SchemaStr[filename] = string(schemaRaw)
}

if err := config.Check(); err != nil {
fmt.Fprintln(os.Stderr, "invalid config format: "+err.Error())
os.Exit(1)
}

if err := codegen.Generate(*config); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
Expand Down
4 changes: 4 additions & 0 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codegen

import (
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -19,6 +20,9 @@ func Generate(cfg Config) error {
return err
}

if err := cfg.check(); err != nil {
return fmt.Errorf("invalid config format: " + err.Error())
}
_ = syscall.Unlink(cfg.Exec.Filename)
_ = syscall.Unlink(cfg.Model.Filename)

Expand Down
29 changes: 28 additions & 1 deletion codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *PackageConfig) IsDefined() bool {
return c.Filename != ""
}

func (cfg *Config) Check() error {
func (cfg *Config) check() error {
if err := cfg.Models.Check(); err != nil {
return errors.Wrap(err, "config.models")
}
Expand All @@ -191,6 +191,29 @@ func (cfg *Config) Check() error {
if err := cfg.Resolver.Check(); err != nil {
return errors.Wrap(err, "config.resolver")
}

// check packages names against conflict, if present in the same dir
// and check filenames for uniqueness
packageConfigList := []PackageConfig{
cfg.Model,
cfg.Exec,
cfg.Resolver,
}
filesMap := make(map[string]bool)
pkgConfigsByDir := make(map[string]PackageConfig)
for _, current := range packageConfigList {
_, fileFound := filesMap[current.Filename]
if fileFound {
return fmt.Errorf("filename %s defined more than once", current.Filename)
}
filesMap[current.Filename] = true
previous, inSameDir := pkgConfigsByDir[current.Dir()]
if inSameDir && current.Package != previous.Package {
return fmt.Errorf("filenames %s and %s are in the same directory but have different package definitions", stripPath(current.Filename), stripPath(previous.Filename))
}
pkgConfigsByDir[current.Dir()] = current
}

return nil
}

Expand Down Expand Up @@ -271,3 +294,7 @@ func findCfgInDir(dir string) string {
}
return ""
}

func stripPath(path string) string {
return filepath.Base(path)
}
13 changes: 13 additions & 0 deletions codegen/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,16 @@ func TestReferencedPackages(t *testing.T) {
})

}

func TestConfigCheck(t *testing.T) {
t.Run("invalid config format due to conflicting package names", func(t *testing.T) {
config, err := LoadConfig("testdata/cfg/conflictedPackages.yml")
require.NoError(t, err)

err = config.normalize()
require.NoError(t, err)

err = config.check()
require.EqualError(t, err, "filenames exec.go and models.go are in the same directory but have different package definitions")
})
}
10 changes: 10 additions & 0 deletions codegen/testdata/cfg/conflictedPackages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
schema:
- schema.graphql
exec:
filename: generated/exec.go
package: graphql
model:
filename: generated/models.go
resolver:
filename: generated/resolver.go
type: Resolver

0 comments on commit 015d02e

Please sign in to comment.