mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: support Append<F> for JSON array fields (#2975)
This commit is contained in:
@@ -20,12 +20,16 @@ import (
|
||||
)
|
||||
|
||||
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{}
|
||||
opts := []entc.Option{
|
||||
entc.Dependency(
|
||||
entc.DependencyType(&http.Client{}),
|
||||
),
|
||||
entc.TemplateFiles(
|
||||
"template/debug.tmpl",
|
||||
"template/stringer.tmpl",
|
||||
),
|
||||
}
|
||||
err := entc.Generate("./schema", &gen.Config{
|
||||
// Target: "./gen", If you want generated files placed under custom path
|
||||
Header: `
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
@@ -33,13 +37,11 @@ func main() {
|
||||
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
`,
|
||||
// A usage for custom templates with external functions.
|
||||
Templates: []*gen.Template{
|
||||
// Custom templates can be provided by entc.Extension (see below),
|
||||
// or by setting templates on the gen.Config object.
|
||||
//
|
||||
gen.MustParse(gen.NewTemplate("static").
|
||||
Funcs(template.FuncMap{"title": strings.ToTitle}).
|
||||
ParseFiles("./template/t1.tmpl")), // create template files under ent/template
|
||||
ParseFiles("template/static.tmpl")),
|
||||
},
|
||||
}, opts...)
|
||||
if err != nil {
|
||||
|
||||
@@ -859,6 +859,7 @@ type UserMutation struct {
|
||||
id *int
|
||||
name *string
|
||||
foods *[]string
|
||||
appendfoods []string
|
||||
clearedFields map[string]struct{}
|
||||
tenant *int
|
||||
clearedtenant bool
|
||||
@@ -1043,6 +1044,7 @@ func (m *UserMutation) ResetName() {
|
||||
// SetFoods sets the "foods" field.
|
||||
func (m *UserMutation) SetFoods(s []string) {
|
||||
m.foods = &s
|
||||
m.appendfoods = nil
|
||||
}
|
||||
|
||||
// Foods returns the value of the "foods" field in the mutation.
|
||||
@@ -1071,9 +1073,23 @@ func (m *UserMutation) OldFoods(ctx context.Context) (v []string, err error) {
|
||||
return oldValue.Foods, nil
|
||||
}
|
||||
|
||||
// AppendFoods adds s to the "foods" field.
|
||||
func (m *UserMutation) AppendFoods(s []string) {
|
||||
m.appendfoods = append(m.appendfoods, s...)
|
||||
}
|
||||
|
||||
// AppendedFoods returns the list of values that were appended to the "foods" field in this mutation.
|
||||
func (m *UserMutation) AppendedFoods() ([]string, bool) {
|
||||
if len(m.appendfoods) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
return m.appendfoods, true
|
||||
}
|
||||
|
||||
// ClearFoods clears the value of the "foods" field.
|
||||
func (m *UserMutation) ClearFoods() {
|
||||
m.foods = nil
|
||||
m.appendfoods = nil
|
||||
m.clearedFields[user.FieldFoods] = struct{}{}
|
||||
}
|
||||
|
||||
@@ -1086,6 +1102,7 @@ func (m *UserMutation) FoodsCleared() bool {
|
||||
// ResetFoods resets all changes to the "foods" field.
|
||||
func (m *UserMutation) ResetFoods() {
|
||||
m.foods = nil
|
||||
m.appendfoods = nil
|
||||
delete(m.clearedFields, user.FieldFoods)
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/dialect/sql/sqljson"
|
||||
"entgo.io/ent/examples/privacytenant/ent/group"
|
||||
"entgo.io/ent/examples/privacytenant/ent/predicate"
|
||||
"entgo.io/ent/examples/privacytenant/ent/user"
|
||||
@@ -52,6 +53,12 @@ func (uu *UserUpdate) SetFoods(s []string) *UserUpdate {
|
||||
return uu
|
||||
}
|
||||
|
||||
// AppendFoods appends s to the "foods" field.
|
||||
func (uu *UserUpdate) AppendFoods(s []string) *UserUpdate {
|
||||
uu.mutation.AppendFoods(s)
|
||||
return uu
|
||||
}
|
||||
|
||||
// ClearFoods clears the value of the "foods" field.
|
||||
func (uu *UserUpdate) ClearFoods() *UserUpdate {
|
||||
uu.mutation.ClearFoods()
|
||||
@@ -199,6 +206,11 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
Column: user.FieldFoods,
|
||||
})
|
||||
}
|
||||
if value, ok := uu.mutation.AppendedFoods(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldFoods, value)
|
||||
})
|
||||
}
|
||||
if uu.mutation.FoodsCleared() {
|
||||
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
|
||||
Type: field.TypeJSON,
|
||||
@@ -298,6 +310,12 @@ func (uuo *UserUpdateOne) SetFoods(s []string) *UserUpdateOne {
|
||||
return uuo
|
||||
}
|
||||
|
||||
// AppendFoods appends s to the "foods" field.
|
||||
func (uuo *UserUpdateOne) AppendFoods(s []string) *UserUpdateOne {
|
||||
uuo.mutation.AppendFoods(s)
|
||||
return uuo
|
||||
}
|
||||
|
||||
// ClearFoods clears the value of the "foods" field.
|
||||
func (uuo *UserUpdateOne) ClearFoods() *UserUpdateOne {
|
||||
uuo.mutation.ClearFoods()
|
||||
@@ -475,6 +493,11 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error)
|
||||
Column: user.FieldFoods,
|
||||
})
|
||||
}
|
||||
if value, ok := uuo.mutation.AppendedFoods(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldFoods, value)
|
||||
})
|
||||
}
|
||||
if uuo.mutation.FoodsCleared() {
|
||||
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
|
||||
Type: field.TypeJSON,
|
||||
|
||||
Reference in New Issue
Block a user