-
Notifications
You must be signed in to change notification settings - Fork 898
/
snapshot.go
242 lines (228 loc) · 6.44 KB
/
snapshot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// 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 internal
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"entgo.io/ent/entc/gen"
"entgo.io/ent/entc/load"
)
// Snapshot describes the schema snapshot restore.
type Snapshot struct {
Path string // Path to snapshot.
Config *gen.Config // Config of codegen.
}
// Restore restores the generated package from the latest schema snapshot.
// If there is a conflict between upstream and local snapshots, it is merged
// before running the code generation.
func (s *Snapshot) Restore() error {
buf, err := os.ReadFile(s.Path)
if err != nil {
return fmt.Errorf("unable to read snapshot schema %w", err)
}
snap, err := s.parseSnapshot(buf)
if err != nil {
return err
}
s.Config.Schema = snap.Schema
s.Config.Package = snap.Package
s.addFeatures(snap)
graph, err := gen.NewGraph(s.Config, snap.Schemas...)
if err != nil {
return err
}
return graph.Gen()
}
// schemaIdent holds the schema identifier in snapshot file.
const schemaIdent = "const Schema"
// parseSnapshot parses the given buffer and extract the generated snapshot.
// If it encounters a merge-conflict, it will resolve it by merging the relevant
// parts for the codegen.
func (s *Snapshot) parseSnapshot(buf []byte) (*gen.Snapshot, error) {
var (
conflict bool
matches = make([][]byte, 0, 2)
lines = bytes.Split(buf, []byte("\n"))
)
for i := 0; i < len(lines); i++ {
switch line := lines[i]; {
case bytes.HasPrefix(line, []byte(schemaIdent)):
matches = append(matches, line)
case bytes.HasPrefix(line, []byte(conflictMarker)):
conflict = true
}
}
switch n := len(matches); {
case n == 0:
return nil, fmt.Errorf("schema snapshot was not found in %s", s.Path)
case n > 1 && !conflict:
return nil, fmt.Errorf("expect to have exactly 1 snapshot in %s", s.Path)
}
line, err := trim(matches[0])
if err != nil {
return nil, err
}
local := &gen.Snapshot{}
if err := json.Unmarshal(line, &local); err != nil {
return nil, fmt.Errorf("unmarshal snapshot %v: %w", local, err)
}
if !conflict || len(matches) == 1 {
return local, nil
}
// In case of merge-conflict, we merge the 2 schemas.
line, err = trim(matches[1])
if err != nil {
return nil, err
}
other := &gen.Snapshot{}
if err := json.Unmarshal(line, &other); err != nil {
return nil, fmt.Errorf("unmarshal snapshot %v: %w", local, err)
}
merge(local, other)
return local, nil
}
// addFeatures adds the features in the snapshot to the codegen config.
func (s *Snapshot) addFeatures(snap *gen.Snapshot) {
add := make(map[string]gen.Feature)
for _, name := range snap.Features {
for _, feat := range gen.AllFeatures {
if name == feat.Name {
add[name] = feat
}
}
}
for _, feat := range s.Config.Features {
delete(add, feat.Name)
}
for _, feat := range add {
s.Config.Features = append(s.Config.Features, feat)
}
}
// merge the "other"/"upstream" snapshot to the "local" version.
func merge(local, other *gen.Snapshot) {
if local.Schema == "" {
local.Schema = other.Schema
}
if local.Package == "" {
local.Package = other.Package
}
locals := make(map[string]*load.Schema, len(local.Schemas))
for _, schema := range local.Schemas {
locals[schema.Name] = schema
}
// Merge "other" schemas.
for _, schema := range other.Schemas {
switch match, ok := locals[schema.Name]; {
case !ok:
local.Schemas = append(local.Schemas, schema)
case ok:
mergeSchema(match, schema)
}
}
// Merge codegen features.
features := make(map[string]struct{}, len(local.Features))
for _, feat := range local.Features {
features[feat] = struct{}{}
}
for _, feat := range other.Features {
if _, ok := features[feat]; !ok {
local.Features = append(local.Features, feat)
}
}
}
// mergeSchema merges to "local" the additional information in
// the "other" schema, that may be necessary for code-generation.
func mergeSchema(local, other *load.Schema) {
if local.Config.Table == "" {
local.Config.Table = other.Config.Table
}
if local.Annotations == nil && other.Annotations != nil {
local.Annotations = make(map[string]interface{})
}
for ant := range other.Annotations {
if _, ok := local.Annotations[ant]; !ok {
local.Annotations[ant] = other.Annotations[ant]
}
}
fields := make(map[string]*load.Field, len(local.Fields))
for _, f := range local.Fields {
fields[f.Name] = f
}
for _, f := range other.Fields {
switch match, ok := fields[f.Name]; {
case !ok:
local.Fields = append(local.Fields, f)
case ok:
mergeField(match, f)
}
}
edges := make(map[string]*load.Edge, len(local.Edges))
for _, e := range local.Edges {
edges[e.Name] = e
}
for _, e := range other.Edges {
switch match, ok := edges[e.Name]; {
case !ok:
local.Edges = append(local.Edges, e)
case ok:
mergeEdge(match, e)
}
}
}
// mergeField merges to "local" the additional information in
// the "other" field, that may be necessary for code-generation.
func mergeField(local, other *load.Field) {
if local.Annotations == nil && other.Annotations != nil {
local.Annotations = make(map[string]interface{})
}
for ant := range other.Annotations {
if _, ok := local.Annotations[ant]; !ok {
local.Annotations[ant] = other.Annotations[ant]
}
}
if !local.Immutable && other.Immutable {
local.Immutable = other.Immutable
}
}
// mergeEdge merges to "local" the additional information in
// the "other" edge, that may be necessary for code-generation.
func mergeEdge(local, other *load.Edge) {
if local.Annotations == nil && other.Annotations != nil {
local.Annotations = make(map[string]interface{})
}
for ant := range other.Annotations {
if _, ok := local.Annotations[ant]; !ok {
local.Annotations[ant] = other.Annotations[ant]
}
}
}
// IsBuildError reports if the given error is an error from the Go command (e.g. syntax error).
func IsBuildError(err error) bool {
if strings.HasPrefix(err.Error(), "entc/load: #") {
return true
}
for _, s := range []string{
"syntax error",
"previous declaration",
"invalid character",
"could not import",
"found '<<'",
} {
if strings.Contains(err.Error(), s) {
return true
}
}
return false
}
func trim(line []byte) ([]byte, error) {
start := bytes.IndexByte(line, '`')
end := bytes.LastIndexByte(line, '`')
if start == -1 || start >= end {
return nil, fmt.Errorf("unexpected snapshot line %s", line)
}
return line[start+1 : end], nil
}