From c0a9fe96a773445c80a85c478e9a58ac481f3fed Mon Sep 17 00:00:00 2001 From: Carlos Peliciari Date: Tue, 11 Feb 2025 13:23:12 -0300 Subject: [PATCH] feat(attribute): add int32 support - Extend Value with Int32Value and Int32SliceValue. - Extend KeyValue with Int32 and Int32Slice. - Extend the Key type with Int32 and Int32Slice methods. --- attribute/key.go | 22 ++++++++++++++++++++++ attribute/kv.go | 10 ++++++++++ attribute/value.go | 18 ++++++++++++++++++ attribute/value_test.go | 20 ++++++++++++++++++++ 4 files changed, 70 insertions(+) diff --git a/attribute/key.go b/attribute/key.go index d9a22c65020..4b2833d4c02 100644 --- a/attribute/key.go +++ b/attribute/key.go @@ -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 diff --git a/attribute/kv.go b/attribute/kv.go index 3028f9a40f8..d4aadf43403 100644 --- a/attribute/kv.go +++ b/attribute/kv.go @@ -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) diff --git a/attribute/value.go b/attribute/value.go index 9ea0ecbbd27..1a037cdb25c 100644 --- a/attribute/value.go +++ b/attribute/value.go @@ -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{ diff --git a/attribute/value_test.go b/attribute/value_test.go index d1c88702d25..5a0b79a980d 100644 --- a/attribute/value_test.go +++ b/attribute/value_test.go @@ -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, @@ -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),