-
Notifications
You must be signed in to change notification settings - Fork 63
set serialization #90
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
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" | ||
"strings" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"sigs.k8s.io/structured-merge-diff/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" | ||
|
||
// Value indicates that the content of this path element is a field's value | ||
peValue = "v" | ||
|
||
// Index indicates that the content of this path element is an index in an array | ||
peIndex = "i" | ||
|
||
// Key indicates that the content of this path element is a key value map | ||
peKey = "k" | ||
|
||
// Separator separates the type of a path element from the contents | ||
peSeparator = ":" | ||
) | ||
|
||
var ( | ||
peFieldSepBytes = []byte(peField + peSeparator) | ||
peValueSepBytes = []byte(peValue + peSeparator) | ||
peIndexSepBytes = []byte(peIndex + peSeparator) | ||
peKeySepBytes = []byte(peKey + peSeparator) | ||
peSepBytes = []byte(peSeparator) | ||
) | ||
|
||
// 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:") | ||
} | ||
typeSep, b := b[:2], b[2:] | ||
if typeSep[1] != peSepBytes[0] { | ||
return PathElement{}, fmt.Errorf("missing colon: %v", s) | ||
} | ||
switch typeSep[0] { | ||
case peFieldSepBytes[0]: | ||
// Slice s rather than convert b, to save on | ||
// allocations. | ||
str := s[2:] | ||
return PathElement{ | ||
FieldName: &str, | ||
}, nil | ||
case peValueSepBytes[0]: | ||
iter := readPool.BorrowIterator(b) | ||
defer readPool.ReturnIterator(iter) | ||
v, err := value.ReadJSONIter(iter) | ||
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. Could you call |
||
if err != nil { | ||
return PathElement{}, err | ||
} | ||
return PathElement{Value: &v}, nil | ||
case peKeySepBytes[0]: | ||
iter := readPool.BorrowIterator(b) | ||
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. Same as above |
||
defer readPool.ReturnIterator(iter) | ||
v, err := value.ReadJSONIter(iter) | ||
if err != nil { | ||
return PathElement{}, err | ||
} | ||
if v.MapValue == nil { | ||
return PathElement{}, fmt.Errorf("expected key value pairs but got %#v", v) | ||
} | ||
return PathElement{Key: v.MapValue}, 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 | ||
} | ||
} | ||
|
||
var ( | ||
readPool = jsoniter.NewIterator(jsoniter.ConfigCompatibleWithStandardLibrary).Pool() | ||
writePool = jsoniter.NewStream(jsoniter.ConfigCompatibleWithStandardLibrary, nil, 1024).Pool() | ||
) | ||
|
||
// SerializePathElement serializes a path element | ||
func SerializePathElement(pe PathElement) (string, error) { | ||
buf := strings.Builder{} | ||
stream := writePool.BorrowStream(&buf) | ||
defer writePool.ReturnStream(stream) | ||
switch { | ||
case pe.FieldName != nil: | ||
if _, err := stream.Write(peFieldSepBytes); err != nil { | ||
return "", err | ||
} | ||
stream.WriteRaw(*pe.FieldName) | ||
case pe.Key != nil: | ||
if _, err := stream.Write(peKeySepBytes); err != nil { | ||
return "", err | ||
} | ||
v := value.Value{MapValue: pe.Key} | ||
v.WriteJSONStream(stream) | ||
case pe.Value != nil: | ||
if _, err := stream.Write(peValueSepBytes); err != nil { | ||
return "", err | ||
} | ||
pe.Value.WriteJSONStream(stream) | ||
case pe.Index != nil: | ||
if _, err := stream.Write(peIndexSepBytes); err != nil { | ||
return "", err | ||
} | ||
stream.WriteInt(*pe.Index) | ||
default: | ||
return "", errors.New("invalid PathElement") | ||
} | ||
err := stream.Flush() | ||
return buf.String(), err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
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 "testing" | ||
|
||
func TestPathElementRoundTrip(t *testing.T) { | ||
tests := []string{ | ||
`i:0`, | ||
`i:1234`, | ||
`f:`, | ||
`f:spec`, | ||
`f:more-complicated-string`, | ||
`k:{"name":"my-container"}`, | ||
`k:{"port":"8080","protocol":"TCP"}`, | ||
`k:{"optionalField":null}`, | ||
`k:{"jsonField":{"A":1,"B":null,"C":"D","E":{"F":"G"}}}`, | ||
`k:{"listField":["1","2","3"]}`, | ||
`v:null`, | ||
`v:"some-string"`, | ||
`v:1234`, | ||
`v:{"some":"json"}`, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test, func(t *testing.T) { | ||
pe, err := DeserializePathElement(test) | ||
if err != nil { | ||
t.Fatalf("Failed to create path element: %v", err) | ||
} | ||
output, err := SerializePathElement(pe) | ||
if err != nil { | ||
t.Fatalf("Failed to create string from path element: %v", err) | ||
} | ||
if test != output { | ||
t.Fatalf("Expected round-trip:\ninput: %v\noutput: %v", test, output) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPathElementIgnoreUnknown(t *testing.T) { | ||
_, err := DeserializePathElement("r:Hello") | ||
if err != ErrUnknownPathElementType { | ||
t.Fatalf("Unknown qualifiers must not return an invalid path element") | ||
} | ||
} | ||
|
||
func TestDeserializePathElementError(t *testing.T) { | ||
tests := []string{ | ||
``, | ||
`no-colon`, | ||
`i:index is not a number`, | ||
`i:1.23`, | ||
`i:`, | ||
`v:invalid json`, | ||
`v:`, | ||
`k:invalid json`, | ||
`k:{"name":invalid}`, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test, func(t *testing.T) { | ||
pe, err := DeserializePathElement(test) | ||
if err == nil { | ||
t.Fatalf("Expected error, no error found. got: %#v, %s", pe, pe) | ||
} | ||
}) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is copying the full buffer, is it worth optimizing?