github.com/hamba/avro@v1.8.0/codec_marshaler_test.go (about) 1 package avro_test 2 3 import ( 4 "bytes" 5 "errors" 6 "testing" 7 "time" 8 9 "github.com/hamba/avro" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestDecoder_TextUnmarshalerPtr(t *testing.T) { 14 defer ConfigTeardown() 15 16 data := []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a} 17 schema := "string" 18 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 19 assert.NoError(t, err) 20 21 var ts TestTimestampPtr 22 err = dec.Decode(&ts) 23 24 assert.NoError(t, err) 25 want := TestTimestampPtr(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC)) 26 assert.Equal(t, want, ts) 27 } 28 29 func TestDecoder_TextUnmarshalerPtrPtr(t *testing.T) { 30 defer ConfigTeardown() 31 32 data := []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a} 33 schema := "string" 34 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 35 assert.NoError(t, err) 36 37 var ts *TestTimestampPtr 38 err = dec.Decode(&ts) 39 40 assert.NoError(t, err) 41 assert.NotNil(t, ts) 42 want := TestTimestampPtr(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC)) 43 assert.Equal(t, want, *ts) 44 } 45 46 func TestDecoder_TextUnmarshalerError(t *testing.T) { 47 defer ConfigTeardown() 48 49 data := []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a} 50 schema := "string" 51 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 52 assert.NoError(t, err) 53 54 var ts *TestTimestampError 55 err = dec.Decode(&ts) 56 57 assert.Error(t, err) 58 } 59 60 func TestEncoder_TextMarshaler(t *testing.T) { 61 defer ConfigTeardown() 62 63 schema := "string" 64 buf := bytes.NewBuffer([]byte{}) 65 enc, err := avro.NewEncoder(schema, buf) 66 assert.NoError(t, err) 67 ts := TestTimestamp(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC)) 68 69 err = enc.Encode(ts) 70 71 assert.NoError(t, err) 72 assert.Equal(t, []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a}, buf.Bytes()) 73 } 74 75 func TestEncoder_TextMarshalerPtr(t *testing.T) { 76 defer ConfigTeardown() 77 78 schema := "string" 79 buf := bytes.NewBuffer([]byte{}) 80 enc, err := avro.NewEncoder(schema, buf) 81 assert.NoError(t, err) 82 ts := TestTimestampPtr(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC)) 83 84 err = enc.Encode(&ts) 85 86 assert.NoError(t, err) 87 assert.Equal(t, []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a}, buf.Bytes()) 88 } 89 90 func TestEncoder_TextMarshalerPtrNil(t *testing.T) { 91 defer ConfigTeardown() 92 93 schema := "string" 94 buf := bytes.NewBuffer([]byte{}) 95 enc, err := avro.NewEncoder(schema, buf) 96 assert.NoError(t, err) 97 var ts *TestTimestampPtr 98 99 err = enc.Encode(ts) 100 101 assert.NoError(t, err) 102 assert.Equal(t, []byte{0x00}, buf.Bytes()) 103 } 104 105 func TestEncoder_TextMarshalerError(t *testing.T) { 106 defer ConfigTeardown() 107 108 schema := "string" 109 buf := bytes.NewBuffer([]byte{}) 110 enc, err := avro.NewEncoder(schema, buf) 111 assert.NoError(t, err) 112 ts := TestTimestampError{} 113 114 err = enc.Encode(&ts) 115 116 assert.Error(t, err) 117 } 118 119 type TestTimestamp time.Time 120 121 func (t TestTimestamp) MarshalText() ([]byte, error) { 122 return (time.Time)(t).MarshalText() 123 } 124 125 type TestTimestampPtr time.Time 126 127 func (t *TestTimestampPtr) UnmarshalText(data []byte) error { 128 return (*time.Time)(t).UnmarshalText(data) 129 } 130 131 func (t *TestTimestampPtr) MarshalText() ([]byte, error) { 132 return (*time.Time)(t).MarshalText() 133 } 134 135 type TestTimestampError time.Time 136 137 func (t *TestTimestampError) UnmarshalText(data []byte) error { 138 return errors.New("test") 139 } 140 141 func (t *TestTimestampError) MarshalText() ([]byte, error) { 142 return nil, errors.New("test") 143 }