lzaeh commented on code in PR #2760: URL: https://github.com/apache/fory/pull/2760#discussion_r2445520383
########## go/fory/primitive_array.go: ########## @@ -0,0 +1,450 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package fory + +import ( + "fmt" + "reflect" +) + +type boolArraySerializer struct { +} + +func (s boolArraySerializer) TypeId() TypeId { + return BOOL_ARRAY +} + +func (s boolArraySerializer) NeedWriteRef() bool { + return true +} + +func (s boolArraySerializer) Write(f *Fory, buf *ByteBuffer, value reflect.Value) error { + if value.Kind() == reflect.Interface { + value = value.Elem() + } + size := value.Len() + if size >= MaxInt32 { + return fmt.Errorf("too long slice: %d", size) + } + buf.WriteLength(size) + for i := 0; i < size; i++ { + buf.WriteBool(value.Index(i).Bool()) + } + return nil +} + +func (s boolArraySerializer) Read(f *Fory, buf *ByteBuffer, type_ reflect.Type, value reflect.Value) error { + length := buf.ReadLength() + var r reflect.Value + switch type_.Kind() { + case reflect.Slice: + if type_.Elem().Kind() != reflect.Bool { + return fmt.Errorf("element kind must be bool, got %v", type_.Elem()) + } + r = reflect.MakeSlice(type_, length, length) + + case reflect.Array: + if type_.Elem().Kind() != reflect.Bool { + return fmt.Errorf("element kind must be bool, got %v", type_.Elem()) + } + if length != type_.Len() { + return fmt.Errorf("length %d does not match array type %v", length, type_) + } + r = reflect.New(type_).Elem() + + case reflect.Interface: + arrT := reflect.ArrayOf(length, reflect.TypeOf(true)) // [length]bool + r = reflect.New(arrT).Elem() + + default: + return fmt.Errorf("unsupported kind %v, want slice/array/interface", type_.Kind()) + } + + for i := 0; i < length; i++ { + b := buf.ReadBool() + r.Index(i).SetBool(b) + } + + value.Set(r) + return nil +} + +type int8ArraySerializer struct { +} + +func (s int8ArraySerializer) TypeId() TypeId { + return INT8_ARRAY +} + +func (s int8ArraySerializer) NeedWriteRef() bool { + return true +} + +func (s int8ArraySerializer) Write(f *Fory, buf *ByteBuffer, value reflect.Value) error { + if value.Kind() == reflect.Interface { + value = value.Elem() + } + size := value.Len() + if size >= MaxInt32 { + return fmt.Errorf("too long slice: %d", size) + } + buf.WriteLength(size) + for i := 0; i < size; i++ { + buf.WriteByte_(byte(value.Index(i).Int())) + } + return nil +} + +func (s int8ArraySerializer) Read(f *Fory, buf *ByteBuffer, type_ reflect.Type, value reflect.Value) error { + length := buf.ReadLength() + var r reflect.Value + switch type_.Kind() { + case reflect.Slice: + if type_.Elem().Kind() != reflect.Int8 { + return fmt.Errorf("element kind must be bool, got %v", type_.Elem()) + } + r = reflect.MakeSlice(type_, length, length) + + case reflect.Array: + if type_.Elem().Kind() != reflect.Int8 { + return fmt.Errorf("element kind must be bool, got %v", type_.Elem()) + } + if length != type_.Len() { + return fmt.Errorf("length %d does not match array type %v", length, type_) + } + r = reflect.New(type_).Elem() + + case reflect.Interface: + arrT := reflect.ArrayOf(length, reflect.TypeOf(int8(0))) // [length]int8 Review Comment: > we can't use `[n]T` for deserialization, the `n` used by user is fixed at compile-time, but the array length can be different. We should use slice `[]numeric` for primitive array type in fory type system. > > For golang serialization, we just don't allow `[n]T` being deserialized. Or only allow `[n]T` being serialized, but this seems useless. If you can't deserialzie it, serialize it seems unmeaningful too Sorry for the delay—I just wrapped up my graduation stuff. If all arrays are deserialized into slices, should arrays inside structs also be deserialized as slices? If so, I think the tests will need to be updated as well. I was considering this while planning comprehensive tests for the contents of commonArray.@chaokunyang -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
