forked from kubernetes-sigs/structured-merge-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize-pe.go
147 lines (131 loc) · 3.74 KB
/
serialize-pe.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
/*
Copyright 2018 The Kubernetes Authors.
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 fieldpath
import (
"errors"
"fmt"
"strconv"
"sigs.k8s.io/structured-merge-diff/v4/internal/builder"
"sigs.k8s.io/structured-merge-diff/v4/value"
)
var ErrUnknownPathElementType = errors.New("unknown path element type")
const (
// Field indicates that the content of this path element is a field's name
peField byte = 'f'
// Value indicates that the content of this path element is a field's value
peValue byte = 'v'
// Index indicates that the content of this path element is an index in an array
peIndex byte = 'i'
// Key indicates that the content of this path element is a key value map
peKey byte = 'k'
// Separator separates the type of a path element from the contents
peSeparator byte = ':'
)
var (
peFieldSepBytes = []byte{peField, peSeparator}
peValueSepBytes = []byte{peValue, peSeparator}
peIndexSepBytes = []byte{peIndex, peSeparator}
peKeySepBytes = []byte{peKey, peSeparator}
)
// DeserializePathElement parses a serialized path element
func DeserializePathElement(s string) (PathElement, error) {
b := builder.StringToReadOnlyByteSlice(s)
if len(b) < 2 {
return PathElement{}, errors.New("key must be 2 characters long")
}
typeSep0, typeSep1, b := b[0], b[1], b[2:]
if typeSep1 != peSeparator {
return PathElement{}, fmt.Errorf("missing colon: %v", s)
}
switch typeSep0 {
case peFieldSepBytes[0]:
// Slice s rather than convert b, to save on
// allocations.
str := s[2:]
return PathElement{
FieldName: &str,
}, nil
case peValueSepBytes[0]:
v, err := value.FromJSON(b)
if err != nil {
return PathElement{}, err
}
return PathElement{Value: &v}, nil
case peKeySepBytes[0]:
fields, err := value.FieldListFromJSON(b)
if err != nil {
return PathElement{}, err
}
fields.Sort()
return PathElement{Key: &fields}, nil
case peIndexSepBytes[0]:
i, err := strconv.Atoi(s[2:])
if err != nil {
return PathElement{}, err
}
return PathElement{
Index: &i,
}, nil
default:
return PathElement{}, ErrUnknownPathElementType
}
}
// SerializePathElement serializes a path element
func SerializePathElement(pe PathElement) (string, error) {
buf := builder.JSONBuilder{}
err := serializePathElementToWriter(&buf, pe)
return buf.String(), err
}
func serializePathElementToWriter(w *builder.JSONBuilder, pe PathElement) error {
switch {
case pe.FieldName != nil:
if _, err := w.Write(peFieldSepBytes); err != nil {
return err
}
w.WriteString(*pe.FieldName)
case pe.Key != nil:
if _, err := w.Write(peKeySepBytes); err != nil {
return err
}
w.WriteByte('{')
nrKeys := len(*pe.Key)
for i, f := range *pe.Key {
if err := w.WriteJSON(f.Name); err != nil {
return err
}
w.WriteByte(':')
if err := w.WriteJSON(f.Value.Unstructured()); err != nil {
return err
}
if i < nrKeys-1 {
w.WriteByte(',')
}
}
w.WriteByte('}')
case pe.Value != nil:
if _, err := w.Write(peValueSepBytes); err != nil {
return err
}
if err := w.WriteJSON((*pe.Value).Unstructured()); err != nil {
return err
}
case pe.Index != nil:
if _, err := w.Write(peIndexSepBytes); err != nil {
return err
}
w.WriteString(strconv.Itoa(*pe.Index))
default:
return errors.New("invalid PathElement")
}
return nil
}