From 9242549f4a0a5fa482adbe6f55c69e61d16236f9 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Fri, 8 May 2020 20:30:44 +0300 Subject: [PATCH] entc: validate package identifier before running codegen (#478) Fixed #474 --- entc/entc.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/entc/entc.go b/entc/entc.go index 9e1760dbc..727a5a771 100644 --- a/entc/entc.go +++ b/entc/entc.go @@ -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