标签:examples ida javascrip main 代码生成 命令 flag div UNC
ent 提供了cli 工具,可以方便我们进行schema 以及代码生成,同时目前提供的cli已经够用了
go get github.com/facebookincubator/ent/cmd/entc
entc init User Pet
基于schema 的定义(字段,索引,边,配置)
entc generate ./ent/schema
说明:
生成的代码包含了crud、migrate、entity、client
使用独立的cli以及生成的代码,可能会和我们的框架不兼容 ,处理方法使用go:generate
参考配置:
package ent
?
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate ./schema
generate go code for the schema directory
?
Usage:
entc generate [flags] path
?
Examples:
entc generate ./ent/schema
entc generate github.com/a8m/x
?
Flags:
--header string override codegen header
-h, --help help for generate
--idtype [int string] type of the id field (default int)
--storage strings list of storage drivers to support (default [sql])
--target string target directory for codegen
--template strings external templates to execute
entc 可以做为一个包在我们的项目中使用
package main
?
import (
"log"
?
"github.com/facebookincubator/ent/entc"
"github.com/facebookincubator/ent/entc/gen"
"github.com/facebookincubator/ent/schema/field"
)
?
func main() {
err := entc.Generate("./schema", &gen.Config{
Header: "// Your Custom Header",
IDType: &field.TypeInfo{Type: field.TypeInt},
})
if err != nil {
log.Fatal("running ent codegen:", err)
}
}
entc describe ./ent/schema
City:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
| id | <nil> | false | false | false | false | false | false | json:"id,omitempty" | 0 |
| name | string | false | false | false | false | false | false | json:"name,omitempty" | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
+---------+--------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+---------+--------+---------+---------+----------+--------+----------+
| streets | Street | false | | O2M | false | true |
+---------+--------+---------+---------+----------+--------+----------+
?
Street:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
| id | <nil> | false | false | false | false | false | false | json:"id,omitempty" | 0 |
| name | string | false | false | false | false | false | false | json:"name,omitempty" | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
+------+------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+------+------+---------+---------+----------+--------+----------+
| city | City | true | streets | M2O | true | true |
+------+------+---------+---------+----------+--------+----------+
https://entgo.io/docs/code-gen/
标签:examples ida javascrip main 代码生成 命令 flag div UNC
原文地址:https://www.cnblogs.com/rongfengliang/p/11675348.html