Skip to content

Commit

Permalink
Add minimal support for array value type
Browse files Browse the repository at this point in the history
Array value type was added to AnyValue Protobuf definition but corresponding
support in the Collector was missing.

This commit adds AnyValueArray internal data type which wraps an a slice of
AnyValue pointers.

AnyValueArray supports minimal set of functions for now. We will add more
functions if needed in the future.
  • Loading branch information
Tigran Najaryan committed Aug 26, 2020
1 parent 7f13eb6 commit 6ccd8cc
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 18 deletions.
8 changes: 8 additions & 0 deletions cmd/pdatagen/internal/common_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ var commonFile = &File{
`"testing"`,
``,
`"github.com/stretchr/testify/assert"`,
``,
`otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"`,
},
structs: []baseStruct{
instrumentationLibrary,
anyValueArray,
},
}

Expand Down Expand Up @@ -114,3 +117,8 @@ var anyValue = &messageStruct{
structName: "AttributeValue",
originFullName: "otlpcommon.AnyValue",
}

var anyValueArray = &sliceStruct{
structName: "AnyValueArray",
element: anyValue,
}
81 changes: 69 additions & 12 deletions consumer/pdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func (ts TimestampUnixNano) String() string {
type AttributeValueType int

const (
AttributeValueNULL = iota
AttributeValueNULL AttributeValueType = iota
AttributeValueSTRING
AttributeValueINT
AttributeValueDOUBLE
AttributeValueBOOL
AttributeValueMAP
// TODO: add ARRAY value types.
AttributeValueARRAY
)

func (avt AttributeValueType) String() string {
Expand All @@ -59,8 +59,9 @@ func (avt AttributeValueType) String() string {
return "DOUBLE"
case AttributeValueMAP:
return "MAP"
case AttributeValueARRAY:
return "ARRAY"
}
// TODO: add cases for ARRAY value types.
return ""
}

Expand Down Expand Up @@ -100,6 +101,10 @@ func NewAttributeValueNull() AttributeValue {
return AttributeValue{orig: &orig}
}

func NewAttributeValue() AttributeValue {
return NewAttributeValueNull()
}

// NewAttributeValueString creates a new AttributeValue with the given string value.
func NewAttributeValueString(v string) AttributeValue {
orig := &otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: v}}
Expand All @@ -124,12 +129,18 @@ func NewAttributeValueBool(v bool) AttributeValue {
return AttributeValue{orig: &orig}
}

// NewAttributeValueMap creates a new AttributeValue of array type.
// NewAttributeValueMap creates a new AttributeValue of map type.
func NewAttributeValueMap() AttributeValue {
orig := &otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_KvlistValue{KvlistValue: &otlpcommon.KeyValueList{}}}
return AttributeValue{orig: &orig}
}

// NewAttributeValueArray creates a new AttributeValue of array type.
func NewAttributeValueArray() AttributeValue {
orig := &otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{}}}
return AttributeValue{orig: &orig}
}

// NewAttributeValueSlice creates a slice of attributes values that are correctly initialized.
func NewAttributeValueSlice(len int) []AttributeValue {
// Allocate 3 slices, one for AttributeValues, another for underlying OTLP structs
Expand Down Expand Up @@ -171,40 +182,41 @@ func (a AttributeValue) Type() AttributeValueType {
return AttributeValueDOUBLE
case *otlpcommon.AnyValue_KvlistValue:
return AttributeValueMAP
case *otlpcommon.AnyValue_ArrayValue:
return AttributeValueARRAY
}
// TODO: add cases for ARRAY value types.
return AttributeValueNULL
}

// Value returns the string value associated with this AttributeValue.
// StringVal returns the string value associated with this AttributeValue.
// If the Type() is not AttributeValueSTRING then returns empty string.
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) StringVal() string {
return (*a.orig).GetStringValue()
}

// Value returns the int64 value associated with this AttributeValue.
// IntVal returns the int64 value associated with this AttributeValue.
// If the Type() is not AttributeValueINT then returns int64(0).
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) IntVal() int64 {
return (*a.orig).GetIntValue()
}

// Value returns the float64 value associated with this AttributeValue.
// DoubleVal returns the float64 value associated with this AttributeValue.
// If the Type() is not AttributeValueDOUBLE then returns float64(0).
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) DoubleVal() float64 {
return (*a.orig).GetDoubleValue()
}

// Value returns the bool value associated with this AttributeValue.
// BoolVal returns the bool value associated with this AttributeValue.
// If the Type() is not AttributeValueBOOL then returns false.
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) BoolVal() bool {
return (*a.orig).GetBoolValue()
}

// Value returns the map value associated with this AttributeValue.
// MapVal returns the map value associated with this AttributeValue.
// If the Type() is not AttributeValueMAP then returns an empty map. Note that modifying
// such empty map has no effect on this AttributeValue.
//
Expand All @@ -217,6 +229,19 @@ func (a AttributeValue) MapVal() AttributeMap {
return newAttributeMap(&kvlist.Values)
}

