cmd/ent: replace entc with ent (#989)

See #981
This commit is contained in:
Ariel Mashraki
2020-11-29 10:23:24 +02:00
committed by GitHub
parent e407098690
commit b77d2d4277
32 changed files with 361 additions and 255 deletions

249
cmd/internal/base/base.go Normal file
View File

@@ -0,0 +1,249 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Package base defines shared basic pieces of the ent command.
package base
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"unicode"
"github.com/facebook/ent/cmd/internal/printer"
"github.com/facebook/ent/entc"
"github.com/facebook/ent/entc/gen"
"github.com/facebook/ent/schema/field"
"github.com/spf13/cobra"
)
// custom implementation for pflag.
type IDType field.Type
// Set implements the Set method of the flag.Value interface.
func (t *IDType) Set(s string) error {
switch s {
case field.TypeInt.String():
*t = IDType(field.TypeInt)
case field.TypeInt64.String():
*t = IDType(field.TypeInt64)
case field.TypeUint.String():
*t = IDType(field.TypeUint)
case field.TypeUint64.String():
*t = IDType(field.TypeUint64)
case field.TypeString.String():
*t = IDType(field.TypeString)
default:
return fmt.Errorf("invalid type %q", s)
}
return nil
}
// Type returns the type representation of the id option for help command.
func (IDType) Type() string {
return fmt.Sprintf("%v", []field.Type{
field.TypeInt,
field.TypeInt64,
field.TypeUint,
field.TypeUint64,
field.TypeString,
})
}
// String returns the default value for the help command.
func (IDType) String() string {
return field.TypeInt.String()
}
// InitCmd returns the init command for ent/c packages.
func InitCmd() *cobra.Command {
var target string
cmd := &cobra.Command{
Use: "init [flags] [schemas]",
Short: "initialize an environment with zero or more schemas",
Example: examples(
"ent init Example",
"ent init --target entv1/schema User Group",
),
Args: func(_ *cobra.Command, names []string) error {
for _, name := range names {
if !unicode.IsUpper(rune(name[0])) {
return errors.New("schema names must begin with uppercase")
}
}
return nil
},
Run: func(cmd *cobra.Command, names []string) {
if err := initEnv(target, names); err != nil {
log.Fatalln(err)
}
},
}
cmd.Flags().StringVar(&target, "target", defaultSchema, "target directory for schemas")
return cmd
}
// DescribeCmd returns the describe command for ent/c packages.
func DescribeCmd() *cobra.Command {
return &cobra.Command{
Use: "describe [flags] path",
Short: "printer a description of the graph schema",
Example: examples(
"ent describe ./ent/schema",
"ent describe github.com/a8m/x",
),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, path []string) {
graph, err := entc.LoadGraph(path[0], &gen.Config{})
if err != nil {
log.Fatalln(err)
}
printer.Fprint(os.Stdout, graph)
},
}
}
// GenerateCmd returns the generate command for ent/c packages.
func GenerateCmd(postRun ...func(*gen.Config)) *cobra.Command {
var (
cfg gen.Config
storage string
features []string
templates []string
idtype = IDType(field.TypeInt)
cmd = &cobra.Command{
Use: "generate [flags] path",
Short: "generate go code for the schema directory",
Example: examples(
"ent generate ./ent/schema",
"ent generate github.com/a8m/x",
),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, path []string) {
opts := []entc.Option{
entc.Storage(storage),
entc.FeatureNames(features...),
}
for _, tmpl := range templates {
typ := "dir"
if parts := strings.SplitN(tmpl, "=", 2); len(parts) > 1 {
typ, tmpl = parts[0], parts[1]
}
switch typ {
case "dir":
opts = append(opts, entc.TemplateDir(tmpl))
case "file":
opts = append(opts, entc.TemplateFiles(tmpl))
case "glob":
opts = append(opts, entc.TemplateGlob(tmpl))
default:
log.Fatalln("unsupported template type", typ)
}
}
// If the target directory is not inferred from
// the schema path, resolve its package path.
if cfg.Target != "" {
pkgPath, err := PkgPath(DefaultConfig, cfg.Target)
if err != nil {
log.Fatalln(err)
}
cfg.Package = pkgPath
}
cfg.IDType = &field.TypeInfo{Type: field.Type(idtype)}
if err := entc.Generate(path[0], &cfg, opts...); err != nil {
log.Fatalln(err)
}
for _, fn := range postRun {
fn(&cfg)
}
},
}
)
cmd.Flags().Var(&idtype, "idtype", "type of the id field")
cmd.Flags().StringVar(&storage, "storage", "sql", "storage driver to support in codegen")
cmd.Flags().StringVar(&cfg.Header, "header", "", "override codegen header")
cmd.Flags().StringVar(&cfg.Target, "target", "", "target directory for codegen")
cmd.Flags().StringSliceVarP(&features, "feature", "", nil, "extend codegen with additional features")
cmd.Flags().StringSliceVarP(&templates, "template", "", nil, "external templates to execute")
return cmd
}
// initEnv initialize an environment for ent codegen.
func initEnv(target string, names []string) error {
if err := createDir(target); err != nil {
return err
}
for _, name := range names {
b := bytes.NewBuffer(nil)
if err := tmpl.Execute(b, name); err != nil {
log.Fatalln(err)
}
target := filepath.Join(target, strings.ToLower(name+".go"))
if err := ioutil.WriteFile(target, b.Bytes(), 0644); err != nil {
log.Fatalln(err)
}
}
return nil
}
func createDir(target string) error {
_, err := os.Stat(target)
if err == nil || !os.IsNotExist(err) {
return err
}
if err := os.MkdirAll(target, os.ModePerm); err != nil {
return fmt.Errorf("creating schema directory: %w", err)
}
if target != defaultSchema {
return nil
}
if err := ioutil.WriteFile("ent/generate.go", []byte(genFile), 0644); err != nil {
return fmt.Errorf("creating generate.go file: %w", err)
}
return nil
}
// schema template for the "init" command.
var tmpl = template.Must(template.New("schema").
Parse(`package schema
import "github.com/facebook/ent"
// {{ . }} holds the schema definition for the {{ . }} entity.
type {{ . }} struct {
ent.Schema
}
// Fields of the {{ . }}.
func ({{ . }}) Fields() []ent.Field {
return nil
}
// Edges of the {{ . }}.
func ({{ . }}) Edges() []ent.Edge {
return nil
}
`))
const (
// default schema package path.
defaultSchema = "ent/schema"
// ent/generate.go file used for "go generate" command.
genFile = "package ent\n\n//go:generate go run github.com/facebook/ent/cmd/ent generate ./schema\n"
)
// examples formats the given examples to the cli.
func examples(ex ...string) string {
for i := range ex {
ex[i] = " " + ex[i] // indent each row with 2 spaces.
}
return strings.Join(ex, "\n")
}

