Skip to content

Commit

Permalink
Checkpoint: Implemented marshal funcs and added test
Browse files Browse the repository at this point in the history
Signed-off-by: Annanay <annanayagarwal@gmail.com>
  • Loading branch information
annanay25 committed Sep 22, 2021
1 parent a8f1d7d commit c97995c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
33 changes: 32 additions & 1 deletion modules/overrides/list_to_map.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package overrides

import "encoding/json"
import (
"encoding/json"

"gopkg.in/yaml.v2"
)

type ListToMap struct {
m map[string]struct{}
}

var _ yaml.Marshaler = (*ListToMap)(nil)
var _ yaml.Unmarshaler = (*ListToMap)(nil)
var _ json.Marshaler = (*ListToMap)(nil)
var _ json.Unmarshaler = (*ListToMap)(nil)

// MarshalYAML implements the Marshal interface of the yaml pkg.
func (l ListToMap) MarshalYAML() (interface{}, error) {
list := make([]string, 0)
for k := range l.m {
list = append(list, k)
}

b, err := yaml.Marshal(&list)
return b, err
}

// UnmarshalYAML implements the Unmarshaler interface of the yaml pkg.
func (l *ListToMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
list := make([]string, 0)
Expand All @@ -21,6 +41,17 @@ func (l *ListToMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

// MarshalJSON implements the Marshal interface of the json pkg.
func (l ListToMap) MarshalJSON() ([]byte, error) {
list := make([]string, 0)
for k := range l.m {
list = append(list, k)
}

return json.Marshal(&list)
}

// UnmarshalJSON implements the Unmarshal interface of the json pkg.
func (l *ListToMap) UnmarshalJSON(b []byte) error {
list := make([]string, 0)
err := json.Unmarshal(b, &list)
Expand Down
55 changes: 55 additions & 0 deletions modules/overrides/list_to_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package overrides

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func TestListToMapMarshalOperations(t *testing.T) {
testCases := []struct {
name string
original ListToMap
marshalledYAML string
marshalledJSON string
}{
{
name: "empty map",
original: ListToMap{},
marshalledYAML: "",
marshalledJSON: "[]",
},
{
name: "map with entries",
original: ListToMap{
m: map[string]struct{}{
"foo": {},
},
},
marshalledYAML: "- foo",
marshalledJSON: "[\"foo\"]",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
bytes, err := yaml.Marshal(tc.original)
assert.NoError(t, err)
assert.Equal(t, tc.marshalledYAML, string(bytes))

l := ListToMap{}
assert.NoError(t, yaml.Unmarshal([]byte(tc.marshalledYAML), &l))
assert.Equal(t, tc.original, l)

bytes, err = json.Marshal(tc.original)
assert.NoError(t, err)
assert.Equal(t, tc.marshalledJSON, string(bytes))

l2 := ListToMap{}
assert.NoError(t, json.Unmarshal([]byte(tc.marshalledJSON), &l2))
assert.Equal(t, tc.original, l2)
})
}
}

0 comments on commit c97995c

Please sign in to comment.