Skip to content

Commit

Permalink
add float32 type
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Jan 25, 2025
1 parent f8ad910 commit 44ccc1c
Show file tree
Hide file tree
Showing 66 changed files with 4,283 additions and 720 deletions.
2 changes: 1 addition & 1 deletion client/ctype.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (t CType) IsSupportedFieldCType() bool {
func (t CType) IsCompatibleWith(kind FieldKind) bool {
switch t {
case PN_COUNTER, P_COUNTER:
if kind == FieldKind_NILLABLE_INT || kind == FieldKind_NILLABLE_FLOAT {
if kind == FieldKind_NILLABLE_INT || kind == FieldKind_NILLABLE_FLOAT64 || kind == FieldKind_NILLABLE_FLOAT32 {
return true
}
return false
Expand Down
103 changes: 78 additions & 25 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ func validateFieldSchema(val any, field FieldDefinition) (NormalValue, error) {
return NewNormalString(v), nil

case FieldKind_STRING_ARRAY:
v, err := getArray(val, getString)
v, err := getArray(val, getString, field.Size)
if err != nil {
return nil, err
}
return NewNormalStringArray(v), nil

case FieldKind_NILLABLE_STRING_ARRAY:
v, err := getNillableArray(val, getString)
v, err := getNillableArray(val, getString, field.Size)
if err != nil {
return nil, err
}
Expand All @@ -295,39 +295,60 @@ func validateFieldSchema(val any, field FieldDefinition) (NormalValue, error) {
return NewNormalBool(v), nil

case FieldKind_BOOL_ARRAY:
v, err := getArray(val, getBool)
v, err := getArray(val, getBool, field.Size)
if err != nil {
return nil, err
}
return NewNormalBoolArray(v), nil

case FieldKind_NILLABLE_BOOL_ARRAY:
v, err := getNillableArray(val, getBool)
v, err := getNillableArray(val, getBool, field.Size)
if err != nil {
return nil, err
}
return NewNormalNillableBoolArray(v), nil

case FieldKind_NILLABLE_FLOAT:
case FieldKind_NILLABLE_FLOAT64:
v, err := getFloat64(val)
if err != nil {
return nil, err
}
return NewNormalFloat(v), nil
return NewNormalFloat64(v), nil

case FieldKind_FLOAT_ARRAY:
v, err := getArray(val, getFloat64)
case FieldKind_FLOAT64_ARRAY:
v, err := getArray(val, getFloat64, field.Size)
if err != nil {
return nil, err
}
return NewNormalFloatArray(v), nil
return NewNormalFloat64Array(v), nil

case FieldKind_NILLABLE_FLOAT_ARRAY:
v, err := getNillableArray(val, getFloat64)
case FieldKind_NILLABLE_FLOAT64_ARRAY:
v, err := getNillableArray(val, getFloat64, field.Size)
if err != nil {
return nil, err
}
return NewNormalNillableFloatArray(v), nil
return NewNormalNillableFloat64Array(v), nil

case FieldKind_NILLABLE_FLOAT32:
v, err := getFloat32(val)
if err != nil {
return nil, err
}
return NewNormalFloat32(v), nil

case FieldKind_FLOAT32_ARRAY:
v, err := getArray(val, getFloat32, field.Size)
if err != nil {
return nil, err
}
return NewNormalFloat32Array(v), nil

case FieldKind_NILLABLE_FLOAT32_ARRAY:
v, err := getNillableArray(val, getFloat32, field.Size)
if err != nil {
return nil, err
}
return NewNormalNillableFloat32Array(v), nil

case FieldKind_NILLABLE_DATETIME:
v, err := getDateTime(val)
Expand All @@ -344,14 +365,14 @@ func validateFieldSchema(val any, field FieldDefinition) (NormalValue, error) {
return NewNormalInt(v), nil

case FieldKind_INT_ARRAY:
v, err := getArray(val, getInt64)
v, err := getArray(val, getInt64, field.Size)
if err != nil {
return nil, err
}
return NewNormalIntArray(v), nil

case FieldKind_NILLABLE_INT_ARRAY:
v, err := getNillableArray(val, getInt64)
v, err := getNillableArray(val, getInt64, field.Size)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -397,13 +418,38 @@ func getFloat64(v any) (float64, error) {
return float64(val), nil
case int64:
return float64(val), nil
case float32:
return float64(val), nil
case float64:
return val, nil
default:
return 0, NewErrUnexpectedType[float64]("field", v)
}
}

func getFloat32(v any) (float32, error) {
switch val := v.(type) {
case *fastjson.Value:
f64, err := val.Float64()
if err != nil {
return 0, err
}
return float32(f64), nil
case int:
return float32(val), nil
case int32:
return float32(val), nil
case int64:
return float32(val), nil
case float32:
return val, nil
case float64:
return float32(val), nil
default:
return 0, NewErrUnexpectedType[float32]("field", v)
}
}

func getInt64(v any) (int64, error) {
switch val := v.(type) {
case *fastjson.Value:
Expand Down Expand Up @@ -441,7 +487,9 @@ func getDateTime(v any) (time.Time, error) {
func getArray[T any](
v any,
typeGetter func(any) (T, error),
size int,
) ([]T, error) {
array := []T{}
switch val := v.(type) {
case *fastjson.Value:
if val.Type() == fastjson.TypeNull {
Expand All @@ -463,8 +511,7 @@ func getArray[T any](
return nil, err
}
}

return arr, nil
array = arr
case []any:
arr := make([]T, len(val))
for i, arrItem := range val {
Expand All @@ -475,18 +522,22 @@ func getArray[T any](
}
}

return arr, nil
array = arr
case []T:
return val, nil
default:
return []T{}, nil
array = val
}
if size != 0 && len(array) != size {
return nil, NewErrArraySizeMismatch(array, size)
}
return array, nil
}

func getNillableArray[T any](
v any,
typeGetter func(any) (T, error),
size int,
) ([]immutable.Option[T], error) {
array := []immutable.Option[T]{}
switch val := v.(type) {
case *fastjson.Value:
if val.Type() == fastjson.TypeNull {
Expand All @@ -511,7 +562,7 @@ func getNillableArray[T any](
arr[i] = immutable.Some(v)
}

return arr, nil
array = arr
case []any:
arr := make([]immutable.Option[T], len(val))
for i, arrItem := range val {
Expand All @@ -526,12 +577,14 @@ func getNillableArray[T any](
arr[i] = immutable.Some(v)
}

return arr, nil
array = arr
case []immutable.Option[T]:
return val, nil
default:
return []immutable.Option[T]{}, nil
array = val
}
if size != 0 && len(array) != size {
return nil, NewErrArraySizeMismatch(array, size)
}
return array, nil
}

// Head returns the current head CID of the document.
Expand Down
33 changes: 25 additions & 8 deletions client/normal_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,28 @@ func (v normalIntArray) Equal(other NormalValue) bool {
return areNormalArraysEqual(v.val, other.IntArray)
}

type normalFloatArray struct {
type normalFloat64Array struct {
baseArrayNormalValue[[]float64]
}

func (v normalFloatArray) FloatArray() ([]float64, bool) {
func (v normalFloat64Array) Float64Array() ([]float64, bool) {
return v.val, true
}

func (v normalFloatArray) Equal(other NormalValue) bool {
return areNormalArraysEqual(v.val, other.FloatArray)
func (v normalFloat64Array) Equal(other NormalValue) bool {
return areNormalArraysEqual(v.val, other.Float64Array)
}

type normalFloat32Array struct {
baseArrayNormalValue[[]float32]
}

func (v normalFloat32Array) Float32Array() ([]float32, bool) {
return v.val, true
}

func (v normalFloat32Array) Equal(other NormalValue) bool {
return areNormalArraysEqual(v.val, other.Float32Array)
}

type normalStringArray struct {
Expand Down Expand Up @@ -142,9 +154,14 @@ func NewNormalIntArray[T constraints.Integer | constraints.Float](val []T) Norma
return normalIntArray{newBaseArrayNormalValue(normalizeNumArr[int64](val))}
}

// NewNormalFloatArray creates a new NormalValue that represents a `[]float64` value.
func NewNormalFloatArray[T constraints.Integer | constraints.Float](val []T) NormalValue {
return normalFloatArray{newBaseArrayNormalValue(normalizeNumArr[float64](val))}
// NewNormalFloat64Array creates a new NormalValue that represents a `[]float64` value.
func NewNormalFloat64Array[T constraints.Integer | constraints.Float](val []T) NormalValue {
return normalFloat64Array{newBaseArrayNormalValue(normalizeNumArr[float64](val))}
}

// NewNormalFloat32Array creates a new NormalValue that represents a `[]float32` value.
func NewNormalFloat32Array[T constraints.Integer | constraints.Float](val []T) NormalValue {
return normalFloat32Array{newBaseArrayNormalValue(normalizeNumArr[float32](val))}
}

// NewNormalStringArray creates a new NormalValue that represents a `[]string` value.
Expand Down Expand Up @@ -172,7 +189,7 @@ func NewNormalJSONArray(val []JSON) NormalValue {
return normalJSONArray{newBaseArrayNormalValue(val)}
}

func normalizeNumArr[R int64 | float64, T constraints.Integer | constraints.Float](val []T) []R {
func normalizeNumArr[R int64 | float64 | float32, T constraints.Integer | constraints.Float](val []T) []R {
var v any = val
if arr, ok := v.([]R); ok {
return arr
Expand Down
35 changes: 27 additions & 8 deletions client/normal_array_of_nillables.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,28 @@ func (v normalNillableIntArray) Equal(other NormalValue) bool {
return areNormalArraysOfNillablesEqual(v.val, other.NillableIntArray)
}

type normalNillableFloatArray struct {
type normalNillableFloat64Array struct {
baseArrayNormalValue[[]immutable.Option[float64]]
}

func (v normalNillableFloatArray) NillableFloatArray() ([]immutable.Option[float64], bool) {
func (v normalNillableFloat64Array) NillableFloat64Array() ([]immutable.Option[float64], bool) {
return v.val, true
}

func (v normalNillableFloatArray) Equal(other NormalValue) bool {
return areNormalArraysOfNillablesEqual(v.val, other.NillableFloatArray)
func (v normalNillableFloat64Array) Equal(other NormalValue) bool {
return areNormalArraysOfNillablesEqual(v.val, other.NillableFloat64Array)
}

type normalNillableFloat32Array struct {
baseArrayNormalValue[[]immutable.Option[float32]]
}

func (v normalNillableFloat32Array) NillableFloat32Array() ([]immutable.Option[float32], bool) {
return v.val, true
}

func (v normalNillableFloat32Array) Equal(other NormalValue) bool {
return areNormalArraysOfNillablesEqual(v.val, other.NillableFloat32Array)
}

type normalNillableStringArray struct {
Expand Down Expand Up @@ -116,11 +128,18 @@ func NewNormalNillableIntArray[T constraints.Integer | constraints.Float](val []
return normalNillableIntArray{newBaseArrayNormalValue(normalizeNillableNumArr[int64](val))}
}

// NewNormalNillableFloatArray creates a new NormalValue that represents a `[]immutable.Option[float64]` value.
func NewNormalNillableFloatArray[T constraints.Integer | constraints.Float](
// NewNormalNillableFloat64Array creates a new NormalValue that represents a `[]immutable.Option[float64]` value.
func NewNormalNillableFloat64Array[T constraints.Integer | constraints.Float](
val []immutable.Option[T],
) NormalValue {
return normalNillableFloat64Array{newBaseArrayNormalValue(normalizeNillableNumArr[float64](val))}
}

// NewNormalNillableFloat32Array creates a new NormalValue that represents a `[]immutable.Option[float32]` value.
func NewNormalNillableFloat32Array[T constraints.Integer | constraints.Float](
val []immutable.Option[T],
) NormalValue {
return normalNillableFloatArray{newBaseArrayNormalValue(normalizeNillableNumArr[float64](val))}
return normalNillableFloat32Array{newBaseArrayNormalValue(normalizeNillableNumArr[float32](val))}
}

// NewNormalNillableStringArray creates a new NormalValue that represents a `[]immutable.Option[string]` value.
Expand All @@ -143,7 +162,7 @@ func NewNormalNillableDocumentArray(val []immutable.Option[*Document]) NormalVal
return normalNillableDocumentArray{newBaseArrayNormalValue(val)}
}

func normalizeNillableNumArr[R int64 | float64, T constraints.Integer | constraints.Float](
func normalizeNillableNumArr[R int64 | float64 | float32, T constraints.Integer | constraints.Float](
val []immutable.Option[T],
) []immutable.Option[R] {
var v any = val
Expand Down
Loading

0 comments on commit 44ccc1c

Please sign in to comment.