forked from trimmer-io/go-xmp
/
sync.go
274 lines (244 loc) · 6.55 KB
/
sync.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (c) 2017-2018 Alexander Eichhorn
//
// 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.
// Sync across different models by specifying paths to values within each
// model. Paths must begin with a registered namespace short name followed
// by a colon (:). Paths must be the same strings as used in type's XMP tag,
// e.g
//
// XMP Tag XMP Path
// `xmp:"xmpDM:cameraAngle"` xmpDM:cameraAngle
//
// To refer nested structs or arrays, paths may contain multiple segments
// separated by a forward slash (/). Array elements may be addressed using
// square brackets after the XMP name of the array, e.g.
//
// XMP Array
// dc:creator[0] // the first array element in XMP lists and bags
// dc:description[en] // a language entry in XMP alternative arrays
//
// Sync flags
//
// Syncing can be controlled by flags. Flags are binary and can be combined to
// achieve a desired result. Some useful combinations are
//
// S_CREATE|S_REPLACE|S_DELETE
// create dest when not exist, overwrite when exist, clear when exist and
// source is empty
//
// S_CREATE|S_DELETE
// create dest when not exist, clear when exist and source is empty
//
// S_CREATE|S_REPLACE
// create when not exist, overwrite when exist unless source is empty
//
// You may find other combinations helpful as well. When working with lists
// as sync destination, combine the above with S_APPEND or S_UNIQUE to extend
// a list. When S_DELETE is set and the source value is empty, the list will
// be cleared. With S_REPLACE, a list will be replaced by the source value,
// that is, afterwards the list contains a single entry only. Precedence order
// for slices/arrays when multiple flags are set is UNIQUE > APPEND > REPLACE.
//
// Examples
// "xmpDM:cameraAngle" <-> "trim:Shot/Angle"
// "tiff:Artist" <-> "dc:creator"
//
//
package xmp
import (
"fmt"
"strings"
)
type SyncDesc struct {
Source Path
Dest Path
Flags SyncFlags
Convert ConverterFunc
}
type SyncDescList []*SyncDesc
type ConverterFunc func(string) string
type SyncFlags int
const (
CREATE SyncFlags = 1 << iota // create when not exist, nothing when exist or source is empty
REPLACE // replace when dest exist, nothing when missing or source is empty
DELETE // clear dest when source value is empty
APPEND // list-only: append non-empty source value
UNIQUE // list-only: append non-empty unique source value
NOFAIL // don't fail when state+op+flags don't match
DEFAULT = CREATE | REPLACE | DELETE | UNIQUE
MERGE = CREATE | UNIQUE | NOFAIL
EXTEND = CREATE | REPLACE | UNIQUE | NOFAIL
ADD = CREATE | UNIQUE | NOFAIL
)
func ParseSyncFlag(s string) SyncFlags {
switch s {
case "create":
return CREATE
case "replace":
return REPLACE
case "delete":
return DELETE
case "append":
return APPEND
case "unique":
return UNIQUE
case "nofail":
return NOFAIL
case "default":
return DEFAULT
case "merge":
return MERGE
case "extend":
return EXTEND
case "add":
return ADD
default:
return 0
}
}
func ParseSyncFlags(s string) (SyncFlags, error) {
var flags SyncFlags
for _, v := range strings.Split(s, ",") {
f := ParseSyncFlag(strings.ToLower(v))
if f == 0 {
return 0, fmt.Errorf("invalid xmp sync flag '%s'", v)
}
flags |= f
}
return flags, nil
}
func (f *SyncFlags) UnmarshalText(data []byte) error {
if len(data) == 0 {
*f = 0
return nil
}
if flags, err := ParseSyncFlags(string(data)); err != nil {
return err
} else {
*f = flags
}
return nil
}
// Model is optional and exists for performance reasons to avoid lookups when
// a sync destination is a specific model only (useful to implement SyncFromXMP()
// in models)
func (d *Document) SyncMulti(desc SyncDescList, m Model) error {
for _, v := range desc {
if err := d.Sync(v.Source, v.Dest, v.Flags, m, v.Convert); err != nil {
return err
}
}
return nil
}
func (d *Document) Sync(sPath, dPath Path, flags SyncFlags, v Model, f ConverterFunc) error {
// use default flags when zero
if flags == 0 {
flags = DEFAULT
}
// only XMP paths are supported here
if !sPath.IsXmpPath() || !dPath.IsXmpPath() {
return nil
}
sNs, _ := sPath.Namespace(d)
dNs, _ := dPath.Namespace(d)
// skip when either namespace does not exist
if sNs == nil || dNs == nil {
return nil
}
// skip when sPath model does not exist
sModel := d.FindModel(sNs)
if sModel == nil {
return nil
}
dModel := v
if dModel != nil {
// dPath model must match dPath namespace
if !dPath.MatchNamespace(dModel.Namespaces()[0]) {
return nil
}
} else {
dModel = d.FindModel(dNs)
}
// create dPath model
if dModel == nil {
if flags&CREATE == 0 {
return nil
}
dModel = dNs.NewModel()
d.AddModel(dModel)
}
sValue, err := GetModelPath(sModel, sPath)
if err != nil {
if flags&NOFAIL > 0 {
return nil
}
return err
}
dValue, err := GetModelPath(dModel, dPath)
if err != nil {
if flags&NOFAIL > 0 {
return nil
}
return err
}
// skip when equal
if sValue == dValue {
return nil
}
// empty source will only be used with delete flag
if sValue == "" && flags&DELETE == 0 {
return nil
}
// empty destination values require create flag
if dValue == "" && flags&CREATE == 0 {
return nil
}
// existing destination values require replace/delete/append/unique flag
if dValue != "" && flags&(REPLACE|DELETE|APPEND|UNIQUE) == 0 {
return nil
}
// convert the source value if requested
if f != nil {
sValue = f(sValue)
}
if err = SetModelPath(dModel, dPath, sValue, flags); err != nil {
if flags&NOFAIL > 0 {
return nil
}
return err
}
d.SetDirty()
return nil
}
func (d *Document) Merge(b *Document, flags SyncFlags) error {
p, err := b.ListPaths()
if err != nil {
return err
}
// copy namespaces
for n, v := range b.intNsMap {
d.intNsMap[n] = v
}
for n, v := range b.extNsMap {
d.extNsMap[n] = v
}
// copy content
for _, v := range p {
v.Flags = flags
if err := d.SetPath(v); err != nil {
return err
}
}
d.SetDirty()
return nil
}