Skip to content

Commit

Permalink
Refactored config check so that it runs after being normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
Elgarni committed Feb 11, 2019
1 parent f7238ec commit 7fcc437
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 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
36 changes: 11 additions & 25 deletions 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 @@ -200,36 +200,18 @@ func (cfg *Config) Check() error {
cfg.Resolver,
}
filesMap := make(map[string]bool)
pkgConfigsByDir := make(map[string][]PackageConfig)
for i, current := range packageConfigList {
if i == 0 {
filesMap[current.Filename] = true
pkgConfigsByDir[current.Dir()] = []PackageConfig{current}
continue
}
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
prevPkgList, inSameDir := pkgConfigsByDir[current.Dir()]
if inSameDir {
for _, previous := range prevPkgList {
if current.Package != previous.Package {
eitherPackageEmpty := previous.Package != "" || current.Package != ""
if eitherPackageEmpty {
if current.Package == filepath.Base(current.Dir()) && previous.Package == "" {
break
}
if previous.Package == filepath.Base(previous.Dir()) && current.Package == "" {
break
}
return fmt.Errorf("filenames %s and %s are in the same directory but have different package definitions", current.Filename, previous.Filename)
}
}
}
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()] = append(pkgConfigsByDir[current.Dir()], current)
pkgConfigsByDir[current.Dir()] = current
}

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

func stripPath(path string) string {
return filepath.Base(path)
}

0 comments on commit 7fcc437

Please sign in to comment.