forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change key to a string alias and KeyValue constructors (#217)
* change key to a string alias and KeyValue constructors * fix int and uint tests
- Loading branch information
1 parent
75562dd
commit 5e409de
Showing
15 changed files
with
288 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package key_test | ||
|
||
import ( | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"go.opentelemetry.io/api/core" | ||
"go.opentelemetry.io/api/key" | ||
) | ||
|
||
func TestKeyValueConstructors(t *testing.T) { | ||
|
||
tt := []struct { | ||
name string | ||
actual core.KeyValue | ||
expected core.KeyValue | ||
}{ | ||
{ | ||
name: "Bool", | ||
actual: key.Bool("k1", true), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.BOOL, | ||
Bool: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Int64", | ||
actual: key.Int64("k1", 123), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.INT64, | ||
Int64: 123, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Uint64", | ||
actual: key.Uint64("k1", 1), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.UINT64, | ||
Uint64: 1, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Float64", | ||
actual: key.Float64("k1", 123.5), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.FLOAT64, | ||
Float64: 123.5, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Int32", | ||
actual: key.Int32("k1", 123), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.INT32, | ||
Int64: 123, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Uint32", | ||
actual: key.Uint32("k1", 123), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.UINT32, | ||
Uint64: 123, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Float32", | ||
actual: key.Float32("k1", 123.5), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.FLOAT32, | ||
Float64: 123.5, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Bytes", | ||
actual: key.Bytes("k1", []byte("v1")), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Type: core.BYTES, | ||
Bytes: []byte("v1"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Int", | ||
actual: key.Int("k1", 123), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Int64: 123, | ||
Type: IntType(123), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Uint", | ||
actual: key.Uint("k1", 123), | ||
expected: core.KeyValue{ | ||
Key: "k1", | ||
Value: core.Value{ | ||
Uint64: 123, | ||
Type: UintType(123), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tt { | ||
t.Run(test.name, func(t *testing.T) { | ||
if diff := cmp.Diff(test.actual, test.expected); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// IntType returns the core.ValueType depending on system int byte-size | ||
func IntType(v int) core.ValueType { | ||
if unsafe.Sizeof(v) == 4 { | ||
return core.INT32 | ||
} | ||
return core.INT64 | ||
} | ||
|
||
// UintType returns the core.ValueType depending on system uint byte-size | ||
func UintType(v uint) core.ValueType { | ||
if unsafe.Sizeof(v) == 4 { | ||
return core.UINT32 | ||
} | ||
return core.UINT64 | ||
} |
Oops, something went wrong.