summaryrefslogtreecommitdiff
path: root/vendor/github.com/hamba/avro/v2/codec_enum.go
blob: 65ab453545e3ba78a18329523791a1e054b69dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package avro

import (
	"encoding"
	"errors"
	"fmt"
	"reflect"
	"unsafe"

	"github.com/modern-go/reflect2"
)

func createDecoderOfEnum(schema *EnumSchema, typ reflect2.Type) ValDecoder {
	switch {
	case typ.Kind() == reflect.String:
		return &enumCodec{enum: schema}
	case typ.Implements(textUnmarshalerType):
		return &enumTextMarshalerCodec{typ: typ, enum: schema}
	case reflect2.PtrTo(typ).Implements(textUnmarshalerType):
		return &enumTextMarshalerCodec{typ: typ, enum: schema, ptr: true}
	}

	return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}

func createEncoderOfEnum(schema *EnumSchema, typ reflect2.Type) ValEncoder {
	switch {
	case typ.Kind() == reflect.String:
		return &enumCodec{enum: schema}
	case typ.Implements(textMarshalerType):
		return &enumTextMarshalerCodec{typ: typ, enum: schema}
	case reflect2.PtrTo(typ).Implements(textMarshalerType):
		return &enumTextMarshalerCodec{typ: typ, enum: schema, ptr: true}
	}

	return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}

type enumCodec struct {
	enum *EnumSchema
}

func (c *enumCodec) Decode(ptr unsafe.Pointer, r *Reader) {
	i := int(r.ReadInt())

	symbol, ok := c.enum.Symbol(i)
	if !ok {
		r.ReportError("decode enum symbol", "unknown enum symbol")
		return
	}

	*((*string)(ptr)) = symbol
}

func (c *enumCodec) Encode(ptr unsafe.Pointer, w *Writer) {
	str := *((*string)(ptr))
	for i, sym := range c.enum.symbols {
		if str != sym {
			continue
		}

		w.WriteInt(int32(i))
		return
	}

	w.Error = fmt.Errorf("avro: unknown enum symbol: %s", str)
}

type enumTextMarshalerCodec struct {
	typ  reflect2.Type
	enum *EnumSchema
	ptr  bool
}

func (c *enumTextMarshalerCodec) Decode(ptr unsafe.Pointer, r *Reader) {
	i := int(r.ReadInt())

	symbol, ok := c.enum.Symbol(i)
	if !ok {
		r.ReportError("decode enum symbol", "unknown enum symbol")
		return
	}

	var obj any
	if c.ptr {
		obj = c.typ.PackEFace(ptr)
	} else {
		obj = c.typ.UnsafeIndirect(ptr)
	}
	if reflect2.IsNil(obj) {
		ptrType := c.typ.(*reflect2.UnsafePtrType)
		newPtr := ptrType.Elem().UnsafeNew()
		*((*unsafe.Pointer)(ptr)) = newPtr
		obj = c.typ.UnsafeIndirect(ptr)
	}
	unmarshaler := (obj).(encoding.TextUnmarshaler)
	if err := unmarshaler.UnmarshalText([]byte(symbol)); err != nil {
		r.ReportError("decode enum text unmarshaler", err.Error())
	}
}

func (c *enumTextMarshalerCodec) Encode(ptr unsafe.Pointer, w *Writer) {
	var obj any
	if c.ptr {
		obj = c.typ.PackEFace(ptr)
	} else {
		obj = c.typ.UnsafeIndirect(ptr)
	}
	if c.typ.IsNullable() && reflect2.IsNil(obj) {
		w.Error = errors.New("encoding nil enum text marshaler")
		return
	}
	marshaler := (obj).(encoding.TextMarshaler)
	b, err := marshaler.MarshalText()
	if err != nil {
		w.Error = err
		return
	}

	str := string(b)
	for i, sym := range c.enum.symbols {
		if str != sym {
			continue
		}

		w.WriteInt(int32(i))
		return
	}

	w.Error = fmt.Errorf("avro: unknown enum symbol: %s", str)
}