mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
* entc/gen/template/dialect/sql: cleaning up isSQLConstraintError in generated ent.go to use new sqlgraph IsConstraintError checks. Adding IsFK and IsUniqueness methods to the generated ent.ConstraintError (on SQL dialects) struct to allow users to glean more information about the specific constraint violation. Fixes #1310 * fix indentation in tmpl file regen files rm new methods from generated code * regen
entcpkg example
An example of using entc (ent codegen) as package rather than an executable.
In this example, we have a file named entc.go under the ./ent directory, that holds the
configuration for the codegen:
// +build ignore
package main
import (
"log"
"strings"
"text/template"
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
)
func main() {
// A usage for custom templates with external functions.
// One template is defined in the option below, and the
// second template is provided with the `Templates` field.
opts := []entc.Option{
entc.Funcs(template.FuncMap{"title": strings.ToTitle}),
entc.TemplateFiles("template/static.tmpl"),
}
err := entc.Generate("./schema", &gen.Config{
Header: `
// 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.
// Code generated by entc, DO NOT EDIT.
`,
Templates: []*template.Template{
template.Must(template.New("debug").
Funcs(gen.Funcs).
ParseFiles("template/debug.tmpl")),
},
}, opts...)
if err != nil {
log.Fatal("running ent codegen:", err)
}
}
As you can see, the file is tagged with // +build ignore in order to not include it
in the ent package. In order to run the codegen, run the file itself (using go run)
or run go generate ./ent. The generate.go file holds the go run command:
package ent
//go:generate go run entc.go
The generate.go file is preferred if you have many generate pragmas in your project.