diff --git a/pkg/primitive/int.go b/pkg/primitive/int.go index 94371a82..01428636 100644 --- a/pkg/primitive/int.go +++ b/pkg/primitive/int.go @@ -8,60 +8,71 @@ import ( ) type ( + // Integer is an interface representing an integer. Integer interface { Value Int() int64 } - // Int is a representation of a int. + + // Int is a representation of an int. Int int - // Int8 is a representation of a int8. + + // Int8 is a representation of an int8. Int8 int8 - // Int16 is a representation of a int16. + + // Int16 is a representation of an int16. Int16 int16 - // Int32 is a representation of a int32. + + // Int32 is a representation of an int32. Int32 int32 - // Int64 is a representation of a int64. + + // Int64 is a representation of an int64. Int64 int64 ) -var _ Integer = (Int)(0) -var _ Integer = (Int8)(0) -var _ Integer = (Int16)(0) -var _ Integer = (Int32)(0) -var _ Integer = (Int64)(0) +var ( + _ Integer = (Int)(0) + _ Integer = (Int8)(0) + _ Integer = (Int16)(0) + _ Integer = (Int32)(0) + _ Integer = (Int64)(0) +) // NewInt returns a new Int. func NewInt(value int) Int { return Int(value) } -// Int returns a raw representation. -func (o Int) Int() int64 { - return int64(o) +// Int returns the raw representation. +func (i Int) Int() int64 { + return int64(i) } -func (o Int) Kind() Kind { +// Kind returns the type of the int data. +func (i Int) Kind() Kind { return KindInt } -func (o Int) Compare(v Value) int { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return compare[int64](o.Int(), int64(r.Uint())) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Int()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[int64](o.Int(), r.Int()) +// Compare compares two Int values. +func (i Int) Compare(v Value) int { + if r, ok := v.(Integer); ok { + return compare[int64](i.Int(), r.Int()) } + if r, ok := v.(Uinteger); ok { + return compare[int64](i.Int(), int64(r.Uint())) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(i.Int()), r.Float()) + } + if i.Kind() > v.Kind() { + return 1 + } + return -1 } -func (o Int) Interface() any { - return int(o) +// Interface converts Int to an int. +func (i Int) Interface() any { + return int(i) } // NewInt8 returns a new Int8. @@ -69,33 +80,36 @@ func NewInt8(value int8) Int8 { return Int8(value) } -// Int returns a raw representation. -func (o Int8) Int() int64 { - return int64(o) +// Int returns the raw representation. +func (i Int8) Int() int64 { + return int64(i) } -func (o Int8) Kind() Kind { +// Kind returns the type of the int8 data. +func (i Int8) Kind() Kind { return KindInt8 } -func (o Int8) Compare(v Value) int { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return compare[int64](o.Int(), int64(r.Uint())) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Int()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[int64](o.Int(), r.Int()) +// Compare compares two Int8 values. +func (i Int8) Compare(v Value) int { + if r, ok := v.(Integer); ok { + return compare[int64](i.Int(), r.Int()) + } + if r, ok := v.(Uinteger); ok { + return compare[int64](i.Int(), int64(r.Uint())) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(i.Int()), r.Float()) + } + if i.Kind() > v.Kind() { + return 1 } + return -1 } -func (o Int8) Interface() any { - return int8(o) +// Interface converts Int8 to an int8. +func (i Int8) Interface() any { + return int8(i) } // NewInt16 returns a new Int16. @@ -103,33 +117,36 @@ func NewInt16(value int16) Int16 { return Int16(value) } -// Int returns a raw representation. -func (o Int16) Int() int64 { - return int64(o) +// Int returns the raw representation. +func (i Int16) Int() int64 { + return int64(i) } -func (o Int16) Kind() Kind { +// Kind returns the type of the int16 data. +func (i Int16) Kind() Kind { return KindInt16 } -func (o Int16) Compare(v Value) int { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return compare[int64](o.Int(), int64(r.Uint())) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Int()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[int64](o.Int(), r.Int()) +// Compare compares two Int16 values. +func (i Int16) Compare(v Value) int { + if r, ok := v.(Integer); ok { + return compare[int64](i.Int(), r.Int()) + } + if r, ok := v.(Uinteger); ok { + return compare[int64](i.Int(), int64(r.Uint())) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(i.Int()), r.Float()) + } + if i.Kind() > v.Kind() { + return 1 } + return -1 } -func (o Int16) Interface() any { - return int16(o) +// Interface converts Int16 to an int16. +func (i Int16) Interface() any { + return int16(i) } // NewInt32 returns a new Int32. @@ -137,33 +154,36 @@ func NewInt32(value int32) Int32 { return Int32(value) } -// Int returns a raw representation. -func (o Int32) Int() int64 { - return int64(o) +// Int returns the raw representation. +func (i Int32) Int() int64 { + return int64(i) } -func (o Int32) Kind() Kind { +// Kind returns the type of the int32 data. +func (i Int32) Kind() Kind { return KindInt32 } -func (o Int32) Compare(v Value) int { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return compare[int64](o.Int(), int64(r.Uint())) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Int()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[int64](o.Int(), r.Int()) +// Compare compares two Int32 values. +func (i Int32) Compare(v Value) int { + if r, ok := v.(Integer); ok { + return compare[int64](i.Int(), r.Int()) + } + if r, ok := v.(Uinteger); ok { + return compare[int64](i.Int(), int64(r.Uint())) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(i.Int()), r.Float()) } + if i.Kind() > v.Kind() { + return 1 + } + return -1 } -func (o Int32) Interface() any { - return int32(o) +// Interface converts Int32 to an int32. +func (i Int32) Interface() any { + return int32(i) } // NewInt64 returns a new Int64. @@ -171,83 +191,97 @@ func NewInt64(value int64) Int64 { return Int64(value) } -// Int returns a raw representation. -func (o Int64) Int() int64 { - return int64(o) +// Int returns the raw representation. +func (i Int64) Int() int64 { + return int64(i) } -func (o Int64) Kind() Kind { +// Kind returns the type of the int64 data. +func (i Int64) Kind() Kind { return KindInt64 } -func (o Int64) Compare(v Value) int { - if r, ok := v.(Integer); !ok { - if r, ok := v.(Uinteger); ok { - return compare[int64](o.Int(), int64(r.Uint())) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Int()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[int64](o.Int(), r.Int()) +// Compare compares two Int64 values. +func (i Int64) Compare(v Value) int { + if r, ok := v.(Integer); ok { + return compare[int64](i.Int(), r.Int()) + } + if r, ok := v.(Uinteger); ok { + return compare[int64](i.Int(), int64(r.Uint())) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(i.Int()), r.Float()) } + if i.Kind() > v.Kind() { + return 1 + } + return -1 } -func (o Int64) Interface() any { - return int64(o) +// Interface converts Int64 to an int64. +func (i Int64) Interface() any { + return int64(i) } -// NewIntEncoder is encode int to Int. +// NewIntEncoder encodes int to Int. func NewIntEncoder() encoding.Encoder[any, Value] { return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { - if s := reflect.ValueOf(source); s.Kind() == reflect.Int { + switch s := reflect.ValueOf(source); s.Kind() { + case reflect.Int: return NewInt(int(s.Int())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Int8 { + case reflect.Int8: return NewInt8(int8(s.Int())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Int16 { + case reflect.Int16: return NewInt16(int16(s.Int())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Int32 { + case reflect.Int32: return NewInt32(int32(s.Int())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Int64 { + case reflect.Int64: return NewInt64(int64(s.Int())), nil } return nil, errors.WithStack(encoding.ErrUnsupportedValue) }) } -// NewIntDecoder is decode Int to int. +// NewIntDecoder decodes Int to int. func NewIntDecoder() encoding.Decoder[Value, any] { return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(Integer); ok { - if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { - if t.Elem().Kind() == reflect.Int { + if t := reflect.ValueOf(target); t.Kind() == reflect.Ptr { + switch t.Elem().Kind() { + case reflect.Float32: + t.Elem().Set(reflect.ValueOf(float32(s.Int()))) + case reflect.Float64: + t.Elem().Set(reflect.ValueOf(s.Int())) + case reflect.Int: t.Elem().Set(reflect.ValueOf(int(s.Int()))) - return nil - } else if t.Elem().Kind() == reflect.Int8 { + case reflect.Int8: t.Elem().Set(reflect.ValueOf(int8(s.Int()))) - return nil - } else if t.Elem().Kind() == reflect.Int16 { + case reflect.Int16: t.Elem().Set(reflect.ValueOf(int16(s.Int()))) - return nil - } else if t.Elem().Kind() == reflect.Int32 { + case reflect.Int32: t.Elem().Set(reflect.ValueOf(int32(s.Int()))) - return nil - } else if t.Elem().Kind() == reflect.Int64 { - t.Elem().Set(reflect.ValueOf(int64(s.Int()))) - return nil - } else if t.Elem().Kind() == reflect.Float32 { - t.Elem().Set(reflect.ValueOf(float32(s.Int()))) - return nil - } else if t.Elem().Kind() == reflect.Float64 { - t.Elem().Set(reflect.ValueOf(float64(s.Int()))) - return nil - } else if t.Elem().Type() == typeAny { - t.Elem().Set(reflect.ValueOf(s.Interface())) - return nil + case reflect.Int64: + t.Elem().Set(reflect.ValueOf(s.Int())) + case reflect.Uint: + t.Elem().Set(reflect.ValueOf(uint(s.Int()))) + case reflect.Uint8: + t.Elem().Set(reflect.ValueOf(uint8(s.Int()))) + case reflect.Uint16: + t.Elem().Set(reflect.ValueOf(uint16(s.Int()))) + case reflect.Uint32: + t.Elem().Set(reflect.ValueOf(uint32(s.Int()))) + case reflect.Uint64: + t.Elem().Set(reflect.ValueOf(uint64(s.Int()))) + case reflect.Bool: + t.Elem().Set(reflect.ValueOf(s.Int() != 0)) + default: + if t.Type() == typeAny { + t.Elem().Set(reflect.ValueOf(s.Interface())) + } else { + return errors.WithStack(encoding.ErrUnsupportedValue) + } } + return nil } } return errors.WithStack(encoding.ErrUnsupportedValue) diff --git a/pkg/primitive/int_test.go b/pkg/primitive/int_test.go index 30c1e866..c15cbe58 100644 --- a/pkg/primitive/int_test.go +++ b/pkg/primitive/int_test.go @@ -6,65 +6,68 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewInt(t *testing.T) { - t.Run("", func(t *testing.T) { +func TestNewInteger(t *testing.T) { + t.Run("Int", func(t *testing.T) { v := NewInt(0) - assert.Equal(t, KindInt, v.Kind()) assert.Equal(t, int(0), v.Interface()) }) - t.Run("8", func(t *testing.T) { - v := NewInt8(0) + t.Run("Int8", func(t *testing.T) { + v := NewInt8(0) assert.Equal(t, KindInt8, v.Kind()) assert.Equal(t, int8(0), v.Interface()) }) - t.Run("16", func(t *testing.T) { - v := NewInt16(0) + t.Run("Int16", func(t *testing.T) { + v := NewInt16(0) assert.Equal(t, KindInt16, v.Kind()) assert.Equal(t, int16(0), v.Interface()) }) - t.Run("32", func(t *testing.T) { - v := NewInt32(0) + t.Run("Int32", func(t *testing.T) { + v := NewInt32(0) assert.Equal(t, KindInt32, v.Kind()) assert.Equal(t, int32(0), v.Interface()) }) - t.Run("64", func(t *testing.T) { - v := NewInt64(0) + t.Run("Int64", func(t *testing.T) { + v := NewInt64(0) assert.Equal(t, KindInt64, v.Kind()) assert.Equal(t, int64(0), v.Interface()) }) } -func TestInt_Compare(t *testing.T) { - t.Run("", func(t *testing.T) { +func TestInteger_Compare(t *testing.T) { + t.Run("Int", func(t *testing.T) { assert.Equal(t, 0, NewInt(0).Compare(NewInt(0))) assert.Equal(t, 1, NewInt(1).Compare(NewInt(0))) assert.Equal(t, -1, NewInt(0).Compare(NewInt(1))) assert.Equal(t, 0, NewInt(0).Compare(NewFloat32(0))) }) - t.Run("8", func(t *testing.T) { + + t.Run("Int8", func(t *testing.T) { assert.Equal(t, 0, NewInt8(0).Compare(NewInt(0))) assert.Equal(t, 1, NewInt8(1).Compare(NewInt(0))) assert.Equal(t, -1, NewInt8(0).Compare(NewInt(1))) assert.Equal(t, 0, NewInt8(0).Compare(NewFloat32(0))) }) - t.Run("16", func(t *testing.T) { + + t.Run("Int16", func(t *testing.T) { assert.Equal(t, 0, NewInt16(0).Compare(NewInt(0))) assert.Equal(t, 1, NewInt16(1).Compare(NewInt(0))) assert.Equal(t, -1, NewInt16(0).Compare(NewInt(1))) assert.Equal(t, 0, NewInt16(0).Compare(NewFloat32(0))) }) - t.Run("32", func(t *testing.T) { + + t.Run("Int32", func(t *testing.T) { assert.Equal(t, 0, NewInt32(0).Compare(NewInt(0))) assert.Equal(t, 1, NewInt32(1).Compare(NewInt(0))) assert.Equal(t, -1, NewInt32(0).Compare(NewInt(1))) assert.Equal(t, 0, NewInt32(0).Compare(NewFloat32(0))) }) - t.Run("64", func(t *testing.T) { + + t.Run("Int64", func(t *testing.T) { assert.Equal(t, 0, NewInt64(0).Compare(NewInt(0))) assert.Equal(t, 1, NewInt64(1).Compare(NewInt(0))) assert.Equal(t, -1, NewInt64(0).Compare(NewInt(1))) @@ -72,67 +75,72 @@ func TestInt_Compare(t *testing.T) { }) } -func TestInt_Encode(t *testing.T) { +func TestInteger_EncodeAndDecode(t *testing.T) { e := NewIntEncoder() + d := NewIntDecoder() - t.Run("", func(t *testing.T) { - v, err := e.Encode(int(1)) - assert.NoError(t, err) - assert.Equal(t, NewInt(1), v) - }) - t.Run("8", func(t *testing.T) { - v, err := e.Encode(int8(1)) + t.Run("Int", func(t *testing.T) { + source := int(1) + + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, NewInt8(1), v) - }) - t.Run("16", func(t *testing.T) { - v, err := e.Encode(int16(1)) + assert.Equal(t, Int(1), encoded) + + var decoded int + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, NewInt16(1), v) + assert.Equal(t, source, decoded) }) - t.Run("32", func(t *testing.T) { - v, err := e.Encode(int32(1)) + + t.Run("Int8", func(t *testing.T) { + source := int8(1) + + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, NewInt32(1), v) - }) - t.Run("64", func(t *testing.T) { - v, err := e.Encode(int64(1)) + assert.Equal(t, Int8(1), encoded) + + var decoded int8 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, NewInt64(1), v) + assert.Equal(t, source, decoded) }) -} -func TestInt_Decode(t *testing.T) { - d := NewIntDecoder() + t.Run("Int16", func(t *testing.T) { + source := int16(1) - t.Run("", func(t *testing.T) { - var v int - err := d.Decode(NewInt(1), &v) + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, int(1), v) - }) - t.Run("8", func(t *testing.T) { - var v int8 - err := d.Decode(NewInt8(1), &v) + assert.Equal(t, Int16(1), encoded) + + var decoded int16 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, int8(1), v) + assert.Equal(t, source, decoded) }) - t.Run("16", func(t *testing.T) { - var v int16 - err := d.Decode(NewInt16(1), &v) + + t.Run("Int32", func(t *testing.T) { + source := int32(1) + + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, int16(1), v) - }) - t.Run("32", func(t *testing.T) { - var v int32 - err := d.Decode(NewInt32(1), &v) + assert.Equal(t, Int32(1), encoded) + + var decoded int32 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, int32(1), v) + assert.Equal(t, source, decoded) }) - t.Run("64", func(t *testing.T) { - var v int64 - err := d.Decode(NewInt64(1), &v) + + t.Run("Int64", func(t *testing.T) { + source := int64(1) + + encoded, err := e.Encode(source) + assert.NoError(t, err) + assert.Equal(t, Int64(1), encoded) + + var decoded int64 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, int64(1), v) + assert.Equal(t, source, decoded) }) } diff --git a/pkg/primitive/uint.go b/pkg/primitive/uint.go index 9c8b43fb..4ab5b422 100644 --- a/pkg/primitive/uint.go +++ b/pkg/primitive/uint.go @@ -8,60 +8,71 @@ import ( ) type ( + // Uinteger is an interface representing an unsigned integer. Uinteger interface { Value Uint() uint64 } + // Uint is a representation of a uint. Uint uint + // Uint8 is a representation of a uint8. Uint8 uint8 + // Uint16 is a representation of a uint16. Uint16 uint16 + // Uint32 is a representation of a uint32. Uint32 uint32 + // Uint64 is a representation of a uint64. Uint64 uint64 ) -var _ Uinteger = (Uint)(0) -var _ Uinteger = (Uint8)(0) -var _ Uinteger = (Uint16)(0) -var _ Uinteger = (Uint32)(0) -var _ Uinteger = (Uint64)(0) +var ( + _ Uinteger = (Uint)(0) + _ Uinteger = (Uint8)(0) + _ Uinteger = (Uint16)(0) + _ Uinteger = (Uint32)(0) + _ Uinteger = (Uint64)(0) +) // NewUint returns a new Uint. func NewUint(value uint) Uint { return Uint(value) } -// Uint returns a raw representation. -func (o Uint) Uint() uint64 { - return uint64(o) +// Uint returns the raw representation. +func (u Uint) Uint() uint64 { + return uint64(u) } -func (o Uint) Kind() Kind { +// Kind returns the type of the uint data. +func (u Uint) Kind() Kind { return KindUint } -func (o Uint) Compare(v Value) int { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return compare[int64](int64(o.Uint()), r.Int()) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Uint()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[uint64](o.Uint(), r.Uint()) +// Compare compares two Uint values. +func (u Uint) Compare(v Value) int { + if r, ok := v.(Uinteger); ok { + return compare[uint64](u.Uint(), r.Uint()) } + if r, ok := v.(Integer); ok { + return compare[int64](int64(u.Uint()), r.Int()) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(u.Uint()), r.Float()) + } + if u.Kind() > v.Kind() { + return 1 + } + return -1 } -func (o Uint) Interface() any { - return uint(o) +// Interface converts Uint to a uint. +func (u Uint) Interface() any { + return uint(u) } // NewUint8 returns a new Uint8. @@ -69,33 +80,36 @@ func NewUint8(value uint8) Uint8 { return Uint8(value) } -// Uint returns a raw representation. -func (o Uint8) Uint() uint64 { - return uint64(o) +// Uint returns the raw representation. +func (u Uint8) Uint() uint64 { + return uint64(u) } -func (o Uint8) Kind() Kind { +// Kind returns the type of the uint8 data. +func (u Uint8) Kind() Kind { return KindUint8 } -func (o Uint8) Compare(v Value) int { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return compare[int64](int64(o.Uint()), r.Int()) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Uint()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[uint64](o.Uint(), r.Uint()) +// Compare compares two Uint8 values. +func (u Uint8) Compare(v Value) int { + if r, ok := v.(Uinteger); ok { + return compare[uint64](u.Uint(), r.Uint()) + } + if r, ok := v.(Integer); ok { + return compare[int64](int64(u.Uint()), r.Int()) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(u.Uint()), r.Float()) } + if u.Kind() > v.Kind() { + return 1 + } + return -1 } -func (o Uint8) Interface() any { - return uint8(o) +// Interface converts Uint8 to a uint8. +func (u Uint8) Interface() any { + return uint8(u) } // NewUint16 returns a new Uint16. @@ -103,33 +117,36 @@ func NewUint16(value uint16) Uint16 { return Uint16(value) } -// Uint returns a raw representation. -func (o Uint16) Uint() uint64 { - return uint64(o) +// Uint returns the raw representation. +func (u Uint16) Uint() uint64 { + return uint64(u) } -func (o Uint16) Kind() Kind { +// Kind returns the type of the uint16 data. +func (u Uint16) Kind() Kind { return KindUint16 } -func (o Uint16) Compare(v Value) int { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return compare[int64](int64(o.Uint()), r.Int()) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Uint()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[uint64](o.Uint(), r.Uint()) +// Compare compares two Uint16 values. +func (u Uint16) Compare(v Value) int { + if r, ok := v.(Uinteger); ok { + return compare[uint64](u.Uint(), r.Uint()) + } + if r, ok := v.(Integer); ok { + return compare[int64](int64(u.Uint()), r.Int()) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(u.Uint()), r.Float()) } + if u.Kind() > v.Kind() { + return 1 + } + return -1 } -func (o Uint16) Interface() any { - return uint16(o) +// Interface converts Uint16 to a uint16. +func (u Uint16) Interface() any { + return uint16(u) } // NewUint32 returns a new Uint32. @@ -137,33 +154,36 @@ func NewUint32(value uint32) Uint32 { return Uint32(value) } -// Uint returns a raw representation. -func (o Uint32) Uint() uint64 { - return uint64(o) +// Uint returns the raw representation. +func (u Uint32) Uint() uint64 { + return uint64(u) } -func (o Uint32) Kind() Kind { +// Kind returns the type of the uint32 data. +func (u Uint32) Kind() Kind { return KindUint32 } -func (o Uint32) Compare(v Value) int { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return compare[int64](int64(o.Uint()), r.Int()) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Uint()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[uint64](o.Uint(), r.Uint()) +// Compare compares two Uint32 values. +func (u Uint32) Compare(v Value) int { + if r, ok := v.(Uinteger); ok { + return compare[uint64](u.Uint(), r.Uint()) + } + if r, ok := v.(Integer); ok { + return compare[int64](int64(u.Uint()), r.Int()) } + if r, ok := v.(Float); ok { + return compare[float64](float64(u.Uint()), r.Float()) + } + if u.Kind() > v.Kind() { + return 1 + } + return -1 } -func (o Uint32) Interface() any { - return uint32(o) +// Interface converts Uint32 to a uint32. +func (u Uint32) Interface() any { + return uint32(u) } // NewUint64 returns a new Uint64. @@ -171,77 +191,97 @@ func NewUint64(value uint64) Uint64 { return Uint64(value) } -// Uint returns a raw representation. -func (o Uint64) Uint() uint64 { - return uint64(o) +// Uint returns the raw representation. +func (u Uint64) Uint() uint64 { + return uint64(u) } -func (o Uint64) Kind() Kind { +// Kind returns the type of the uint64 data. +func (u Uint64) Kind() Kind { return KindUint64 } -func (o Uint64) Compare(v Value) int { - if r, ok := v.(Uinteger); !ok { - if r, ok := v.(Integer); ok { - return compare[int64](int64(o.Uint()), r.Int()) - } else if r, ok := v.(Float); ok { - return compare[float64](float64(o.Uint()), r.Float()) - } else if o.Kind() > v.Kind() { - return 1 - } else { - return -1 - } - } else { - return compare[uint64](o.Uint(), r.Uint()) +// Compare compares two Uint64 values. +func (u Uint64) Compare(v Value) int { + if r, ok := v.(Uinteger); ok { + return compare[uint64](u.Uint(), r.Uint()) + } + if r, ok := v.(Integer); ok { + return compare[int64](int64(u.Uint()), r.Int()) + } + if r, ok := v.(Float); ok { + return compare[float64](float64(u.Uint()), r.Float()) + } + if u.Kind() > v.Kind() { + return 1 } + return -1 } -func (o Uint64) Interface() any { - return uint64(o) +// Interface converts Uint64 to a uint64. +func (u Uint64) Interface() any { + return uint64(u) } -// NewUintEncoder is encode uint to Uint. +// NewUintEncoder encodes uint to Uint. func NewUintEncoder() encoding.Encoder[any, Value] { return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { - if s := reflect.ValueOf(source); s.Kind() == reflect.Uint { + switch s := reflect.ValueOf(source); s.Kind() { + case reflect.Uint: return NewUint(uint(s.Uint())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Uint8 { + case reflect.Uint8: return NewUint8(uint8(s.Uint())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Uint16 { + case reflect.Uint16: return NewUint16(uint16(s.Uint())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Uint32 { + case reflect.Uint32: return NewUint32(uint32(s.Uint())), nil - } else if s := reflect.ValueOf(source); s.Kind() == reflect.Uint64 { + case reflect.Uint64: return NewUint64(uint64(s.Uint())), nil } return nil, errors.WithStack(encoding.ErrUnsupportedValue) }) } -// NewUintDecoder is decode Uint to uint. +// NewUintDecoder decodes Uint to uint. func NewUintDecoder() encoding.Decoder[Value, any] { return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(Uinteger); ok { - if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { - if t.Elem().Kind() == reflect.Uint { + if t := reflect.ValueOf(target); t.Kind() == reflect.Ptr { + switch t.Elem().Kind() { + case reflect.Float32: + t.Elem().Set(reflect.ValueOf(float32(s.Uint()))) + case reflect.Float64: + t.Elem().Set(reflect.ValueOf(s.Uint())) + case reflect.Int: + t.Elem().Set(reflect.ValueOf(int(s.Uint()))) + case reflect.Int8: + t.Elem().Set(reflect.ValueOf(int8(s.Uint()))) + case reflect.Int16: + t.Elem().Set(reflect.ValueOf(int16(s.Uint()))) + case reflect.Int32: + t.Elem().Set(reflect.ValueOf(int32(s.Uint()))) + case reflect.Int64: + t.Elem().Set(reflect.ValueOf(int32(s.Uint()))) + case reflect.Uint: t.Elem().Set(reflect.ValueOf(uint(s.Uint()))) - return nil - } else if t.Elem().Kind() == reflect.Uint8 { + case reflect.Uint8: t.Elem().Set(reflect.ValueOf(uint8(s.Uint()))) - return nil - } else if t.Elem().Kind() == reflect.Uint16 { + case reflect.Uint16: t.Elem().Set(reflect.ValueOf(uint16(s.Uint()))) - return nil - } else if t.Elem().Kind() == reflect.Uint32 { + case reflect.Uint32: t.Elem().Set(reflect.ValueOf(uint32(s.Uint()))) - return nil - } else if t.Elem().Kind() == reflect.Uint64 { + case reflect.Uint64: t.Elem().Set(reflect.ValueOf(uint64(s.Uint()))) - return nil - } else if t.Elem().Type() == typeAny { - t.Elem().Set(reflect.ValueOf(s.Interface())) - return nil + case reflect.Bool: + t.Elem().Set(reflect.ValueOf(s.Uint() != 0)) + default: + if t.Type() == typeAny { + t.Elem().Set(reflect.ValueOf(s.Interface())) + } else { + return errors.WithStack(encoding.ErrUnsupportedValue) + } } + return nil } } return errors.WithStack(encoding.ErrUnsupportedValue) diff --git a/pkg/primitive/uint_test.go b/pkg/primitive/uint_test.go index c871acdf..130004b8 100644 --- a/pkg/primitive/uint_test.go +++ b/pkg/primitive/uint_test.go @@ -6,65 +6,68 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewUint(t *testing.T) { - t.Run("", func(t *testing.T) { +func TestUinteger(t *testing.T) { + t.Run("Uint", func(t *testing.T) { v := NewUint(0) - assert.Equal(t, KindUint, v.Kind()) assert.Equal(t, uint(0), v.Interface()) }) - t.Run("8", func(t *testing.T) { - v := NewUint8(0) + t.Run("Uint8", func(t *testing.T) { + v := NewUint8(0) assert.Equal(t, KindUint8, v.Kind()) assert.Equal(t, uint8(0), v.Interface()) }) - t.Run("16", func(t *testing.T) { - v := NewUint16(0) + t.Run("Uint16", func(t *testing.T) { + v := NewUint16(0) assert.Equal(t, KindUint16, v.Kind()) assert.Equal(t, uint16(0), v.Interface()) }) - t.Run("32", func(t *testing.T) { - v := NewUint32(0) + t.Run("Uint32", func(t *testing.T) { + v := NewUint32(0) assert.Equal(t, KindUint32, v.Kind()) assert.Equal(t, uint32(0), v.Interface()) }) - t.Run("64", func(t *testing.T) { - v := NewUint64(0) + t.Run("Uint64", func(t *testing.T) { + v := NewUint64(0) assert.Equal(t, KindUint64, v.Kind()) assert.Equal(t, uint64(0), v.Interface()) }) } -func TestUint_Compare(t *testing.T) { - t.Run("", func(t *testing.T) { +func TestUinteger_Compare(t *testing.T) { + t.Run("Uint", func(t *testing.T) { assert.Equal(t, 0, NewUint(0).Compare(NewUint(0))) assert.Equal(t, 1, NewUint(1).Compare(NewUint(0))) assert.Equal(t, -1, NewUint(0).Compare(NewUint(1))) assert.Equal(t, 0, NewUint(0).Compare(NewFloat32(0))) }) - t.Run("8", func(t *testing.T) { + + t.Run("Uint8", func(t *testing.T) { assert.Equal(t, 0, NewUint8(0).Compare(NewUint(0))) assert.Equal(t, 1, NewUint8(1).Compare(NewUint(0))) assert.Equal(t, -1, NewUint8(0).Compare(NewUint(1))) assert.Equal(t, 0, NewUint8(0).Compare(NewFloat32(0))) }) - t.Run("16", func(t *testing.T) { + + t.Run("Uint16", func(t *testing.T) { assert.Equal(t, 0, NewUint16(0).Compare(NewUint(0))) assert.Equal(t, 1, NewUint16(1).Compare(NewUint(0))) assert.Equal(t, -1, NewUint16(0).Compare(NewUint(1))) assert.Equal(t, 0, NewUint16(0).Compare(NewFloat32(0))) }) - t.Run("32", func(t *testing.T) { + + t.Run("Uint32", func(t *testing.T) { assert.Equal(t, 0, NewUint32(0).Compare(NewUint(0))) assert.Equal(t, 1, NewUint32(1).Compare(NewUint(0))) assert.Equal(t, -1, NewUint32(0).Compare(NewUint(1))) assert.Equal(t, 0, NewUint32(0).Compare(NewFloat32(0))) }) - t.Run("64", func(t *testing.T) { + + t.Run("Uint64", func(t *testing.T) { assert.Equal(t, 0, NewUint64(0).Compare(NewUint(0))) assert.Equal(t, 1, NewUint64(1).Compare(NewUint(0))) assert.Equal(t, -1, NewUint64(0).Compare(NewUint(1))) @@ -72,67 +75,72 @@ func TestUint_Compare(t *testing.T) { }) } -func TestUint_Encode(t *testing.T) { +func TestUinteger_EncodeAndDecode(t *testing.T) { e := NewUintEncoder() + d := NewUintDecoder() - t.Run("", func(t *testing.T) { - v, err := e.Encode(uint(1)) - assert.NoError(t, err) - assert.Equal(t, NewUint(1), v) - }) - t.Run("8", func(t *testing.T) { - v, err := e.Encode(uint8(1)) + t.Run("Uint", func(t *testing.T) { + source := uint(1) + + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, NewUint8(1), v) - }) - t.Run("16", func(t *testing.T) { - v, err := e.Encode(uint16(1)) + assert.Equal(t, Uint(1), encoded) + + var decoded uint + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, NewUint16(1), v) + assert.Equal(t, source, decoded) }) - t.Run("32", func(t *testing.T) { - v, err := e.Encode(uint32(1)) + + t.Run("Uint8", func(t *testing.T) { + source := uint8(1) + + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, NewUint32(1), v) - }) - t.Run("64", func(t *testing.T) { - v, err := e.Encode(uint64(1)) + assert.Equal(t, Uint8(1), encoded) + + var decoded uint8 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, NewUint64(1), v) + assert.Equal(t, source, decoded) }) -} -func TestUint_Decode(t *testing.T) { - d := NewUintDecoder() + t.Run("Uint16", func(t *testing.T) { + source := uint16(1) - t.Run("", func(t *testing.T) { - var v uint - err := d.Decode(NewUint(1), &v) + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, uint(1), v) - }) - t.Run("8", func(t *testing.T) { - var v uint8 - err := d.Decode(NewUint8(1), &v) + assert.Equal(t, Uint16(1), encoded) + + var decoded uint16 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, uint8(1), v) + assert.Equal(t, source, decoded) }) - t.Run("16", func(t *testing.T) { - var v uint16 - err := d.Decode(NewUint16(1), &v) + + t.Run("Uint32", func(t *testing.T) { + source := uint32(1) + + encoded, err := e.Encode(source) assert.NoError(t, err) - assert.Equal(t, uint16(1), v) - }) - t.Run("32", func(t *testing.T) { - var v uint32 - err := d.Decode(NewUint32(1), &v) + assert.Equal(t, Uint32(1), encoded) + + var decoded uint32 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, uint32(1), v) + assert.Equal(t, source, decoded) }) - t.Run("64", func(t *testing.T) { - var v uint64 - err := d.Decode(NewUint64(1), &v) + + t.Run("Uint64", func(t *testing.T) { + source := uint64(1) + + encoded, err := e.Encode(source) + assert.NoError(t, err) + assert.Equal(t, Uint64(1), encoded) + + var decoded uint64 + err = d.Decode(encoded, &decoded) assert.NoError(t, err) - assert.Equal(t, uint64(1), v) + assert.Equal(t, source, decoded) }) }