mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
update extension
This commit is contained in:
parent
d0f45c663f
commit
7d5f2aed7b
@ -45,8 +45,8 @@ func Test_customize_field_decoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_customize_field_decoder_factory(t *testing.T) {
|
func Test_customize_field_by_extension(t *testing.T) {
|
||||||
RegisterFieldCustomizer(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
|
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
|
||||||
if (type_.String() == "jsoniter.Tom" && field.Name == "field1") {
|
if (type_.String() == "jsoniter.Tom" && field.Name == "field1") {
|
||||||
return []string{"field-1"}, func(ptr unsafe.Pointer, iter *Iterator) {
|
return []string{"field-1"}, func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||||
@ -63,3 +63,27 @@ func Test_customize_field_decoder_factory(t *testing.T) {
|
|||||||
t.Fatal(tom.field1)
|
t.Fatal(tom.field1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Jerry struct {
|
||||||
|
field1 string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_customize_type_by_extension(t *testing.T) {
|
||||||
|
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
|
||||||
|
if (type_.String() == "jsoniter.Jerry" && field == nil) {
|
||||||
|
return nil, func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
obj := (*Jerry)(ptr)
|
||||||
|
obj.field1 = iter.ReadString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
})
|
||||||
|
jerry := Jerry{}
|
||||||
|
err := Unmarshal([]byte(`"100"`), &jerry)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if jerry.field1 != "100" {
|
||||||
|
t.Fatal(jerry.field1)
|
||||||
|
}
|
||||||
|
}
|
@ -450,17 +450,17 @@ func getDecoderFromCache(cacheKey reflect.Type) Decoder {
|
|||||||
|
|
||||||
var typeDecoders map[string]Decoder
|
var typeDecoders map[string]Decoder
|
||||||
var fieldDecoders map[string]Decoder
|
var fieldDecoders map[string]Decoder
|
||||||
var fieldCustomizers []FieldCustomizerFunc
|
var extensions []ExtensionFunc
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
typeDecoders = map[string]Decoder{}
|
typeDecoders = map[string]Decoder{}
|
||||||
fieldDecoders = map[string]Decoder{}
|
fieldDecoders = map[string]Decoder{}
|
||||||
fieldCustomizers = []FieldCustomizerFunc{}
|
extensions = []ExtensionFunc{}
|
||||||
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
|
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
||||||
type FieldCustomizerFunc func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc)
|
type ExtensionFunc func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc)
|
||||||
|
|
||||||
type funcDecoder struct {
|
type funcDecoder struct {
|
||||||
func_ DecoderFunc
|
func_ DecoderFunc
|
||||||
@ -478,8 +478,8 @@ func RegisterFieldDecoder(type_ string, field string, func_ DecoderFunc) {
|
|||||||
fieldDecoders[fmt.Sprintf("%s/%s", type_, field)] = &funcDecoder{func_}
|
fieldDecoders[fmt.Sprintf("%s/%s", type_, field)] = &funcDecoder{func_}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterFieldCustomizer(func_ FieldCustomizerFunc) {
|
func RegisterExtension(extension ExtensionFunc) {
|
||||||
fieldCustomizers = append(fieldCustomizers, func_)
|
extensions = append(extensions, extension)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClearDecoders() {
|
func ClearDecoders() {
|
||||||
@ -632,6 +632,14 @@ func decoderOfPtr(type_ reflect.Type) (Decoder, error) {
|
|||||||
if typeName == "jsoniter.Any" {
|
if typeName == "jsoniter.Any" {
|
||||||
return &anyDecoder{}, nil
|
return &anyDecoder{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, extension := range extensions {
|
||||||
|
alternativeFieldNames, func_ := extension(type_, nil)
|
||||||
|
if alternativeFieldNames != nil {
|
||||||
|
return nil, fmt.Errorf("%v should not return alternative field names when only type is being passed", extension)
|
||||||
|
}
|
||||||
|
typeDecoders[typeName] = &funcDecoder{func_}
|
||||||
|
}
|
||||||
typeDecoder := typeDecoders[typeName]
|
typeDecoder := typeDecoders[typeName]
|
||||||
if typeDecoder != nil {
|
if typeDecoder != nil {
|
||||||
return typeDecoder, nil
|
return typeDecoder, nil
|
||||||
@ -694,8 +702,8 @@ func decoderOfStruct(type_ reflect.Type) (Decoder, error) {
|
|||||||
field := type_.Field(i)
|
field := type_.Field(i)
|
||||||
fieldDecoderKey := fmt.Sprintf("%s/%s", type_.String(), field.Name)
|
fieldDecoderKey := fmt.Sprintf("%s/%s", type_.String(), field.Name)
|
||||||
var fieldNames []string
|
var fieldNames []string
|
||||||
for _, customizer := range fieldCustomizers {
|
for _, extension := range extensions {
|
||||||
alternativeFieldNames, func_ := customizer(type_, &field)
|
alternativeFieldNames, func_ := extension(type_, &field)
|
||||||
if alternativeFieldNames != nil {
|
if alternativeFieldNames != nil {
|
||||||
fieldNames = alternativeFieldNames
|
fieldNames = alternativeFieldNames
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user