Skip to content

Commit

Permalink
feat(attribute): add int32 support
Browse files Browse the repository at this point in the history
- Extend Value with Int32Value and Int32SliceValue.
- Extend KeyValue with Int32 and Int32Slice.
- Extend the Key type with Int32 and Int32Slice methods.
  • Loading branch information
cpeliciari committed Feb 11, 2025
1 parent 1971f5f commit c0a9fe9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions attribute/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ func (k Key) IntSlice(v []int) KeyValue {
}
}

// Int32 creates a KeyValue instance with an INT64 Value.
//
// If creating both a key and value at the same time, use the provided
// convenience function instead -- Int32(name, value).
func (k Key) Int32(v int32) KeyValue {
return KeyValue{
Key: k,
Value: Int32Value(v),
}
}

// Int32Slice creates a KeyValue instance with an INT64SLICE Value.
//
// If creating both a key and value at the same time, use the provided
// convenience function instead -- Int32Slice(name, value).
func (k Key) Int32Slice(v []int32) KeyValue {
return KeyValue{
Key: k,
Value: Int32SliceValue(v),
}
}

// Int64 creates a KeyValue instance with an INT64 Value.
//
// If creating both a key and value at the same time, use the provided
Expand Down
10 changes: 10 additions & 0 deletions attribute/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func IntSlice(k string, v []int) KeyValue {
return Key(k).IntSlice(v)
}

// Int32 creates a KeyValue with an INT64 Value type.
func Int32(k string, v int32) KeyValue {
return Key(k).Int32(v)
}

// Int32Slice creates a KeyValue with an INT64 Value type.
func Int32Slice(k string, v []int32) KeyValue {
return Key(k).Int32Slice(v)
}

// Int64 creates a KeyValue with an INT64 Value type.
func Int64(k string, v int64) KeyValue {
return Key(k).Int64(v)
Expand Down
18 changes: 18 additions & 0 deletions attribute/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ func IntSliceValue(v []int) Value {
}
}

// Int32Value creates an INT32 Value.
func Int32Value(v int32) Value {
return Int64Value(int64(v))
}

// Int32SliceValue creates an INT32SLICE Value.
func Int32SliceValue(v []int32) Value {
var int64Val int64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val)))
for i, val := range v {
cp.Elem().Index(i).SetInt(int64(val))
}
return Value{
vtype: INT64SLICE,
slice: cp.Elem().Interface(),
}
}

// Int64Value creates an INT64 Value.
func Int64Value(v int64) Value {
return Value{
Expand Down
20 changes: 20 additions & 0 deletions attribute/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ func TestValue(t *testing.T) {
wantType: attribute.INT64SLICE,
wantValue: []int64{42, -3, 12},
},
{
name: "Key.Int32() correctly returns keys's internal signed integral value",
value: k.Int32(42).Value,
wantType: attribute.INT64,
wantValue: int64(42),
},
{
name: "Key.Int32Slice() correctly returns keys's internal []int32 value",
value: k.Int32Slice([]int32{42, -3, 12}).Value,
wantType: attribute.INT64SLICE,
wantValue: []int64{42, -3, 12},
},
{
name: "Key.Float64() correctly returns keys's internal float64 value",
value: k.Float64(42.1).Value,
Expand Down Expand Up @@ -119,6 +131,14 @@ func TestEquivalence(t *testing.T) {
attribute.IntSlice("IntSlice", []int{312, 1, -2}),
attribute.IntSlice("IntSlice", []int{312, 1, -2}),
},
{
attribute.Int32("Int32", 44),
attribute.Int32("Int32", 44),
},
{
attribute.Int32Slice("Int32Slice", []int32{212, 21, -2}),
attribute.Int32Slice("Int32Slice", []int32{212, 21, -2}),
},
{
attribute.Int64("Int64", 98),
attribute.Int64("Int64", 98),
Expand Down

0 comments on commit c0a9fe9

Please sign in to comment.