Skip to content

Commit

Permalink
Merge pull request #3122 from karaatanassov/issue-3121
Browse files Browse the repository at this point in the history
fix: byte, float and int (de) serialization
  • Loading branch information
karaatanassov committed May 11, 2023
2 parents 2dbc28b + 93cb079 commit a939567
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 12 deletions.
16 changes: 8 additions & 8 deletions vim25/json/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ func discriminatorParseTypeName(
// type is a pointer.
n, p := indirectTypeName(tn)

// First look up the type in the built-in type registry.
t, ok := discriminatorTypeRegistry[n]
var t reflect.Type
ok := false
// look up the type in the external registry to allow name override.
if typeFn != nil {
t, ok = typeFn(n)
}
if !ok {
// If not found in the type registry then see if the type
// is returne from the optional type function.
if typeFn == nil {
return nil, false
}
if t, ok = typeFn(n); !ok {
// Use the built-in registry if the external registry fails
if t, ok = discriminatorTypeRegistry[n]; !ok {
return nil, false
}
}
Expand Down
10 changes: 6 additions & 4 deletions vim25/types/json_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ const (

var discriminatorTypeRegistry = map[string]reflect.Type{
"boolean": reflect.TypeOf(true),
"byte": reflect.TypeOf(int8(0)),
"byte": reflect.TypeOf(uint8(0)),
"short": reflect.TypeOf(int16(0)),
"int": reflect.TypeOf(int32(0)),
"long": reflect.TypeOf(int64(0)),
"float": reflect.TypeOf(float64(0)),
"float": reflect.TypeOf(float32(0)),
"double": reflect.TypeOf(float64(0)),
"string": reflect.TypeOf(""),
}

Expand All @@ -61,11 +62,12 @@ func NewJSONDecoder(r io.Reader) *json.Decoder {
// VMOMI primitive names
var discriminatorNamesRegistry = map[reflect.Type]string{
reflect.TypeOf(true): "boolean",
reflect.TypeOf(int8(0)): "byte",
reflect.TypeOf(uint8(0)): "byte",
reflect.TypeOf(int16(0)): "short",
reflect.TypeOf(int32(0)): "int",
reflect.TypeOf(int64(0)): "long",
reflect.TypeOf(float64(0)): "float",
reflect.TypeOf(float32(0)): "float",
reflect.TypeOf(float64(0)): "double",
reflect.TypeOf(""): "string",
}

Expand Down
107 changes: 107 additions & 0 deletions vim25/types/json_encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved.
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 types

import (
"bytes"
"strings"
"testing"

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

func TestOptionValueSerialization(t *testing.T) {
options := []struct {
name string
wire string
binding OptionValue
}{
{
name: "boolean",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "boolean","_value": true}
}`,
binding: OptionValue{Key: "option1", Value: true},
},
{
name: "byte",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "byte","_value": 16}
}`,
binding: OptionValue{Key: "option1", Value: uint8(16)},
},
{
name: "short",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "short","_value": 300}
}`,
binding: OptionValue{Key: "option1", Value: int16(300)},
},
{
name: "int",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "int","_value": 300}}`,
binding: OptionValue{Key: "option1", Value: int32(300)},
},
{
name: "long",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "long","_value": 300}}`,
binding: OptionValue{Key: "option1", Value: int64(300)},
},
{
name: "float",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "float","_value": 30.5}}`,
binding: OptionValue{Key: "option1", Value: float32(30.5)},
},
{
name: "double",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "double","_value": 12.2}}`,
binding: OptionValue{Key: "option1", Value: float64(12.2)},
},
{
name: "string",
wire: `{"_typeName": "OptionValue","key": "option1",
"value": {"_typeName": "string","_value": "test"}}`,
binding: OptionValue{Key: "option1", Value: "test"},
},
}

for _, opt := range options {
t.Run("Serialize "+opt.name, func(t *testing.T) {
r := strings.NewReader(opt.wire)
dec := NewJSONDecoder(r)
v := OptionValue{}
e := dec.Decode(&v)
if e != nil {
assert.Fail(t, "Cannot read json", "json %v err %v", opt.wire, e)
return
}
assert.Equal(t, opt.binding, v)
})

t.Run("De-serialize "+opt.name, func(t *testing.T) {
var w bytes.Buffer
enc := NewJSONEncoder(&w)
enc.Encode(opt.binding)
s := w.String()
assert.JSONEq(t, opt.wire, s)
})
}
}

0 comments on commit a939567

Please sign in to comment.