forked from ent/contrib
/
mutate.go
124 lines (114 loc) · 3.37 KB
/
mutate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2019-present Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package schemast
import (
"go/ast"
"go/token"
"entgo.io/ent"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
)
// Mutator changes a Context.
type Mutator interface {
Mutate(ctx *Context) error
}
// Mutate applies a sequence of mutations to a Context
func Mutate(ctx *Context, mutations ...Mutator) error {
for _, mut := range mutations {
if err := mut.Mutate(ctx); err != nil {
return err
}
}
return nil
}
// UpsertSchema implements Mutator. UpsertSchema will add to the Context the type named Name if not present and rewrite
// the type's Fields and Edges methods to return the desired fields and edges.
type UpsertSchema struct {
Name string
Fields []ent.Field
Edges []ent.Edge
Indexes []ent.Index
Annotations []schema.Annotation
}
// Mutate applies the UpsertSchema mutation to the Context.
func (u *UpsertSchema) Mutate(ctx *Context) error {
if !ctx.HasType(u.Name) {
if err := ctx.AddType(u.Name); err != nil {
return err
}
}
if err := resetMethods(ctx, u.Name); err != nil {
return err
}
for _, fld := range u.Fields {
if err := ctx.AppendField(u.Name, fld.Descriptor()); err != nil {
return err
}
if fld.Descriptor().Info.Type == field.TypeUUID {
ctx.appendImport(u.Name, "github.com/google/uuid")
}
// Append any imported struct for JSON fields
if fld.Descriptor().Info.Type == field.TypeJSON {
if fld.Descriptor().Info.RType != nil && fld.Descriptor().Info.RType.PkgPath != "" {
ctx.appendImport(u.Name, fld.Descriptor().Info.RType.PkgPath)
}
}
}
for _, edg := range u.Edges {
if err := ctx.AppendEdge(u.Name, edg.Descriptor()); err != nil {
return err
}
}
for _, annot := range u.Annotations {
if err := ctx.AppendTypeAnnotation(u.Name, annot); err != nil {
return err
}
}
for _, idx := range u.Indexes {
if err := ctx.AppendIndex(u.Name, idx); err != nil {
return err
}
}
return nil
}
func resetMethods(ctx *Context, typeName string) error {
for _, m := range []string{"Fields", "Edges", "Annotations", "Indexes"} {
if _, ok := ctx.lookupMethod(typeName, m); !ok {
continue
}
stmt, err := ctx.returnStmt(typeName, m)
if err != nil {
return err
}
stmt.Results = []ast.Expr{ast.NewIdent("nil")}
}
return nil
}
func (c *Context) appendReturnItem(k kind, typeName string, item ast.Expr) error {
if _, ok := c.lookupMethod(typeName, k.methodName); !ok {
if err := c.appendMethod(typeName, k.methodName, k.ifaceSelector); err != nil {
return err
}
}
stmt, err := c.returnStmt(typeName, k.methodName)
if err != nil {
return err
}
return appendToReturn(stmt, k.ifaceSelector, item)
}
func (c *Context) appendImport(typeName, pkgPath string) {
if f, ok := c.newTypes[typeName]; ok {
f.Imports = append(f.Imports, &ast.ImportSpec{Path: &ast.BasicLit{Value: pkgPath, Kind: token.STRING}})
}
}