-
Notifications
You must be signed in to change notification settings - Fork 65
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
inteon
wants to merge
4
commits into
kubernetes-sigs:master
Choose a base branch
from
inteon:use_json_v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+253
−294
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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 { | ||
|
@@ -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 | ||
inteon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.