-
Notifications
You must be signed in to change notification settings - Fork 329
/
setting_marshal.go
81 lines (67 loc) · 1.67 KB
/
setting_marshal.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
package store
import (
"context"
"github.com/cortezaproject/corteza/server/pkg/envoy/resource"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/types"
)
var (
gSettings map[string]bool
)
func NewSettingFromResource(res *resource.Setting, cfg *EncoderConfig) resourceState {
return &setting{
cfg: mergeConfig(cfg, res.Config()),
res: res,
st: &res.Res,
}
}
func (n *setting) Prepare(ctx context.Context, pl *payload) (err error) {
// Init global state
if gSettings == nil {
gSettings = make(map[string]bool)
ss, _, err := store.SearchSettingValues(ctx, pl.s, types.SettingsFilter{})
if err != store.ErrNotFound && err != nil {
return err
}
for _, s := range ss {
gSettings[s.Name] = true
}
}
return nil
}
func (n *setting) Encode(ctx context.Context, pl *payload) (err error) {
res := n.st
us, err := resolveUserstamps(ctx, pl.s, pl.state.ParentResources, n.res.Userstamps())
if err != nil {
return err
}
ts := n.res.Timestamps()
if ts != nil {
if ts.UpdatedAt != nil {
res.UpdatedAt = *ts.UpdatedAt.T
} else {
res.UpdatedAt = *now()
}
} else {
res.UpdatedAt = *now()
}
if us != nil {
if us.OwnedBy != nil {
res.OwnedBy = us.OwnedBy.UserID
}
if us.UpdatedBy != nil {
res.UpdatedBy = us.UpdatedBy.UserID
}
}
if _, exists := gSettings[res.Name]; !exists {
return store.CreateSettingValue(ctx, pl.s, res)
}
// On existing setting, replace/merge right basically overwrites the existing value;
// otherwise, the new value is ignored.
switch n.cfg.OnExisting {
case resource.Replace,
resource.MergeRight:
return store.UpdateSettingValue(ctx, pl.s, res)
}
return nil
}