Skip to content

Commit

Permalink
fix: add missing test and proper check for map[string]interface{}
Browse files Browse the repository at this point in the history
Somehow this got lost during transition to new type checking. This commit should fix that.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Aug 24, 2022
1 parent 647e9da commit 3617e19
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
10 changes: 5 additions & 5 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func BenchmarkCustom(b *testing.B) {
protoenc.CleanEncoderDecoder()
})

o := OneFieldStruct[CustomEncoderStruct]{
Field: CustomEncoderStruct{
o := Value[CustomEncoderStruct]{
V: CustomEncoderStruct{
Value: 150,
},
}
Expand All @@ -65,17 +65,17 @@ func BenchmarkCustom(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()

target := &OneFieldStruct[CustomEncoderStruct]{}
target := &Value[CustomEncoderStruct]{}
for i := 0; i < b.N; i++ {
*target = OneFieldStruct[CustomEncoderStruct]{}
*target = Value[CustomEncoderStruct]{}

err := protoenc.Unmarshal(encoded, target)
if err != nil {
b.Fatal(err)
}
}

require.Equal(b, o.Field.Value+2, target.Field.Value)
require.Equal(b, o.V.Value+2, target.V.Value)
}

func BenchmarkSlice(b *testing.B) {
Expand Down
69 changes: 43 additions & 26 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/big"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -84,36 +85,32 @@ func Test2dSlice(t *testing.T) {
t.Run("should fail on 2d int slice", func(t *testing.T) {
t.Parallel()

encoded := Slice[int]{Values: [][]int{{1, 2, 3}, {4, 5, 6}}}
encoded := Value[[][]int]{V: [][]int{{1, 2, 3}, {4, 5, 6}}}
_, err := protoenc.Marshal(&encoded)
require.Error(t, err)
})

t.Run("should fail on 2d uint16 slice", func(t *testing.T) {
t.Parallel()

encoded := Slice[uint16]{Values: [][]uint16{{1, 2, 3}, {4, 5, 6}}}
encoded := Value[[][]uint16]{V: [][]uint16{{1, 2, 3}, {4, 5, 6}}}
_, err := protoenc.Marshal(&encoded)
require.Error(t, err)
})

t.Run("should ok on 2d byte slice", func(t *testing.T) {
t.Parallel()

encoded := Slice[byte]{Values: [][]byte{{1, 2, 3}, {4, 5, 6}}}
encoded := Value[[][]byte]{V: [][]byte{{1, 2, 3}, {4, 5, 6}}}
buf := must(protoenc.Marshal(&encoded))(t)

decoded := Slice[byte]{}
decoded := Value[[][]byte]{}
require.NoError(t, protoenc.Unmarshal(buf, &decoded))

require.Equal(t, encoded.Values, decoded.Values)
require.Equal(t, encoded.V, decoded.V)
})
}

type Slice[T any] struct {
Values [][]T `protobuf:"1"`
}

func TestBigInt(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -302,7 +299,7 @@ func TestMarshalEmpty(t *testing.T) {
buf := must(protoenc.Marshal(&Empty{}))(t)
require.Len(t, buf, 0)

buf = must(protoenc.Marshal(&OneFieldStruct[Empty]{}))(t)
buf = must(protoenc.Marshal(&Value[Empty]{}))(t)
require.Equal(t, []byte{0x0a, 0x00}, buf)
}

Expand Down Expand Up @@ -421,13 +418,13 @@ func TestCustomEncoders(t *testing.T) {
testCustomEncodersDecoders(
encodeCustomEncoderStruct,
decodeCustomEncoderStruct,
OneFieldStruct[CustomEncoderStruct]{
Field: CustomEncoderStruct{
Value[CustomEncoderStruct]{
V: CustomEncoderStruct{
Value: 150,
},
},
OneFieldStruct[CustomEncoderStruct]{
Field: CustomEncoderStruct{
Value[CustomEncoderStruct]{
V: CustomEncoderStruct{
Value: 152,
},
},
Expand All @@ -437,13 +434,13 @@ func TestCustomEncoders(t *testing.T) {
testCustomEncodersDecoders(
encodeCustomEncoderStructPtr,
decodeCustomEncoderStructPtr,
OneFieldStruct[*CustomEncoderStruct]{
Field: &CustomEncoderStruct{
Value[*CustomEncoderStruct]{
V: &CustomEncoderStruct{
Value: 150,
},
},
OneFieldStruct[*CustomEncoderStruct]{
Field: &CustomEncoderStruct{
Value[*CustomEncoderStruct]{
V: &CustomEncoderStruct{
Value: 156,
},
},
Expand All @@ -453,14 +450,14 @@ func TestCustomEncoders(t *testing.T) {
testCustomEncodersDecoders(
encodeCustomEncoderStruct,
decodeCustomEncoderStruct,
OneFieldStruct[[]CustomEncoderStruct]{
Field: []CustomEncoderStruct{
Value[[]CustomEncoderStruct]{
V: []CustomEncoderStruct{
{Value: 150},
{Value: 151},
},
},
OneFieldStruct[[]CustomEncoderStruct]{
Field: []CustomEncoderStruct{
Value[[]CustomEncoderStruct]{
V: []CustomEncoderStruct{
{Value: 152},
{Value: 153},
},
Expand Down Expand Up @@ -530,10 +527,6 @@ func testCustomEncodersDecoders[V any, T any](
}
}

type OneFieldStruct[T any] struct {
Field T `protobuf:"1"`
}

func TestIncorrectCustomEncoders(t *testing.T) {
t.Cleanup(func() {
protoenc.CleanEncoderDecoder()
Expand All @@ -554,6 +547,30 @@ func TestIncorrectCustomEncoders(t *testing.T) {
})
}

func TestMarshalMapInterface(t *testing.T) {
// json decodes numeric values as float64s
// json decoder do not support slices
testEncodeDecode(Value[map[string]interface{}]{
V: map[string]interface{}{
"a": 1.0,
"b": "2",
"c": true,
"e": map[string]interface{}{
"a": 1.0,
"g": 10.10,
},
},
})(t)
}

func TestMarshalTime(t *testing.T) {
// json decodes numeric values as float64s
// json decoder do not support slices
testEncodeDecode(Value[time.Time]{
V: time.Date(2019, time.January, 1, 0, 0, 0, 0, time.UTC),
})(t)
}

func TestResursiveTypes(t *testing.T) {
t.Run("test recursive struct", func(t *testing.T) {
type Recursive struct {
Expand Down
7 changes: 5 additions & 2 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ func testEncodeDecodeWrapped[T any](slc T, expected []byte) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()

original := OneFieldStruct[T]{Field: slc}
original := Value[T]{V: slc}
buf := must(protoenc.Marshal(&original))(t)

require.Equal(t, expected, buf)

var decoded OneFieldStruct[T]
var decoded Value[T]

require.NoError(t, protoenc.Unmarshal(buf, &decoded))
require.Equal(t, original, decoded)
Expand Down Expand Up @@ -341,6 +341,9 @@ func TestDisallowedTypes(t *testing.T) {
"map[string]string": {
fn: testDisallowedTypes[map[string]string],
},
"[]map[string]string": {
fn: testDisallowedTypes[[]map[string]string],
},
"sliceWrapper[*int]": {
fn: testDisallowedTypes[sliceWrapper[*int]],
},
Expand Down
4 changes: 4 additions & 0 deletions type_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ func validateFieldPtr(typ reflect.Type) error {
}

func validateMap(elem reflect.Type) error {
if elem == typeMapInterface {
return nil
}

if err := validateMapKey(elem.Key()); err != nil {
return err
}
Expand Down

0 comments on commit 3617e19

Please sign in to comment.