You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-07-15 01:04:25 +02:00
Shrink core.Value (#256)
* shrink the value type went down from 40 bytes to 24 * add missing license blurb * stringify value type * print string value types in stdout exporter * make Value function take a pointer receiver
This commit is contained in:
@ -9,265 +9,114 @@ import (
|
||||
"go.opentelemetry.io/api/core"
|
||||
)
|
||||
|
||||
func TestBool(t *testing.T) {
|
||||
func TestValue(t *testing.T) {
|
||||
k := core.Key("test")
|
||||
bli := getBitlessInfo(42)
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v bool
|
||||
want core.Value
|
||||
name string
|
||||
value core.Value
|
||||
wantType core.ValueType
|
||||
wantValue interface{}
|
||||
}{
|
||||
{
|
||||
name: "Key.Bool() correctly returns keys's internal bool value",
|
||||
v: true,
|
||||
want: core.Value{
|
||||
Type: core.BOOL,
|
||||
Bool: true,
|
||||
},
|
||||
name: "Key.Bool() correctly returns keys's internal bool value",
|
||||
value: k.Bool(true).Value,
|
||||
wantType: core.BOOL,
|
||||
wantValue: true,
|
||||
},
|
||||
{
|
||||
name: "Key.Int64() correctly returns keys's internal int64 value",
|
||||
value: k.Int64(42).Value,
|
||||
wantType: core.INT64,
|
||||
wantValue: int64(42),
|
||||
},
|
||||
{
|
||||
name: "Key.Uint64() correctly returns keys's internal uint64 value",
|
||||
value: k.Uint64(42).Value,
|
||||
wantType: core.UINT64,
|
||||
wantValue: uint64(42),
|
||||
},
|
||||
{
|
||||
name: "Key.Float64() correctly returns keys's internal float64 value",
|
||||
value: k.Float64(42.1).Value,
|
||||
wantType: core.FLOAT64,
|
||||
wantValue: float64(42.1),
|
||||
},
|
||||
{
|
||||
name: "Key.Int32() correctly returns keys's internal int32 value",
|
||||
value: k.Int32(42).Value,
|
||||
wantType: core.INT32,
|
||||
wantValue: int32(42),
|
||||
},
|
||||
{
|
||||
name: "Key.Uint32() correctly returns keys's internal uint32 value",
|
||||
value: k.Uint32(42).Value,
|
||||
wantType: core.UINT32,
|
||||
wantValue: uint32(42),
|
||||
},
|
||||
{
|
||||
name: "Key.Float32() correctly returns keys's internal float32 value",
|
||||
value: k.Float32(42.1).Value,
|
||||
wantType: core.FLOAT32,
|
||||
wantValue: float32(42.1),
|
||||
},
|
||||
{
|
||||
name: "Key.String() correctly returns keys's internal string value",
|
||||
value: k.String("foo").Value,
|
||||
wantType: core.STRING,
|
||||
wantValue: "foo",
|
||||
},
|
||||
{
|
||||
name: "Key.Int() correctly returns keys's internal signed integral value",
|
||||
value: k.Int(bli.intValue).Value,
|
||||
wantType: bli.signedType,
|
||||
wantValue: bli.signedValue,
|
||||
},
|
||||
{
|
||||
name: "Key.Uint() correctly returns keys's internal unsigned integral value",
|
||||
value: k.Uint(bli.uintValue).Value,
|
||||
wantType: bli.unsignedType,
|
||||
wantValue: bli.unsignedValue,
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) core.Bool(v bool) KeyValue {}
|
||||
have := core.Key("").Bool(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
t.Logf("Running test case %s", testcase.name)
|
||||
if testcase.value.Type() != testcase.wantType {
|
||||
t.Errorf("wrong value type, got %#v, expected %#v", testcase.value.Type(), testcase.wantType)
|
||||
}
|
||||
got := testcase.value.AsInterface()
|
||||
if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
|
||||
t.Errorf("+got, -want: %s", diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v int64
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.Int64() correctly returns keys's internal int64 value",
|
||||
v: int64(42),
|
||||
want: core.Value{
|
||||
Type: core.INT64,
|
||||
Int64: int64(42),
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Int64(v int64) KeyValue {
|
||||
have := core.Key("").Int64(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
type bitlessInfo struct {
|
||||
intValue int
|
||||
uintValue uint
|
||||
signedType core.ValueType
|
||||
unsignedType core.ValueType
|
||||
signedValue interface{}
|
||||
unsignedValue interface{}
|
||||
}
|
||||
|
||||
func TestUint64(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v uint64
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.Uint64() correctly returns keys's internal uint64 value",
|
||||
v: uint64(42),
|
||||
want: core.Value{
|
||||
Type: core.UINT64,
|
||||
Uint64: uint64(42),
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Uint64(v uint64) KeyValue {
|
||||
have := core.Key("").Uint64(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
func getBitlessInfo(i int) bitlessInfo {
|
||||
if unsafe.Sizeof(i) == 4 {
|
||||
return bitlessInfo{
|
||||
intValue: i,
|
||||
uintValue: uint(i),
|
||||
signedType: core.INT32,
|
||||
unsignedType: core.UINT32,
|
||||
signedValue: int32(i),
|
||||
unsignedValue: uint32(i),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v float64
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.float64() correctly returns keys's internal floa64 value",
|
||||
v: float64(42.1),
|
||||
want: core.Value{
|
||||
Type: core.FLOAT64,
|
||||
Float64: float64(42.1),
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Float64(v float64) KeyValue {
|
||||
have := core.Key("").Float64(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt32(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v int32
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.int32() correctly returns keys's internal int32 value",
|
||||
v: int32(42),
|
||||
want: core.Value{
|
||||
Type: core.INT32,
|
||||
Int64: int64(42),
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Int32(v int32) KeyValue {
|
||||
have := core.Key("").Int32(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint32(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v uint32
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.uint32() correctly returns keys's internal uint32 value",
|
||||
v: uint32(42),
|
||||
want: core.Value{
|
||||
Type: core.UINT32,
|
||||
Uint64: uint64(42),
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Uint32(v uint32) KeyValue {
|
||||
have := core.Key("").Uint32(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat32(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v float32
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.float32() correctly returns keys's internal float32 value",
|
||||
v: float32(42.0),
|
||||
want: core.Value{
|
||||
Type: core.FLOAT32,
|
||||
Float64: float64(42.0),
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Float32(v float32) KeyValue {
|
||||
have := core.Key("").Float32(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v string
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.String() correctly returns keys's internal string value",
|
||||
v: "foo",
|
||||
want: core.Value{
|
||||
Type: core.STRING,
|
||||
String: "foo",
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) String(v string) KeyValue {
|
||||
have := core.Key("").String(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt(t *testing.T) {
|
||||
WTYPE := core.INT64
|
||||
if unsafe.Sizeof(int(42)) == 4 {
|
||||
// switch the desired value-type depending on system int byte-size
|
||||
WTYPE = core.INT32
|
||||
}
|
||||
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v int
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.Int() correctly returns keys's internal int64 value",
|
||||
v: int(42),
|
||||
want: core.Value{
|
||||
Type: WTYPE,
|
||||
Int64: int64(42),
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Int(v int) KeyValue {
|
||||
have := core.Key("").Int(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint(t *testing.T) {
|
||||
WTYPE := core.UINT64
|
||||
if unsafe.Sizeof(uint(42)) == 4 {
|
||||
// switch the desired value-type depending on system int byte-size
|
||||
WTYPE = core.UINT32
|
||||
}
|
||||
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
v uint
|
||||
want core.Value
|
||||
}{
|
||||
{
|
||||
name: "Key.Uint() correctly returns keys's internal uint64 value",
|
||||
v: uint(42),
|
||||
want: core.Value{
|
||||
Type: WTYPE,
|
||||
Uint64: 42,
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
//proto: func (k core.Key) Uint(v uint) KeyValue {
|
||||
have := core.Key("").Uint(testcase.v)
|
||||
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
return bitlessInfo{
|
||||
intValue: i,
|
||||
uintValue: uint(i),
|
||||
signedType: core.INT64,
|
||||
unsignedType: core.UINT64,
|
||||
signedValue: int64(i),
|
||||
unsignedValue: uint64(i),
|
||||
}
|
||||
}
|
||||
|
||||
@ -306,66 +155,42 @@ func TestEmit(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.BOOL`,
|
||||
v: core.Value{
|
||||
Type: core.BOOL,
|
||||
Bool: true,
|
||||
},
|
||||
v: core.Bool(true),
|
||||
want: "true",
|
||||
},
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.INT32`,
|
||||
v: core.Value{
|
||||
Type: core.INT32,
|
||||
Int64: 42,
|
||||
},
|
||||
v: core.Int32(42),
|
||||
want: "42",
|
||||
},
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.INT64`,
|
||||
v: core.Value{
|
||||
Type: core.INT64,
|
||||
Int64: 42,
|
||||
},
|
||||
v: core.Int64(42),
|
||||
want: "42",
|
||||
},
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.UINT32`,
|
||||
v: core.Value{
|
||||
Type: core.UINT32,
|
||||
Uint64: 42,
|
||||
},
|
||||
v: core.Uint32(42),
|
||||
want: "42",
|
||||
},
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.UINT64`,
|
||||
v: core.Value{
|
||||
Type: core.UINT64,
|
||||
Uint64: 42,
|
||||
},
|
||||
v: core.Uint64(42),
|
||||
want: "42",
|
||||
},
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.FLOAT32`,
|
||||
v: core.Value{
|
||||
Type: core.FLOAT32,
|
||||
Float64: 42.1,
|
||||
},
|
||||
v: core.Float32(42.1),
|
||||
want: "42.1",
|
||||
},
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.FLOAT64`,
|
||||
v: core.Value{
|
||||
Type: core.FLOAT64,
|
||||
Float64: 42.1,
|
||||
},
|
||||
v: core.Float64(42.1),
|
||||
want: "42.1",
|
||||
},
|
||||
{
|
||||
name: `test Key.Emit() can emit a string representing self.STRING`,
|
||||
v: core.Value{
|
||||
Type: core.STRING,
|
||||
String: "foo",
|
||||
},
|
||||
v: core.String("foo"),
|
||||
want: "foo",
|
||||
},
|
||||
} {
|
||||
|
Reference in New Issue
Block a user