Skip to content

Commit 26b5747

Browse files
committed
wip: restore API
1 parent 4f4fbff commit 26b5747

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

Diff for: fieldpath/serialize-pe.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,26 @@ func serializePathElementToWriter(w *builder.JSONBuilder, pe PathElement) error
113113
if _, err := w.Write(peKeySepBytes); err != nil {
114114
return err
115115
}
116-
if err := value.FieldListToJSON(*pe.Key, w); err != nil {
117-
return err
116+
w.WriteByte('{')
117+
nrKeys := len(*pe.Key)
118+
for i, f := range *pe.Key {
119+
if err := w.WriteJSON(f.Name); err != nil {
120+
return err
121+
}
122+
w.WriteByte(':')
123+
if err := w.WriteJSON(f.Value.Unstructured()); err != nil {
124+
return err
125+
}
126+
if i < nrKeys-1 {
127+
w.WriteByte(',')
128+
}
118129
}
130+
w.WriteByte('}')
119131
case pe.Value != nil:
120132
if _, err := w.Write(peValueSepBytes); err != nil {
121133
return err
122134
}
123-
if err := value.ToJSON(*pe.Value, w); err != nil {
135+
if err := w.WriteJSON((*pe.Value).Unstructured()); err != nil {
124136
return err
125137
}
126138
case pe.Index != nil:

Diff for: internal/builder/builder.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (w noNewlineWriter) Write(p []byte) (n int, err error) {
4848
//go:nocheckptr
4949
func noescape(p unsafe.Pointer) unsafe.Pointer {
5050
x := uintptr(p)
51-
return unsafe.Pointer(x ^ 0) //nolint:unsafeptr
51+
return unsafe.Pointer(x ^ 0)
5252
}
5353

5454
func (r *JSONBuilder) WriteJSON(v interface{}) error {
@@ -65,3 +65,25 @@ func (r *JSONBuilder) WriteJSON(v interface{}) error {
6565

6666
return nil
6767
}
68+
69+
func MarshalString(input string) ([]byte, error) {
70+
out, err := gojson.Marshal((*string)(noescape(unsafe.Pointer(&input))))
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
runtime.KeepAlive(input)
76+
77+
return out, nil
78+
}
79+
80+
func MarshalInterface(input interface{}) ([]byte, error) {
81+
out, err := gojson.Marshal((*interface{})(noescape(unsafe.Pointer(&input))))
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
runtime.KeepAlive(input)
87+
88+
return out, nil
89+
}

Diff for: value/fields.go

-19
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,6 @@ func FieldListFromJSON(input []byte) (FieldList, error) {
7171
return fields, nil
7272
}
7373

74-
// FieldListToJSON is a helper function for producing a JSON document.
75-
func FieldListToJSON(v FieldList, w *builder.JSONBuilder) error {
76-
w.WriteByte('{')
77-
for i, f := range v {
78-
if err := w.WriteJSON(f.Name); err != nil {
79-
return err
80-
}
81-
w.WriteByte(':')
82-
if err := w.WriteJSON(f.Value.Unstructured()); err != nil {
83-
return err
84-
}
85-
if i < len(v)-1 {
86-
w.WriteByte(',')
87-
}
88-
}
89-
w.WriteByte('}')
90-
return nil
91-
}
92-
9374
// Sort sorts the field list by Name.
9475
func (f FieldList) Sort() {
9576
if len(f) < 2 {

Diff for: value/value.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func FromJSON(input []byte) (Value, error) {
8585
}
8686

8787
// ToJSON is a helper function for producing a JSON document.
88-
func ToJSON(v Value, w *builder.JSONBuilder) error {
89-
return w.WriteJSON(v.Unstructured())
88+
func ToJSON(v Value) ([]byte, error) {
89+
return builder.MarshalInterface(v.Unstructured())
9090
}
9191

9292
// ToYAML marshals a value as YAML.

0 commit comments

Comments
 (0)