-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatchable_values.go
More file actions
123 lines (97 loc) · 2.65 KB
/
patchable_values.go
File metadata and controls
123 lines (97 loc) · 2.65 KB
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
package patchablevalues
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"strings"
"github.com/tidwall/gjson"
"github.com/deckhouse/deckhouse/pkg/log"
service "github.com/deckhouse/module-sdk/pkg"
"github.com/deckhouse/module-sdk/pkg/utils"
)
var _ service.PatchableValuesCollector = (*PatchableValues)(nil)
type PatchableValues struct {
values *gjson.Result
patchOperations []*utils.ValuesPatchOperation
}
func NewPatchableValues(values map[string]any) (*PatchableValues, error) {
data, err := json.Marshal(values)
if err != nil {
return nil, err
}
res := gjson.ParseBytes(data)
return &PatchableValues{values: &res}, nil
}
// Get value from patchable. It could be null value
func (p *PatchableValues) Get(path string) gjson.Result {
return p.values.Get(path)
}
// GetOk returns value and `exists` flag
func (p *PatchableValues) GetOk(path string) (gjson.Result, bool) {
v := p.values.Get(path)
if v.Exists() {
return v, true
}
return v, false
}
// GetRaw get empty interface
func (p *PatchableValues) GetRaw(path string) any {
return p.values.Get(path).Value()
}
// Exists checks whether a path exists
func (p *PatchableValues) Exists(path string) bool {
return p.values.Get(path).Exists()
}
// ArrayCount counts the number of elements in a JSON array at a path
func (p *PatchableValues) ArrayCount(path string) (int, error) {
v := p.values.Get(path)
if !v.IsArray() {
return 0, fmt.Errorf("value at %q path is not an array", path)
}
return len(v.Array()), nil
}
func (p *PatchableValues) Set(path string, value any) {
data, err := json.Marshal(value)
if err != nil {
// The struct returned from a Go hook expected to be marshalable in all cases.
// TODO(nabokihms): return a meaningful error.
log.Error("patch path",
slog.String("path", path),
log.Err(err))
return
}
op := &utils.ValuesPatchOperation{
Op: "add",
Path: convertDotFilePathToSlashPath(path),
Value: data,
}
p.patchOperations = append(p.patchOperations, op)
}
func (p *PatchableValues) Remove(path string) {
if !p.Exists(path) {
// return if path not exists
return
}
op := &utils.ValuesPatchOperation{
Op: "remove",
Path: convertDotFilePathToSlashPath(path),
}
p.patchOperations = append(p.patchOperations, op)
}
func (p *PatchableValues) GetPatches() []*utils.ValuesPatchOperation {
return p.patchOperations
}
func (p *PatchableValues) WriteOutput(w io.Writer) error {
if len(p.patchOperations) == 0 {
return nil
}
err := json.NewEncoder(w).Encode(p.patchOperations)
if err != nil {
return err
}
return nil
}
func convertDotFilePathToSlashPath(dotPath string) string {
return strings.ReplaceAll("/"+dotPath, ".", "/")
}