Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(attribute): add int32 support #6299

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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