mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc: validate package identifier before running codegen (#478)
Fixed #474
This commit is contained in:
17
entc/entc.go
17
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user