|
| 1 | +package y3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + "github.com/yomorun/y3-codec-golang/internal/utils" |
| 9 | +) |
| 10 | + |
| 11 | +// basicEncoder is a Encoder for BasicTestData data types |
| 12 | +type basicEncoder interface { |
| 13 | + // Encode: encode interface to bytes |
| 14 | + Encode(input interface{}, signals ...*signal) (buf []byte, err error) |
| 15 | +} |
| 16 | + |
| 17 | +// basicEncoderImpl is implementation of the basicEncoder interface |
| 18 | +type basicEncoderImpl struct { |
| 19 | + observe byte |
| 20 | + root byte |
| 21 | +} |
| 22 | + |
| 23 | +// basicEncoderOption create basicEncoderImpl with option |
| 24 | +type basicEncoderOption func(*basicEncoderImpl) |
| 25 | + |
| 26 | +// basicEncoderOptionRoot set root value for creating basicEncoderImpl |
| 27 | +func basicEncoderOptionRoot(root byte) basicEncoderOption { |
| 28 | + return func(b *basicEncoderImpl) { |
| 29 | + b.root = root |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// newBasicEncoder create a basicEncoder interface |
| 34 | +func newBasicEncoder(observe byte, options ...func(*basicEncoderImpl)) basicEncoder { |
| 35 | + if utils.ForbiddenCustomizedKey(observe) { |
| 36 | + panic(fmt.Errorf("prohibit the use of this key: %#x", observe)) |
| 37 | + } |
| 38 | + |
| 39 | + encoder := &basicEncoderImpl{observe: observe, root: utils.EmptyKey} |
| 40 | + |
| 41 | + for _, option := range options { |
| 42 | + option(encoder) |
| 43 | + } |
| 44 | + |
| 45 | + return encoder |
| 46 | +} |
| 47 | + |
| 48 | +// Encode encode interface{} to bytes |
| 49 | +func (e *basicEncoderImpl) Encode(input interface{}, signals ...*signal) (buf []byte, err error) { |
| 50 | + encoders := make([]*PrimitivePacketEncoder, 0) |
| 51 | + for _, signal := range signals { |
| 52 | + encoders = append(encoders, signal.ToEncoder()) |
| 53 | + } |
| 54 | + return e.encodeBasic(input, encoders) |
| 55 | +} |
| 56 | + |
| 57 | +// encodeBasic encode interface{} to bytes, and inserting signals |
| 58 | +func (e *basicEncoderImpl) encodeBasic(input interface{}, signals []*PrimitivePacketEncoder) ([]byte, error) { |
| 59 | + if e.observe == 0 { |
| 60 | + panic(fmt.Errorf("observe cannot be 0")) |
| 61 | + } |
| 62 | + |
| 63 | + var primitiveEncoder *PrimitivePacketEncoder |
| 64 | + |
| 65 | + value := reflect.ValueOf(input) |
| 66 | + switch value.Kind() { |
| 67 | + case reflect.String: |
| 68 | + primitiveEncoder = e.encodeBasicString(input) |
| 69 | + case reflect.Int32: |
| 70 | + primitiveEncoder = e.encodeBasicInt32(input) |
| 71 | + case reflect.Uint32: |
| 72 | + primitiveEncoder = e.encodeBasicUint32(input) |
| 73 | + case reflect.Int64: |
| 74 | + primitiveEncoder = e.encodeBasicInt64(input) |
| 75 | + case reflect.Uint64: |
| 76 | + primitiveEncoder = e.encodeBasicUint64(input) |
| 77 | + case reflect.Float32: |
| 78 | + primitiveEncoder = e.encodeBasicFloat32(input) |
| 79 | + case reflect.Float64: |
| 80 | + primitiveEncoder = e.encodeBasicFloat64(input) |
| 81 | + case reflect.Bool: |
| 82 | + primitiveEncoder = e.encodeBasicBool(input) |
| 83 | + case reflect.Array, reflect.Slice: |
| 84 | + //e.marshalBasicSlice(value, e.root) |
| 85 | + return e.encodeBasicSlice(value, signals) |
| 86 | + default: |
| 87 | + panic(fmt.Errorf("marshal error, no matching type: %v", value.Kind())) |
| 88 | + } |
| 89 | + |
| 90 | + if primitiveEncoder == nil { |
| 91 | + panic("PrimitivePacketEncoder is nil") |
| 92 | + } |
| 93 | + |
| 94 | + if !utils.IsEmptyKey(e.root) { |
| 95 | + root := NewNodePacketEncoder(int(e.root)) |
| 96 | + for _, signal := range signals { |
| 97 | + root.AddPrimitivePacket(signal) |
| 98 | + } |
| 99 | + root.AddPrimitivePacket(primitiveEncoder) |
| 100 | + return root.Encode(), nil |
| 101 | + } else { |
| 102 | + buf := make([][]byte, 0) |
| 103 | + for _, signal := range signals { |
| 104 | + buf = append(buf, signal.Encode()) |
| 105 | + } |
| 106 | + buf = append(buf, primitiveEncoder.Encode()) |
| 107 | + return bytes.Join(buf, []byte{}), nil |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// encodeBasicSlice encode reflect.Value of slice, and inserting signals |
| 112 | +func (e *basicEncoderImpl) encodeBasicSlice(value reflect.Value, signals []*PrimitivePacketEncoder) ([]byte, error) { |
| 113 | + if value.Len() == 0 { |
| 114 | + return nil, fmt.Errorf("no item is slice") |
| 115 | + } |
| 116 | + |
| 117 | + var nodeEncoder *NodePacketEncoder |
| 118 | + |
| 119 | + switch value.Index(0).Kind() { |
| 120 | + case reflect.String: |
| 121 | + nodeEncoder = e.encodeBasicStringSlice(value) |
| 122 | + case reflect.Int32: |
| 123 | + nodeEncoder = e.encodeBasicInt32Slice(value) |
| 124 | + case reflect.Uint32: |
| 125 | + nodeEncoder = e.encodeBasicUint32Slice(value) |
| 126 | + case reflect.Int64: |
| 127 | + nodeEncoder = e.encodeBasicInt64Slice(value) |
| 128 | + case reflect.Uint64: |
| 129 | + nodeEncoder = e.encodeBasicUint64Slice(value) |
| 130 | + case reflect.Float32: |
| 131 | + nodeEncoder = e.encodeBasicFloat32Slice(value) |
| 132 | + case reflect.Float64: |
| 133 | + nodeEncoder = e.encodeBasicFloat64Slice(value) |
| 134 | + case reflect.Bool: |
| 135 | + nodeEncoder = e.encodeBasicBoolSlice(value) |
| 136 | + default: |
| 137 | + panic(fmt.Errorf("marshal error, no matching type in SliceTestData: %v", value.Index(0).Kind())) |
| 138 | + } |
| 139 | + |
| 140 | + if nodeEncoder == nil { |
| 141 | + panic("NodePacketEncoder is nil") |
| 142 | + } |
| 143 | + |
| 144 | + if !utils.IsEmptyKey(e.root) { |
| 145 | + root := NewNodePacketEncoder(int(e.root)) |
| 146 | + for _, signal := range signals { |
| 147 | + root.AddPrimitivePacket(signal) |
| 148 | + } |
| 149 | + root.AddNodePacket(nodeEncoder) |
| 150 | + return root.Encode(), nil |
| 151 | + } else { |
| 152 | + buf := make([][]byte, 0) |
| 153 | + for _, signal := range signals { |
| 154 | + buf = append(buf, signal.Encode()) |
| 155 | + } |
| 156 | + buf = append(buf, nodeEncoder.Encode()) |
| 157 | + return bytes.Join(buf, []byte{}), nil |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// encodeBasicString encode string to PrimitivePacketEncoder |
| 162 | +func (e *basicEncoderImpl) encodeBasicString(input interface{}) *PrimitivePacketEncoder { |
| 163 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 164 | + encoder.SetStringValue(fmt.Sprintf("%v", input)) |
| 165 | + return encoder |
| 166 | +} |
| 167 | + |
| 168 | +// encodeBasicInt32 encode int32 to PrimitivePacketEncoder |
| 169 | +func (e *basicEncoderImpl) encodeBasicInt32(input interface{}) *PrimitivePacketEncoder { |
| 170 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 171 | + encoder.SetInt32Value(input.(int32)) |
| 172 | + return encoder |
| 173 | +} |
| 174 | + |
| 175 | +// encodeBasicUint32 encode uint32 to PrimitivePacketEncoder |
| 176 | +func (e *basicEncoderImpl) encodeBasicUint32(input interface{}) *PrimitivePacketEncoder { |
| 177 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 178 | + encoder.SetUInt32Value(input.(uint32)) |
| 179 | + return encoder |
| 180 | +} |
| 181 | + |
| 182 | +// encodeBasicInt64 encode int64 to PrimitivePacketEncoder |
| 183 | +func (e *basicEncoderImpl) encodeBasicInt64(input interface{}) *PrimitivePacketEncoder { |
| 184 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 185 | + encoder.SetInt64Value(input.(int64)) |
| 186 | + return encoder |
| 187 | +} |
| 188 | + |
| 189 | +// encodeBasicUint64 encode uint64 to PrimitivePacketEncoder |
| 190 | +func (e *basicEncoderImpl) encodeBasicUint64(input interface{}) *PrimitivePacketEncoder { |
| 191 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 192 | + encoder.SetUInt64Value(input.(uint64)) |
| 193 | + return encoder |
| 194 | +} |
| 195 | + |
| 196 | +// encodeBasicFloat32 encode float32 to PrimitivePacketEncoder |
| 197 | +func (e *basicEncoderImpl) encodeBasicFloat32(input interface{}) *PrimitivePacketEncoder { |
| 198 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 199 | + encoder.SetFloat32Value(input.(float32)) |
| 200 | + return encoder |
| 201 | +} |
| 202 | + |
| 203 | +// encodeBasicFloat64 encode float64 to PrimitivePacketEncoder |
| 204 | +func (e *basicEncoderImpl) encodeBasicFloat64(input interface{}) *PrimitivePacketEncoder { |
| 205 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 206 | + encoder.SetFloat64Value(input.(float64)) |
| 207 | + return encoder |
| 208 | +} |
| 209 | + |
| 210 | +// encodeBasicBool encode bool to PrimitivePacketEncoder |
| 211 | +func (e *basicEncoderImpl) encodeBasicBool(input interface{}) *PrimitivePacketEncoder { |
| 212 | + var encoder = NewPrimitivePacketEncoder(int(e.observe)) |
| 213 | + encoder.SetBoolValue(input.(bool)) |
| 214 | + return encoder |
| 215 | +} |
| 216 | + |
| 217 | +// encodeBasicStringSlice encode reflect.Value of []string to NodePacketEncoder |
| 218 | +func (e *basicEncoderImpl) encodeBasicStringSlice(value reflect.Value) *NodePacketEncoder { |
| 219 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 220 | + if out, ok := utils.ToStringSliceArray(value.Interface()); ok { |
| 221 | + for _, v := range out { |
| 222 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 223 | + item.SetStringValue(fmt.Sprintf("%v", v)) |
| 224 | + node.AddPrimitivePacket(item) |
| 225 | + } |
| 226 | + } |
| 227 | + return node |
| 228 | +} |
| 229 | + |
| 230 | +// encodeBasicInt32Slice encode reflect.Value of []int32 to NodePacketEncoder |
| 231 | +func (e *basicEncoderImpl) encodeBasicInt32Slice(value reflect.Value) *NodePacketEncoder { |
| 232 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 233 | + if out, ok := utils.ToInt64SliceArray(value.Interface()); ok { |
| 234 | + for _, v := range out { |
| 235 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 236 | + item.SetInt32Value(int32(v.(int64))) |
| 237 | + node.AddPrimitivePacket(item) |
| 238 | + } |
| 239 | + } |
| 240 | + return node |
| 241 | +} |
| 242 | + |
| 243 | +// encodeBasicUint32Slice encode reflect.Value of []uint32 to NodePacketEncoder |
| 244 | +func (e *basicEncoderImpl) encodeBasicUint32Slice(value reflect.Value) *NodePacketEncoder { |
| 245 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 246 | + if out, ok := utils.ToUInt64SliceArray(value.Interface()); ok { |
| 247 | + for _, v := range out { |
| 248 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 249 | + item.SetUInt32Value(uint32(v.(uint64))) |
| 250 | + node.AddPrimitivePacket(item) |
| 251 | + } |
| 252 | + } |
| 253 | + return node |
| 254 | +} |
| 255 | + |
| 256 | +// encodeBasicInt64Slice encode reflect.Value of []int64 to NodePacketEncoder |
| 257 | +func (e *basicEncoderImpl) encodeBasicInt64Slice(value reflect.Value) *NodePacketEncoder { |
| 258 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 259 | + if out, ok := utils.ToInt64SliceArray(value.Interface()); ok { |
| 260 | + for _, v := range out { |
| 261 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 262 | + item.SetInt64Value(v.(int64)) |
| 263 | + node.AddPrimitivePacket(item) |
| 264 | + } |
| 265 | + } |
| 266 | + return node |
| 267 | +} |
| 268 | + |
| 269 | +// encodeBasicUint64Slice encode reflect.Value of []uint64 to NodePacketEncoder |
| 270 | +func (e *basicEncoderImpl) encodeBasicUint64Slice(value reflect.Value) *NodePacketEncoder { |
| 271 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 272 | + if out, ok := utils.ToUInt64SliceArray(value.Interface()); ok { |
| 273 | + for _, v := range out { |
| 274 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 275 | + item.SetUInt64Value(v.(uint64)) |
| 276 | + node.AddPrimitivePacket(item) |
| 277 | + } |
| 278 | + } |
| 279 | + return node |
| 280 | +} |
| 281 | + |
| 282 | +// encodeBasicFloat32Slice encode reflect.Value of []float32 to NodePacketEncoder |
| 283 | +func (e *basicEncoderImpl) encodeBasicFloat32Slice(value reflect.Value) *NodePacketEncoder { |
| 284 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 285 | + if out, ok := utils.ToUFloat64SliceArray(value.Interface()); ok { |
| 286 | + for _, v := range out { |
| 287 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 288 | + item.SetFloat32Value(float32(v.(float64))) |
| 289 | + node.AddPrimitivePacket(item) |
| 290 | + } |
| 291 | + } |
| 292 | + return node |
| 293 | +} |
| 294 | + |
| 295 | +// encodeBasicFloat64Slice encode reflect.Value of []float64 to NodePacketEncoder |
| 296 | +func (e *basicEncoderImpl) encodeBasicFloat64Slice(value reflect.Value) *NodePacketEncoder { |
| 297 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 298 | + if out, ok := utils.ToUFloat64SliceArray(value.Interface()); ok { |
| 299 | + for _, v := range out { |
| 300 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 301 | + item.SetFloat64Value(v.(float64)) |
| 302 | + node.AddPrimitivePacket(item) |
| 303 | + } |
| 304 | + } |
| 305 | + return node |
| 306 | +} |
| 307 | + |
| 308 | +// encodeBasicBoolSlice encode reflect.Value of []bool to NodePacketEncoder |
| 309 | +func (e *basicEncoderImpl) encodeBasicBoolSlice(value reflect.Value) *NodePacketEncoder { |
| 310 | + var node = NewNodeArrayPacketEncoder(int(e.observe)) |
| 311 | + if out, ok := utils.ToBoolSliceArray(value.Interface()); ok { |
| 312 | + for _, v := range out { |
| 313 | + var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem) |
| 314 | + item.SetBoolValue(v.(bool)) |
| 315 | + node.AddPrimitivePacket(item) |
| 316 | + } |
| 317 | + } |
| 318 | + return node |
| 319 | +} |
0 commit comments