github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_override_test.go (about) 1 package jzon 2 3 import ( 4 "reflect" 5 "testing" 6 "unsafe" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 type testStringKind string 12 13 type testStringKindDecoder struct { 14 } 15 16 func (*testStringKindDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 17 s, err := it.ReadString() 18 if err != nil { 19 return err 20 } 21 *((*string)(ptr)) = "`" + s + "`" 22 return nil 23 } 24 25 func TestValDecoder_Native_Kind_String(t *testing.T) { 26 decCfg := NewDecoderConfig(&DecoderOption{ 27 ValDecoders: map[reflect.Type]ValDecoder{ 28 reflect.TypeOf(string("")): (*testStringKindDecoder)(nil), 29 }, 30 }) 31 data := []byte(`"abc"`) 32 var s testStringKind = "dummy" 33 err := decCfg.Unmarshal(data, &s) 34 require.NoError(t, err) 35 require.Equal(t, testStringKind("`abc`"), s) 36 }