mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
add test of map with key with custom marshaler
This commit is contained in:
@@ -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 \
|
||||
|
||||
@@ -38,6 +38,7 @@ var testCases = []struct {
|
||||
{&IntsValue, IntsString},
|
||||
{&mapStringStringValue, mapStringStringString},
|
||||
{&namedTypeValue, namedTypeValueString},
|
||||
{&customMapKeyTypeValue, customMapKeyTypeValueString},
|
||||
{&mapMyIntStringValue, mapMyIntStringValueString},
|
||||
{&mapIntStringValue, mapIntStringValueString},
|
||||
{&mapInt32StringValue, mapInt32StringValueString},
|
||||
|
||||
@@ -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}}`
|
||||
Reference in New Issue
Block a user