-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minimal support for array value type
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. This is the same approach as we previously used for maps. AnyValueArray supports minimal set of functions for now. We will add more functions if needed in the future. AnyValueArray is implemented in a separate go file, instead of common.go. In a subsequent PR I will refactor AttributeMap to a separate file too to avoid overloading common.go.
- Loading branch information
Tigran Najaryan
committed
Aug 11, 2020
1 parent
d051556
commit 1d88ecd
Showing
7 changed files
with
712 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pdata | ||
|
||
// This file contains implementation of array value type. | ||
|
||
import ( | ||
otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1" | ||
) | ||
|
||
// AnyValueArray stores an array of values. | ||
type AnyValueArray struct { | ||
orig *[]*otlpcommon.AnyValue | ||
} | ||
|
||
// NewAnyValueArray creates a AnyValueArray with 0 elements. | ||
func NewAnyValueArray() AnyValueArray { | ||
orig := []*otlpcommon.AnyValue(nil) | ||
return AnyValueArray{&orig} | ||
} | ||
|
||
func newAnyValueArray(orig *[]*otlpcommon.AnyValue) AnyValueArray { | ||
return AnyValueArray{orig} | ||
} | ||
|
||
// InitFromSlice overwrites the entire AnyValueArray and reconstructs the AnyValueArray | ||
// with values from the given AttributeBalue slice. | ||
// | ||
// Returns the same instance to allow nicer code like: | ||
// assert.EqualValues(t, NewAnyValueArray().InitFromSlice([]AttributeValue{...}), actual) | ||
func (am AnyValueArray) InitFromSlice(values []AttributeValue) AnyValueArray { | ||
if len(values) == 0 { | ||
*am.orig = []*otlpcommon.AnyValue(nil) | ||
return am | ||
} | ||
origs := make([]otlpcommon.AnyValue, len(values)) | ||
wrappers := make([]*otlpcommon.AnyValue, len(values)) | ||
for ix, v := range values { | ||
wrappers[ix] = &origs[ix] | ||
v.copyTo(&origs[ix]) | ||
} | ||
*am.orig = wrappers | ||
return am | ||
} | ||
|
||
// InitFromAnyValueArray overwrites the entire AnyValueArray and reconstructs the AnyValueArray | ||
// with values from the given AnyValueArray. | ||
// | ||
// Returns the same instance to allow nicer code like: | ||
// assert.EqualValues(t, NewAnyValueArray().InitFromAnyValueArray([]AttributeValue{...}), actual) | ||
func (am AnyValueArray) InitFromAnyValueArray(values AnyValueArray) AnyValueArray { | ||
srcLen := values.Len() | ||
if srcLen == 0 || values.orig == nil { | ||
*am.orig = []*otlpcommon.AnyValue(nil) | ||
return am | ||
} | ||
origs := make([]otlpcommon.AnyValue, srcLen) | ||
wrappers := make([]*otlpcommon.AnyValue, srcLen) | ||
for ix := range *values.orig { | ||
wrappers[ix] = &origs[ix] | ||
AttributeValue{&(*values.orig)[ix]}.copyTo(&origs[ix]) | ||
} | ||
*am.orig = wrappers | ||
return am | ||
} | ||
|
||
// InitEmptyWithCapacity constructs an empty AnyValueArray with predefined slice capacity. | ||
func (am AnyValueArray) InitEmptyWithCapacity(cap int) { | ||
if cap == 0 { | ||
*am.orig = []*otlpcommon.AnyValue(nil) | ||
} | ||
*am.orig = make([]*otlpcommon.AnyValue, 0, cap) | ||
} | ||
|
||
// InitLen constructs an AnyValueArray with predefined length. All elements of the array | ||
// will be empty values. | ||
func (am AnyValueArray) InitLen(len int) { | ||
if len == 0 { | ||
*am.orig = []*otlpcommon.AnyValue(nil) | ||
} | ||
|
||
origs := make([]otlpcommon.AnyValue, len) | ||
wrappers := make([]*otlpcommon.AnyValue, len) | ||
for ix := range origs { | ||
wrappers[ix] = &origs[ix] | ||
} | ||
*am.orig = wrappers | ||
} | ||
|
||
// GetAt returns the AttributeValue at specified index. | ||
// | ||
// Calling this function with a zero-initialized AnyValueArray struct will cause a panic. | ||
// Will cause a panic if index is out of bounds. | ||
func (am AnyValueArray) GetAt(index int) AttributeValue { | ||
return AttributeValue{&(*am.orig)[index]} | ||
} | ||
|
||
// SetAt sets the value at the specified index. | ||
// | ||
// Calling this function with a zero-initialized AnyValueArray struct will cause a panic. | ||
// Will cause a panic if index is out of bounds. | ||
func (am AnyValueArray) SetAt(index int, v AttributeValue) { | ||
v.copyTo((*am.orig)[index]) | ||
} | ||
|
||
// Append a value at the end of the array. | ||
// | ||
// Calling this function with a zero-initialized AnyValueArray struct will cause a panic. | ||
// | ||
// Important: instead of Append it is preferable to use InitLen followed by SetAt calls | ||
// to minimize the number of allocations. | ||
func (am AnyValueArray) Append(v AttributeValue) { | ||
*am.orig = append(*am.orig, &otlpcommon.AnyValue{}) | ||
v.copyTo((*am.orig)[len(*am.orig)-1]) | ||
} | ||
|
||
// Len returns the length of this array. | ||
func (am AnyValueArray) Len() int { | ||
return len(*am.orig) | ||
} | ||
|
||
// ForEach iterates over the every elements in the array by calling the provided func. | ||
// | ||
// Example: | ||
// | ||
// it := sm.ForEach(func(index int, v AttributeValue) { | ||
// ... | ||
// }) | ||
func (am AnyValueArray) ForEach(f func(index int, v AttributeValue)) { | ||
for i := range *am.orig { | ||
f(i, AttributeValue{&(*am.orig)[i]}) | ||
} | ||
} | ||
|
||
// CopyTo copies all elements from the current array to the dest. | ||
func (am AnyValueArray) CopyTo(dest AnyValueArray) { | ||
newLen := len(*am.orig) | ||
if newLen == 0 { | ||
*dest.orig = []*otlpcommon.AnyValue(nil) | ||
return | ||
} | ||
|
||
oldLen := len(*dest.orig) | ||
if newLen <= oldLen { | ||
// New slice fits in existing slice, no need to reallocate. | ||
*dest.orig = (*dest.orig)[:newLen] | ||
for i := range *am.orig { | ||
destAkv := (*dest.orig)[i] | ||
AttributeValue{&(*am.orig)[i]}.copyTo(destAkv) | ||
} | ||
return | ||
} | ||
|
||
// New slice is bigger than exist slice. Allocate new space. | ||
origs := make([]otlpcommon.AnyValue, len(*am.orig)) | ||
wrappers := make([]*otlpcommon.AnyValue, len(*am.orig)) | ||
for i := range *am.orig { | ||
wrappers[i] = &origs[i] | ||
AttributeValue{&(*am.orig)[i]}.copyTo(&origs[i]) | ||
} | ||
*dest.orig = wrappers | ||
} |
Oops, something went wrong.