add test of map with key with custom marshaler

This commit is contained in:
Nicolas S. Dade
2018-03-06 13:49:51 -08:00
parent 32fa128f23
commit 699d6f0801
3 changed files with 34 additions and 1 deletions
+3 -1
View File
@@ -23,7 +23,8 @@ generate: root build
.root/src/$(PKG)/tests/data.go \
.root/src/$(PKG)/tests/omitempty.go \
.root/src/$(PKG)/tests/nothing.go \
.root/src/$(PKG)/tests/named_type.go
.root/src/$(PKG)/tests/named_type.go \
.root/src/$(PKG)/tests/custom_map_key_type.go
.root/bin/easyjson -all .root/src/$(PKG)/tests/data.go
.root/bin/easyjson -all .root/src/$(PKG)/tests/nothing.go
@@ -33,6 +34,7 @@ generate: root build
.root/bin/easyjson -build_tags=use_easyjson .root/src/$(PKG)/benchmark/data.go
.root/bin/easyjson .root/src/$(PKG)/tests/nested_easy.go
.root/bin/easyjson .root/src/$(PKG)/tests/named_type.go
.root/bin/easyjson .root/src/$(PKG)/tests/custom_map_key_type.go
test: generate root
go test \
+1
View File
@@ -38,6 +38,7 @@ var testCases = []struct {
{&IntsValue, IntsString},
{&mapStringStringValue, mapStringStringString},
{&namedTypeValue, namedTypeValueString},
{&customMapKeyTypeValue, customMapKeyTypeValueString},
{&mapMyIntStringValue, mapMyIntStringValueString},
{&mapIntStringValue, mapIntStringValueString},
{&mapInt32StringValue, mapInt32StringValueString},
+30
View File
@@ -0,0 +1,30 @@
package tests
import fmt "fmt"
//easyjson:json
type CustomMapKeyType struct {
Map map[customKeyType]int
}
type customKeyType [2]byte
func (k customKeyType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%02x"`, k)), nil
}
func (k *customKeyType) UnmarshalJSON(b []byte) error {
_, err := fmt.Sscanf(string(b), `"%02x%02x"`, &k[0], &k[1])
return err
}
var customMapKeyTypeValue CustomMapKeyType
func init() {
customMapKeyTypeValue.Map = map[customKeyType]int{
customKeyType{0x01, 0x01}: 1,
customKeyType{0x02, 0x02}: 2,
}
}
var customMapKeyTypeValueString = `{"Map":{"0101":1,"0202":2}}`