// ArrayVal returns the array value associated with this AttributeValue.
// If the Type() is not AttributeValueARRAY then returns an empty array. Note that modifying
// such empty array has no effect on this AttributeValue.
//
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) ArrayVal() AnyValueArray {
arr := (*a.orig).GetArrayValue()
if arr == nil {
return NewAnyValueArray()
}
return newAnyValueArray(&arr.Values)
}

// SetStringVal replaces the string value associated with this AttributeValue,
// it also changes the type to be AttributeValueSTRING.
// Calling this function on zero-initialized AttributeValue will cause a panic.
Expand Down Expand Up @@ -284,6 +309,32 @@ func (a AttributeValue) SetMapVal(m AttributeMap) {
destMap.InitFromAttributeMap(m)
}

// SetArrayVal replaces the value associated with this AttributeValue,
// it also changes the type to be AttributeValueARRAY. The `arr` argument will be deep
// copied into this AttributeValue.
//
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) SetArrayVal(arr AnyValueArray) {
if *a.orig == nil {
*a.orig = &otlpcommon.AnyValue{}
}
var dest *otlpcommon.ArrayValue
switch v := (*a.orig).Value.(type) {
case *otlpcommon.AnyValue_ArrayValue:
if v.ArrayValue == nil {
v.ArrayValue = &otlpcommon.ArrayValue{}
}
dest = v.ArrayValue

default:
dest = &otlpcommon.ArrayValue{}
(*a.orig).Value = &otlpcommon.AnyValue_ArrayValue{ArrayValue: dest}
}

destArr := newAnyValueArray(&dest.Values)
arr.CopyTo(destArr)
}