View File

@@ -0,0 +1,55 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package base
import (
"fmt"
"os"
"path"
"path/filepath"
"golang.org/x/tools/go/packages"
)
// DefaultConfig for loading Go base.
var DefaultConfig = &packages.Config{Mode: packages.NeedName}
// PkgPath returns the Go package name for given target path.
// Even if the existing path is not exist yet in the filesystem.
//
// If base.Config is nil, DefaultConfig will be used to load base.
func PkgPath(config *packages.Config, target string) (string, error) {
if config == nil {
config = DefaultConfig
}
pathCheck, err := filepath.Abs(target)
if err != nil {
return "", err
}
var parts []string
if _, err := os.Stat(pathCheck); os.IsNotExist(err) {
parts = append(parts, filepath.Base(pathCheck))
pathCheck = filepath.Dir(pathCheck)
}
// Try maximum 2 directories above the given
// target to find the root package or module.
for i := 0; i < 2; i++ {
pkgs, err := packages.Load(config, pathCheck)
if err != nil {
return "", fmt.Errorf("load package info: %v", err)
}
if len(pkgs) == 0 || len(pkgs[0].Errors) != 0 {
parts = append(parts, filepath.Base(pathCheck))
pathCheck = filepath.Dir(pathCheck)
continue
}
pkgPath := pkgs[0].PkgPath
for j := len(parts) - 1; j >= 0; j-- {
pkgPath = path.Join(pkgPath, parts[j])
}
return pkgPath, nil
}
return "", fmt.Errorf("root package or module was not found for: %s", target)
}

View File

@@ -0,0 +1,49 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package base
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/packages/packagestest"
)
func TestPkgPath(t *testing.T) { packagestest.TestAll(t, testPkgPath) }
func testPkgPath(t *testing.T, x packagestest.Exporter) {
e := packagestest.Export(t, x, []packagestest.Module{
{
Name: "golang.org/x",
Files: map[string]interface{}{
"x.go": "package x",
"y/y.go": "package y",
},
},
})
defer e.Cleanup()
e.Config.Dir = filepath.Dir(e.File("golang.org/x", "x.go"))
target := filepath.Join(e.Config.Dir, "ent")
pkgPath, err := PkgPath(e.Config, target)
require.NoError(t, err)
require.Equal(t, "golang.org/x/ent", pkgPath)
e.Config.Dir = filepath.Dir(e.File("golang.org/x", "y/y.go"))
target = filepath.Join(e.Config.Dir, "ent")
pkgPath, err = PkgPath(e.Config, target)
require.NoError(t, err)
require.Equal(t, "golang.org/x/y/ent", pkgPath)
target = filepath.Join(e.Config.Dir, "z/ent")
pkgPath, err = PkgPath(e.Config, target)
require.NoError(t, err)
require.Equal(t, "golang.org/x/y/z/ent", pkgPath)
target = filepath.Join(e.Config.Dir, "z/e/n/t")
pkgPath, err = PkgPath(e.Config, target)
require.Error(t, err)
require.Empty(t, pkgPath)
}

View File

