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

Fix slice-valued attributes when used as map keys #2223

Merged
merged 4 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- Slice-valued attributes can correctly be used as map keys. (#2223)

## [1.0.0-RC3] - 2021-09-02

### Added
Expand Down
36 changes: 22 additions & 14 deletions attribute/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func BoolSliceValue(v []bool) Value {
copy(cp, v)
return Value{
vtype: BOOLSLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -94,7 +94,7 @@ func IntSliceValue(v []int) Value {
}
return Value{
vtype: INT64SLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -112,7 +112,7 @@ func Int64SliceValue(v []int64) Value {
copy(cp, v)
return Value{
vtype: INT64SLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -130,7 +130,7 @@ func Float64SliceValue(v []float64) Value {
copy(cp, v)
return Value{
vtype: FLOAT64SLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -148,7 +148,7 @@ func StringSliceValue(v []string) Value {
copy(cp, v)
return Value{
vtype: STRINGSLICE,
slice: cp,
slice: &cp,
}
}

Expand Down Expand Up @@ -197,8 +197,8 @@ func (v Value) AsBool() bool {
// AsBoolSlice returns the []bool value. Make sure that the Value's type is
// BOOLSLICE.
func (v Value) AsBoolSlice() []bool {
if s, ok := v.slice.([]bool); ok {
return s
if s, ok := v.slice.(*[]bool); ok {
return *s
}
return nil
}
Expand All @@ -212,8 +212,8 @@ func (v Value) AsInt64() int64 {
// AsInt64Slice returns the []int64 value. Make sure that the Value's type is
// INT64SLICE.
func (v Value) AsInt64Slice() []int64 {
if s, ok := v.slice.([]int64); ok {
return s
if s, ok := v.slice.(*[]int64); ok {
return *s
}
return nil
}
Expand All @@ -227,8 +227,8 @@ func (v Value) AsFloat64() float64 {
// AsFloat64Slice returns the []float64 value. Make sure that the Value's type is
// INT64SLICE.
func (v Value) AsFloat64Slice() []float64 {
if s, ok := v.slice.([]float64); ok {
return s
if s, ok := v.slice.(*[]float64); ok {
return *s
}
return nil
}
Expand All @@ -242,8 +242,8 @@ func (v Value) AsString() string {
// AsStringSlice returns the []string value. Make sure that the Value's type is
// INT64SLICE.
func (v Value) AsStringSlice() []string {
if s, ok := v.slice.([]string); ok {
return s
if s, ok := v.slice.(*[]string); ok {
return *s
}
return nil
}
Expand Down Expand Up @@ -285,14 +285,22 @@ func (v Value) AsInterface() interface{} {
// Emit returns a string representation of Value's data.
func (v Value) Emit() string {
switch v.Type() {
case ARRAY, BOOLSLICE, INT64SLICE, FLOAT64SLICE, STRINGSLICE:
case ARRAY:
return fmt.Sprint(v.slice)
case BOOLSLICE:
return fmt.Sprint(*(v.slice.(*[]bool)))
case BOOL:
return strconv.FormatBool(v.AsBool())
case INT64SLICE:
return fmt.Sprint(*(v.slice.(*[]int64)))
case INT64:
return strconv.FormatInt(v.AsInt64(), 10)
case FLOAT64SLICE:
return fmt.Sprint(*(v.slice.(*[]float64)))
case FLOAT64:
return fmt.Sprint(v.AsFloat64())
case STRINGSLICE:
return fmt.Sprint(*(v.slice.(*[]string)))
case STRING:
return v.stringly
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestSpanData(t *testing.T) {
DroppedAttributes: 1,
DroppedEvents: 2,
DroppedLinks: 3,
Resource: resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5)),
Resource: resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5), attribute.StringSlice("rk3", []string{"sv1", "sv2"})),
InstrumentationLibrary: instrumentation.Library{
Name: "go.opentelemetry.io/test/otel",
Version: "v0.0.1",
Expand Down