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

add core.KV(string, interface{}) #632

Closed
wants to merge 1 commit into from
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
148 changes: 89 additions & 59 deletions api/core/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,22 @@ func Bool(v bool) Value {
}
}

// Int64 creates an INT64 Value.
func Int64(v int64) Value {
return Value{
vtype: INT64,
numeric: int64ToRaw(v),
}
}

// Uint64 creates a UINT64 Value.
func Uint64(v uint64) Value {
return Value{
vtype: UINT64,
numeric: uint64ToRaw(v),
// Int creates either an INT32 or an INT64 Value, depending on whether
// the int type is 32 or 64 bits wide.
func Int(v int) Value {
if unsafe.Sizeof(v) == 4 {
return Int32(int32(v))
}
return Int64(int64(v))
}

// Float64 creates a FLOAT64 Value.
func Float64(v float64) Value {
return Value{
vtype: FLOAT64,
numeric: float64ToRaw(v),
// Uint creates either a UINT32 or a UINT64 Value, depending on
// whether the uint type is 32 or 64 bits wide.
func Uint(v uint) Value {
if unsafe.Sizeof(v) == 4 {
return Uint32(uint32(v))
}
return Uint64(uint64(v))
}

// Int32 creates an INT32 Value.
Expand All @@ -96,6 +90,14 @@ func Int32(v int32) Value {
}
}

// Int64 creates an INT64 Value.
func Int64(v int64) Value {
return Value{
vtype: INT64,
numeric: int64ToRaw(v),
}
}

// Uint32 creates a UINT32 Value.
func Uint32(v uint32) Value {
return Value{
Expand All @@ -104,6 +106,14 @@ func Uint32(v uint32) Value {
}
}

// Uint64 creates a UINT64 Value.
func Uint64(v uint64) Value {
return Value{
vtype: UINT64,
numeric: uint64ToRaw(v),
}
}

// Float32 creates a FLOAT32 Value.
func Float32(v float32) Value {
return Value{
Expand All @@ -112,6 +122,14 @@ func Float32(v float32) Value {
}
}

// Float64 creates a FLOAT64 Value.
func Float64(v float64) Value {
return Value{
vtype: FLOAT64,
numeric: float64ToRaw(v),
}
}

// String creates a STRING Value.
func String(v string) Value {
return Value{
Expand All @@ -120,22 +138,34 @@ func String(v string) Value {
}
}

// Int creates either an INT32 or an INT64 Value, depending on whether
// the int type is 32 or 64 bits wide.
func Int(v int) Value {
if unsafe.Sizeof(v) == 4 {
return Int32(int32(v))
}
return Int64(int64(v))
}

// Uint creates either a UINT32 or a UINT64 Value, depending on
// whether the uint type is 32 or 64 bits wide.
func Uint(v uint) Value {
if unsafe.Sizeof(v) == 4 {
return Uint32(uint32(v))
func KV(key string, value interface{}) []KeyValue {
k := Key(key)
switch v := value.(type) {
case bool:
return []KeyValue{k.Bool(v)}
case int:
return []KeyValue{k.Int(v)}
case uint:
return []KeyValue{k.Uint(v)}
case int32:
return []KeyValue{k.Int32(v)}
case int64:
return []KeyValue{k.Int64(v)}
case uint32:
return []KeyValue{k.Uint32(v)}
case uint64:
return []KeyValue{k.Uint64(v)}
case float32:
return []KeyValue{k.Float32(v)}
case float64:
return []KeyValue{k.Float64(v)}
case string:
return []KeyValue{k.String(v)}
case fmt.Stringer:
return []KeyValue{k.String(v.String())}
default:
return []KeyValue{k.String(fmt.Sprintf("%v", v))}
}
return Uint64(uint64(v))
}

// Bool creates a KeyValue instance with a BOOL Value.
Expand All @@ -146,35 +176,35 @@ func (k Key) Bool(v bool) KeyValue {
}
}

// Int64 creates a KeyValue instance with an INT64 Value.
func (k Key) Int64(v int64) KeyValue {
func (k Key) Int(v int) KeyValue {
return KeyValue{
Key: k,
Value: Int64(v),
Value: Int(v),
}
}

// Uint64 creates a KeyValue instance with a UINT64 Value.
func (k Key) Uint64(v uint64) KeyValue {
// Uint creates a KeyValue instance with either an UINT32 or an UINT64
// Value, depending on whether the uint type is 32 or 64 bits wide.
func (k Key) Uint(v uint) KeyValue {
return KeyValue{
Key: k,
Value: Uint64(v),
Value: Uint(v),
}
}

// Float64 creates a KeyValue instance with a FLOAT64 Value.
func (k Key) Float64(v float64) KeyValue {
// Int64 creates a KeyValue instance with an INT64 Value.
// Int32 creates a KeyValue instance with an INT32 Value.
func (k Key) Int32(v int32) KeyValue {
return KeyValue{
Key: k,
Value: Float64(v),
Value: Int32(v),
}
}

// Int32 creates a KeyValue instance with an INT32 Value.
func (k Key) Int32(v int32) KeyValue {
func (k Key) Int64(v int64) KeyValue {
return KeyValue{
Key: k,
Value: Int32(v),
Value: Int64(v),
}
}

Expand All @@ -186,40 +216,40 @@ func (k Key) Uint32(v uint32) KeyValue {
}
}

// Float32 creates a KeyValue instance with a FLOAT32 Value.
func (k Key) Float32(v float32) KeyValue {
// Uint64 creates a KeyValue instance with a UINT64 Value.
func (k Key) Uint64(v uint64) KeyValue {
return KeyValue{
Key: k,
Value: Float32(v),
Value: Uint64(v),
}
}

// String creates a KeyValue instance with a STRING Value.
func (k Key) String(v string) KeyValue {
// Float32 creates a KeyValue instance with a FLOAT32 Value.
func (k Key) Float32(v float32) KeyValue {
return KeyValue{
Key: k,
Value: String(v),
Value: Float32(v),
}
}

// Int creates a KeyValue instance with either an INT32 or an INT64
// Value, depending on whether the int type is 32 or 64 bits wide.
func (k Key) Int(v int) KeyValue {
// Float64 creates a KeyValue instance with a FLOAT64 Value.
func (k Key) Float64(v float64) KeyValue {
return KeyValue{
Key: k,
Value: Int(v),
Value: Float64(v),
}
}

// Uint creates a KeyValue instance with either an UINT32 or an UINT64
// Value, depending on whether the uint type is 32 or 64 bits wide.
func (k Key) Uint(v uint) KeyValue {
// String creates a KeyValue instance with a STRING Value.
func (k Key) String(v string) KeyValue {
return KeyValue{
Key: k,
Value: Uint(v),
Value: String(v),
}
}

// Int creates a KeyValue instance with either an INT32 or an INT64
// Value, depending on whether the int type is 32 or 64 bits wide.
// Defined returns true for non-empty keys.
func (k Key) Defined() bool {
return len(k) != 0
Expand Down
83 changes: 83 additions & 0 deletions api/core/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package core_test

import (
"strings"
"testing"
"unsafe"

Expand All @@ -23,6 +24,88 @@ import (
"go.opentelemetry.io/otel/api/core"
)

func TestKV(t *testing.T) {
builder := &strings.Builder{}
builder.WriteString("foo")
for _, testcase := range []struct {
key string
value interface{}
wantType core.ValueType
wantValue interface{}
}{
{
key: "bool type inferred",
value: true,
wantType: core.BOOL,
wantValue: true,
},
{
key: "int64 type inferred",
value: int64(42),
wantType: core.INT64,
wantValue: int64(42),
},
{
key: "uint64 type inferred",
value: uint64(42),
wantType: core.UINT64,
wantValue: uint64(42),
},
{
key: "float64 type inferred",
value: float64(42.1),
wantType: core.FLOAT64,
wantValue: 42.1,
},
{
key: "int32 type inferred",
value: int32(42),
wantType: core.INT32,
wantValue: int32(42),
},
{
key: "uint32 type inferred",
value: uint32(42),
wantType: core.UINT32,
wantValue: uint32(42),
},
{
key: "float32 type inferred",
value: float32(42.1),
wantType: core.FLOAT32,
wantValue: float32(42.1),
},
{
key: "string type inferred",
value: "foo",
wantType: core.STRING,
wantValue: "foo",
},
{
key: "stringer type inferred",
value: builder,
wantType: core.STRING,
wantValue: "foo",
},
{
key: "unknown value serialized as %v",
value: nil,
wantType: core.STRING,
wantValue: "<nil>",
},
} {
t.Logf("Running test case %s", testcase.key)
kv := core.KV(testcase.key, testcase.value)
if kv[0].Value.Type() != testcase.wantType {
t.Errorf("wrong value type, got %#v, expected %#v", kv[0].Value.Type(), testcase.wantType)
}
got := kv[0].Value.AsInterface()
if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
t.Errorf("+got, -want: %s", diff)
}
}
}

func TestValue(t *testing.T) {
k := core.Key("test")
bli := getBitlessInfo(42)
Expand Down