Skip to content

Migrate to encoding/json/v2 #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fieldpath/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (fp Path) Copy() Path {

// MakePath constructs a Path. The parts may be PathElements, ints, strings.
func MakePath(parts ...interface{}) (Path, error) {
var fp Path
fp := make(Path, 0, len(parts))
for _, p := range parts {
switch t := p.(type) {
case PathElement:
Expand Down
184 changes: 100 additions & 84 deletions fieldpath/serialize-pe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,72 +17,101 @@ limitations under the License.
package fieldpath

import (
"bytes"
"errors"
"fmt"
"io"
"strconv"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"sigs.k8s.io/structured-merge-diff/v6/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 = "f"
peField byte = 'f'

// Value indicates that the content of this path element is a field's value
peValue = "v"
peValue byte = 'v'

// Index indicates that the content of this path element is an index in an array
peIndex = "i"
peIndex byte = 'i'

// Key indicates that the content of this path element is a key value map
peKey = "k"
peKey byte = 'k'

// Separator separates the type of a path element from the contents
peSeparator = ":"
peSeparator byte = ':'
)

var (
peFieldSepBytes = []byte(peField + peSeparator)
peValueSepBytes = []byte(peValue + peSeparator)
peIndexSepBytes = []byte(peIndex + peSeparator)
peKeySepBytes = []byte(peKey + peSeparator)
peSepBytes = []byte(peSeparator)
peFieldSepBytes = []byte{peField, peSeparator}
peValueSepBytes = []byte{peValue, peSeparator}
peIndexSepBytes = []byte{peIndex, peSeparator}
peKeySepBytes = []byte{peKey, peSeparator}
)

// readJSONIter reads a Value from a JSON iterator.
// DO NOT EXPORT
// TODO: eliminate this https://github.com/kubernetes-sigs/structured-merge-diff/issues/202
func readJSONIter(iter *jsoniter.Iterator) (value.Value, error) {
v := iter.Read()
if iter.Error != nil && iter.Error != io.EOF {
return nil, iter.Error
}
return value.NewValueInterface(v), nil
// writeValueToEncoder writes a value to an Encoder.
func writeValueToEncoder(v value.Value, enc *jsontext.Encoder) error {
return json.MarshalEncode(enc, v.Unstructured(), json.Deterministic(true))
}

// writeJSONStream writes a value into a JSON stream.
// DO NOT EXPORT
// TODO: eliminate this https://github.com/kubernetes-sigs/structured-merge-diff/issues/202
func writeJSONStream(v value.Value, stream *jsoniter.Stream) {
stream.WriteVal(v.Unstructured())
// FieldListFromJSON is a helper function for reading a JSON document.
func fieldListFromJSON(input []byte) (value.FieldList, error) {
parser := jsontext.NewDecoder(bytes.NewBuffer(input))

if objStart, err := parser.ReadToken(); err != nil {
return nil, fmt.Errorf("parsing JSON: %v", err)
} else if objStart.Kind() != jsontext.BeginObject.Kind() {
return nil, fmt.Errorf("expected object")
}

var fields value.FieldList
for {
if parser.PeekKind() == jsontext.EndObject.Kind() {
if _, err := parser.ReadToken(); err != nil {
return nil, fmt.Errorf("parsing JSON: %v", err)
}
break
}

rawKey, err := parser.ReadToken()
if err == io.EOF {
return nil, fmt.Errorf("unexpected EOF")
} else if err != nil {
return nil, fmt.Errorf("parsing JSON: %v", err)
}

k := rawKey.String()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is rawKey.String() the same as decoding to a string, in terms of interpreting escape sequences, etc?


var v any
if err := json.UnmarshalDecode(parser, &v); err == io.EOF {
return nil, fmt.Errorf("unexpected EOF")
} else if err != nil {
return nil, fmt.Errorf("parsing JSON: %v", err)
}

fields = append(fields, value.Field{Name: k, Value: value.NewValueInterface(v)})
}

return fields, nil
}

// DeserializePathElement parses a serialized path element
func DeserializePathElement(s string) (PathElement, error) {
b := []byte(s)
if len(b) < 2 {
return PathElement{}, errors.New("key must be 2 characters long:")
return PathElement{}, errors.New("key must be 2 characters long")
}
typeSep, b := b[:2], b[2:]
if typeSep[1] != peSepBytes[0] {
typeSep0, typeSep1, b := b[0], b[1], b[2:]
if typeSep1 != peSeparator {
return PathElement{}, fmt.Errorf("missing colon: %v", s)
}
switch typeSep[0] {
switch typeSep0 {
case peFieldSepBytes[0]:
// Slice s rather than convert b, to save on
// allocations.
Expand All @@ -91,29 +120,18 @@ func DeserializePathElement(s string) (PathElement, error) {
FieldName: &str,
}, nil
case peValueSepBytes[0]:
iter := readPool.BorrowIterator(b)
defer readPool.ReturnIterator(iter)
v, err := readJSONIter(iter)
v, err := value.FromJSON(b)
if err != nil {
return PathElement{}, err
}
return PathElement{Value: &v}, nil
case peKeySepBytes[0]:
iter := readPool.BorrowIterator(b)
defer readPool.ReturnIterator(iter)
fields := value.FieldList{}

iter.ReadObjectCB(func(iter *jsoniter.Iterator, key string) bool {
v, err := readJSONIter(iter)
if err != nil {
iter.Error = err
return false
}
fields = append(fields, value.Field{Name: key, Value: v})
return true
})
fields, err := fieldListFromJSON(b)
if err != nil {
return PathElement{}, err
}
fields.Sort()
return PathElement{Key: &fields}, iter.Error
return PathElement{Key: &fields}, nil
case peIndexSepBytes[0]:
i, err := strconv.Atoi(s[2:])
if err != nil {
Expand All @@ -127,60 +145,58 @@ func DeserializePathElement(s string) (PathElement, error) {
}
}

var (
readPool = jsoniter.NewIterator(jsoniter.ConfigCompatibleWithStandardLibrary).Pool()
writePool = jsoniter.NewStream(jsoniter.ConfigCompatibleWithStandardLibrary, nil, 1024).Pool()
)
type PathElementSerializer struct {
buffer bytes.Buffer
encoder jsontext.Encoder
}

// SerializePathElement serializes a path element
func SerializePathElement(pe PathElement) (string, error) {
buf := strings.Builder{}
err := serializePathElementToWriter(&buf, pe)
return buf.String(), err
byteVal, err := (&PathElementSerializer{}).serialize(pe)
return string(byteVal), err
}

func serializePathElementToWriter(w io.Writer, pe PathElement) error {
stream := writePool.BorrowStream(w)
defer writePool.ReturnStream(stream)
func (pes *PathElementSerializer) serialize(pe PathElement) (string, error) {
pes.buffer.Reset()

switch {
case pe.FieldName != nil:
if _, err := stream.Write(peFieldSepBytes); err != nil {
return err
if _, err := pes.buffer.Write(peFieldSepBytes); err != nil {
return "", err
}
stream.WriteRaw(*pe.FieldName)
pes.buffer.WriteString(*pe.FieldName)
case pe.Key != nil:
if _, err := stream.Write(peKeySepBytes); err != nil {
return err
if _, err := pes.buffer.Write(peKeySepBytes); err != nil {
return "", err
}
stream.WriteObjectStart()

for i, field := range *pe.Key {
if i > 0 {
stream.WriteMore()
pes.encoder.Reset(&pes.buffer)
pes.encoder.WriteToken(jsontext.BeginObject)
for _, f := range *pe.Key {
if err := pes.encoder.WriteToken(jsontext.String(f.Name)); err != nil {
return "", err
}
if err := writeValueToEncoder(f.Value, &pes.encoder); err != nil {
return "", err
}
stream.WriteObjectField(field.Name)
writeJSONStream(field.Value, stream)
}
stream.WriteObjectEnd()
pes.encoder.WriteToken(jsontext.EndObject)
case pe.Value != nil:
if _, err := stream.Write(peValueSepBytes); err != nil {
return err
if _, err := pes.buffer.Write(peValueSepBytes); err != nil {
return "", err
}
pes.encoder.Reset(&pes.buffer)
if err := writeValueToEncoder(*pe.Value, &pes.encoder); err != nil {
return "", err
}
writeJSONStream(*pe.Value, stream)
case pe.Index != nil:
if _, err := stream.Write(peIndexSepBytes); err != nil {
return err
if _, err := pes.buffer.Write(peIndexSepBytes); err != nil {
return "", err
}
stream.WriteInt(*pe.Index)
pes.buffer.WriteString(strconv.Itoa(*pe.Index))
default:
return errors.New("invalid PathElement")
return "", errors.New("invalid PathElement")
}
b := stream.Buffer()
err := stream.Flush()
// Help jsoniter manage its buffers--without this, the next
// use of the stream is likely to require an allocation. Look
// at the jsoniter stream code to understand why. They were probably
// optimizing for folks using the buffer directly.
stream.SetBuffer(b[:0])
return err

// TODO: is there a way to not emit newlines
return strings.TrimSpace(pes.buffer.String()), nil
}
Loading