// copyTo copies the value to AnyValue. Will panic if dest is nil.
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) copyTo(dest *otlpcommon.AnyValue) {
Expand All @@ -302,7 +353,13 @@ func (a AttributeValue) copyTo(dest *otlpcommon.AnyValue) {
AttributeValue{&dest}.SetMapVal(newAttributeMap(&v.KvlistValue.Values))
}
case *otlpcommon.AnyValue_ArrayValue:
// TODO: handle ARRAY data type. We need to make a deep copy.
if v.ArrayValue == nil {
// Source is empty.
AttributeValue{&dest}.SetArrayVal(NewAnyValueArray())
} else {
// Deep copy to dest.
AttributeValue{&dest}.SetArrayVal(newAnyValueArray(&v.ArrayValue.Values))
}
default:
// Primitive immutable type, no need for deep copy.
dest.Value = (*a.orig).Value
Expand Down Expand Up @@ -660,7 +717,7 @@ func (am AttributeMap) Len() int {
//
// Example:
//
// it := sm.ForEach(func(k string, v StringValue) {
// it := sm.ForEach(func(k string, v AttributeValue) {
// ...
// })
func (am AttributeMap) ForEach(f func(k string, v AttributeValue)) {
Expand Down
148 changes: 144 additions & 4 deletions consumer/pdata/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func TestAttributeValue(t *testing.T) {
assert.EqualValues(t, true, v.BoolVal())
}

func TestAttributeValueType(t *testing.T) {
assert.EqualValues(t, "NULL", AttributeValueNULL.String())
assert.EqualValues(t, "STRING", AttributeValueSTRING.String())
assert.EqualValues(t, "BOOL", AttributeValueBOOL.String())
assert.EqualValues(t, "INT", AttributeValueINT.String())
assert.EqualValues(t, "DOUBLE", AttributeValueDOUBLE.String())
assert.EqualValues(t, "MAP", AttributeValueMAP.String())
assert.EqualValues(t, "ARRAY", AttributeValueARRAY.String())
}

func fromVal(v interface{}) AttributeValue {
switch val := v.(type) {
case string:
Expand All @@ -73,8 +83,10 @@ func fromVal(v interface{}) AttributeValue {
return NewAttributeValueDouble(val)
case map[string]interface{}:
return fromMap(val)
case []interface{}:
return fromArray(val)
}
return NewAttributeValueNull()
panic("data type is not supported in fromVal()")
}

func fromMap(v map[string]interface{}) AttributeValue {
Expand All @@ -88,7 +100,7 @@ func fromMap(v map[string]interface{}) AttributeValue {
return av
}

func fromJSON(jsonStr string) AttributeValue {
func fromJSONMap(jsonStr string) AttributeValue {
var src map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &src)
if err != nil {
Expand All @@ -98,12 +110,12 @@ func fromJSON(jsonStr string) AttributeValue {
}

func assertMapJSON(t *testing.T, expectedJSON string, actualMap AttributeValue) {
assert.EqualValues(t, fromJSON(expectedJSON).MapVal(), actualMap.MapVal().Sort())
assert.EqualValues(t, fromJSONMap(expectedJSON).MapVal(), actualMap.MapVal().Sort())
}

func TestAttributeValueMap(t *testing.T) {
m1 := NewAttributeValueMap()
assert.EqualValues(t, fromJSON(`{}`), m1)
assert.EqualValues(t, fromJSONMap(`{}`), m1)
assert.EqualValues(t, AttributeValueMAP, m1.Type())
assert.EqualValues(t, NewAttributeMap(), m1.MapVal())
assert.EqualValues(t, 0, m1.MapVal().Len())
Expand Down Expand Up @@ -1125,3 +1137,131 @@ func generateTestBoolAttributeMap() AttributeMap {
})
return am
}

func fromArray(v []interface{}) AttributeValue {
arr := NewAnyValueArray()
for _, v := range v {
av := fromVal(v)
arr.Append(&av)
}
av := NewAttributeValueArray()
av.SetArrayVal(arr)
return av
}

func fromJSONArray(jsonStr string) AttributeValue {
var src []interface{}
err := json.Unmarshal([]byte(jsonStr), &src)
if err != nil {
panic("Invalid input jsonStr:" + jsonStr)
}
return fromArray(src)
}

func assertArrayJSON(t *testing.T, expectedJSON string, actualArray AttributeValue) {
assert.EqualValues(t, fromJSONArray(expectedJSON).ArrayVal(), actualArray.ArrayVal())
}

func TestAttributeValueArray(t *testing.T) {
a1 := NewAttributeValueArray()
assert.EqualValues(t, fromJSONArray(`[]`), a1)
assert.EqualValues(t, AttributeValueARRAY, a1.Type())
assert.EqualValues(t, NewAnyValueArray(), a1.ArrayVal())
assert.EqualValues(t, 0, a1.ArrayVal().Len())

av := NewAttributeValueDouble(123)
a1.ArrayVal().Append(&av)
assertArrayJSON(t, `[123]`, a1)
assert.EqualValues(t, 1, a1.ArrayVal().Len())

v := a1.ArrayVal().At(0)
assert.EqualValues(t, AttributeValueDOUBLE, v.Type())
assert.EqualValues(t, 123, v.DoubleVal())

// Create a second array.
a2 := NewAttributeValueArray()
assertArrayJSON(t, `[]`, a2)
assert.EqualValues(t, 0, a2.ArrayVal().Len())

av = NewAttributeValueString("somestr")
a2.ArrayVal().Append(&av)
assertArrayJSON(t, `["somestr"]`, a2)
assert.EqualValues(t, 1, a2.ArrayVal().Len())

// Insert the second array as a child.
a1.ArrayVal().Append(&a2)
assertArrayJSON(t, `[123, ["somestr"]]`, a1)
assert.EqualValues(t, 2, a1.ArrayVal().Len())

// Check that the array was correctly inserted.
childArray := a1.ArrayVal().At(1)
assert.EqualValues(t, AttributeValueARRAY, childArray.Type())
assert.EqualValues(t, 1, childArray.ArrayVal().Len())

v = childArray.ArrayVal().At(0)
assert.EqualValues(t, AttributeValueSTRING, v.Type())
assert.EqualValues(t, "somestr", v.StringVal())

// Test nil values case for ArrayVal() func.
orig := &otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: nil}}
a1 = AttributeValue{orig: &orig}
assert.EqualValues(t, NewAnyValueArray(), a1.ArrayVal())
}

func TestAttributeValueSetArrayVal(t *testing.T) {
var anyVal *otlpcommon.AnyValue
v := newAttributeValue(&anyVal)
assert.EqualValues(t, AttributeValueNULL, v.Type())

v.SetArrayVal(NewAnyValueArray())
assert.EqualValues(t, AttributeValueARRAY, v.Type())

anyVal = &otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{}}
v = newAttributeValue(&anyVal)
assert.EqualValues(t, AttributeValueARRAY, v.Type())

v.SetArrayVal(NewAnyValueArray())
assert.EqualValues(t, AttributeValueARRAY, v.Type())

v.SetIntVal(123)
assert.EqualValues(t, AttributeValueINT, v.Type())

v.SetArrayVal(NewAnyValueArray())
assert.EqualValues(t, AttributeValueARRAY, v.Type())
assert.EqualValues(t, 0, v.ArrayVal().Len())

av := NewAnyValueArray()
av.Resize(1)
v.SetArrayVal(av)
assert.EqualValues(t, AttributeValueARRAY, v.Type())
assert.EqualValues(t, 1, v.ArrayVal().Len())
}

func TestAnyValueArrayWithNilValues(t *testing.T) {
origWithNil := []*otlpcommon.AnyValue{
nil,
{Value: &otlpcommon.AnyValue_StringValue{StringValue: "test_value"}},
nil,
{Value: nil},
}
sm := AnyValueArray{
orig: &origWithNil,
}
val := sm.At(1)
assert.EqualValues(t, AttributeValueSTRING, val.Type())
assert.EqualValues(t, "test_value", val.StringVal())

val = sm.At(3)
assert.EqualValues(t, AttributeValueNULL, val.Type())
assert.EqualValues(t, "", val.StringVal())

val = sm.At(0)
assert.EqualValues(t, AttributeValueNULL, val.Type())
assert.EqualValues(t, "", val.StringVal())

av := NewAttributeValueString("other_value")
sm.Append(&av)
val = sm.At(4)
assert.EqualValues(t, AttributeValueSTRING, val.Type())
assert.EqualValues(t, "other_value", val.StringVal())
}
Loading

0 comments on commit 6ccd8cc

Please sign in to comment.