entc: validate package identifier before running codegen (#478)

Fixed #474
This commit is contained in:
Ariel Mashraki
2020-05-08 20:30:44 +03:00
committed by GitHub
parent 9b14556314
commit 9242549f4a

View File

@@ -8,9 +8,11 @@ package entc
import (
"fmt"
"go/token"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/facebookincubator/ent/entc/gen"
@@ -80,9 +82,24 @@ func Generate(schemaPath string, cfg *gen.Config, options ...Option) (err error)
if err != nil {
return err
}
if err := normalizePkg(cfg); err != nil {
return err
}
return graph.Gen()
}
func normalizePkg(c *gen.Config) error {
base := path.Base(c.Package)
if strings.ContainsRune(base, '-') {
base = strings.ReplaceAll(base, "-", "_")
c.Package = path.Join(path.Dir(c.Package), base)
}
if !token.IsIdentifier(base) {
return fmt.Errorf("invalid package identifier: %q", base)
}
return nil
}
// Option allows for managing codegen configuration using functional options.
type Option func(*gen.Config) error