@@ -0,0 +1,83 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package printer
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"github.com/facebook/ent/entc/gen"
"github.com/olekukonko/tablewriter"
)
// A Config controls the output of Fprint.
type Config struct {
io.Writer
}
// Print prints a table description of the graph to the given writer.
func (p Config) Print(g *gen.Graph) {
for _, n := range g.Nodes {
p.node(n)
}
}
// Fprint executes "pretty-printer" on the given writer.
func Fprint(w io.Writer, g *gen.Graph) {
Config{Writer: w}.Print(g)
}
// node returns description of a type. The format of the description is:
//
// Type:
// <Fields Table>
//
// <Edges Table>
//
func (p Config) node(t *gen.Type) {
var (
b strings.Builder
table = tablewriter.NewWriter(&b)
header = []string{"Field", "Type", "Unique", "Optional", "Nillable", "Default", "UpdateDefault", "Immutable", "StructTag", "Validators"}
)
b.WriteString(t.Name + ":\n")
table.SetAutoFormatHeaders(false)
table.SetHeader(header)
for _, f := range append([]*gen.Field{t.ID}, t.Fields...) {
v := reflect.ValueOf(*f)
row := make([]string, len(header))
for i := range row {
field := v.FieldByNameFunc(func(name string) bool {
// The first field is mapped from "Name" to "Field".
return name == "Name" && i == 0 || name == header[i]
})
row[i] = fmt.Sprint(field.Interface())
}
table.Append(row)
}
table.Render()
table = tablewriter.NewWriter(&b)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Edge", "Type", "Inverse", "BackRef", "Relation", "Unique", "Optional"})
for _, e := range t.Edges {
table.Append([]string{
e.Name,
e.Type.Name,
strconv.FormatBool(e.IsInverse()),
e.Inverse,
e.Rel.Type.String(),
strconv.FormatBool(e.Unique),
strconv.FormatBool(e.Optional),
})
}
if table.NumLines() > 0 {
table.Render()
}
io.WriteString(p, strings.ReplaceAll(b.String(), "\n", "\n\t")+"\n")
}

View File

@@ -0,0 +1,177 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package printer
import (
"strings"
"testing"
"github.com/facebook/ent/entc/gen"
"github.com/facebook/ent/schema/field"
"github.com/stretchr/testify/assert"
)
func TestPrinter_Print(t *testing.T) {
tests := []struct {
input *gen.Graph
out string
}{
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}, Validators: 1},
{Name: "age", Type: &field.TypeInfo{Type: field.TypeInt}, Nillable: true},
{Name: "created_at", Type: &field.TypeInfo{Type: field.TypeTime}, Nillable: true, Immutable: true},
},
},
},
},
out: `
User:
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 1 |
| age | int | false | false | true | false | false | false | | 0 |
| created_at | time.Time | false | false | true | false | false | true | | 0 |
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
`,
},
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Edges: []*gen.Edge{
{Name: "groups", Type: &gen.Type{Name: "Group"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
{Name: "spouse", Type: &gen.Type{Name: "User"}, Unique: true, Rel: gen.Relation{Type: gen.O2O}},
},
},
},
},
out: `
User:
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+--------+-------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+--------+-------+---------+---------+----------+--------+----------+
| groups | Group | false | | M2M | false | true |
| spouse | User | false | | O2O | true | false |
+--------+-------+---------+---------+----------+--------+----------+
`,
},
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}, Validators: 1},
{Name: "age", Type: &field.TypeInfo{Type: field.TypeInt}, Nillable: true},
},
Edges: []*gen.Edge{
{Name: "groups", Type: &gen.Type{Name: "Group"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
{Name: "spouse", Type: &gen.Type{Name: "User"}, Unique: true, Rel: gen.Relation{Type: gen.O2O}},
},
},
},
},
out: `
User:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 1 |
| age | int | false | false | true | false | false | false | | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+--------+-------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+--------+-------+---------+---------+----------+--------+----------+
| groups | Group | false | | M2M | false | true |
| spouse | User | false | | O2O | true | false |
+--------+-------+---------+---------+----------+--------+----------+
`,
},
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}, Validators: 1},
{Name: "age", Type: &field.TypeInfo{Type: field.TypeInt}, Nillable: true},
},
Edges: []*gen.Edge{
{Name: "groups", Type: &gen.Type{Name: "Group"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
{Name: "spouse", Type: &gen.Type{Name: "User"}, Unique: true, Rel: gen.Relation{Type: gen.O2O}},
},
},
{
Name: "Group",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}},
},
Edges: []*gen.Edge{
{Name: "users", Type: &gen.Type{Name: "User"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
},
},
},
},
out: `
User:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 1 |
| age | int | false | false | true | false | false | false | | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+--------+-------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+--------+-------+---------+---------+----------+--------+----------+
| groups | Group | false | | M2M | false | true |
| spouse | User | false | | O2O | true | false |
+--------+-------+---------+---------+----------+--------+----------+
Group:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+-------+------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+-------+------+---------+---------+----------+--------+----------+
| users | User | false | | M2M | false | true |
+-------+------+---------+---------+----------+--------+----------+
`,
},
}
for _, tt := range tests {
b := &strings.Builder{}
Fprint(b, tt.input)
assert.Equal(t, tt.out, "\n"+b.String())
}
}