Skip to content

Commit

Permalink
v2.1.8: YAML serialization/deserialization
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Rouge <[email protected]>
  • Loading branch information
wk8 committed Jun 27, 2023
1 parent 7589f2d commit 85ca4a2
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .circleci/circle_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -ex

# might as well run a little longer
export FUZZ_TIME=25s
export FUZZ_TIME=20s

# there are too many golangci plugins that don't work for 1.19 just yet, so just skip linting for it
if [[ "$GO_VER" == 1.18.* ]]; then
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

[comment]: # (Changes since last release go here)

## 2.1.8 - Jun 27th 2023

* Added support for YAML serialization/deserialization

## 2.1.7 - Apr 13th 2023

* Renamed test_utils.go to utils_test.go
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ bench:

FUZZ_TIME ?= 10s

# see https://github.com/golang/go/issues/46312
# and https://stackoverflow.com/a/72673487/4867444
# if we end up having more fuzz tests
.PHONY: test_with_fuzz
test_with_fuzz:
$(TEST_COMMAND) -fuzz=. -fuzztime=$(FUZZ_TIME)
$(TEST_COMMAND) -fuzz=FuzzRoundTripJSON -fuzztime=$(FUZZ_TIME)
$(TEST_COMMAND) -fuzz=FuzzRoundTripYAML -fuzztime=$(FUZZ_TIME)

.PHONY: fuzz
fuzz: test_with_fuzz
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ require (
github.com/buger/jsonparser v1.1.1
github.com/mailru/easyjson v0.7.7
github.com/stretchr/testify v1.8.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 3 additions & 2 deletions json_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package orderedmap

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func FuzzRoundTrip(f *testing.F) {
func FuzzRoundTripJSON(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
for _, testCase := range []struct {
name string
Expand Down
22 changes: 10 additions & 12 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package orderedmap

import (
"fmt"
"github.com/rs/zerolog/log"

"gopkg.in/yaml.v3"
)

Expand All @@ -12,7 +12,7 @@ var (
)

// MarshalYAML implements the yaml.Marshaler interface.
func (om *OrderedMap[K, V]) MarshalYAML() (interface{}, error) { //nolint:funlen
func (om *OrderedMap[K, V]) MarshalYAML() (interface{}, error) {
if om == nil {
return []byte("null"), nil
}
Expand All @@ -26,9 +26,8 @@ func (om *OrderedMap[K, V]) MarshalYAML() (interface{}, error) { //nolint:funlen

keyNode := &yaml.Node{}

// serialize key to yaml, then unserialize it back into the node
// serialize key to yaml, then deserialize it back into the node
// this is a hack to get the correct tag for the key

if err := keyNode.Encode(key); err != nil {
return nil, err
}
Expand All @@ -46,23 +45,22 @@ func (om *OrderedMap[K, V]) MarshalYAML() (interface{}, error) { //nolint:funlen

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (om *OrderedMap[K, V]) UnmarshalYAML(value *yaml.Node) error {
if value.Kind != yaml.MappingNode {
return fmt.Errorf("pipeline must contain YAML mapping, has %v", value.Kind)
}

if om.list == nil {
om.initialize(0)
}

log.Info().Msgf("UnmarshalYAML: %v", value)

if value.Kind != yaml.MappingNode {
return fmt.Errorf("pipeline must contain YAML mapping, has %v", value.Kind)
}
for i := 0; i < len(value.Content); i += 2 {
for index := 0; index < len(value.Content); index += 2 {
var key K
var val V

if err := value.Content[i].Decode(&key); err != nil {
if err := value.Content[index].Decode(&key); err != nil {
return err
}
if err := value.Content[i+1].Decode(&val); err != nil {
if err := value.Content[index+1].Decode(&val); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestYAMLRoundTrip(t *testing.T) {
}{
{
name: "empty map",
input: `{}`,
input: "{}\n",
targetFactory: func() any {
return &OrderedMap[string, any]{}
},
Expand Down

0 comments on commit 85ca4a2

Please sign in to comment.