Skip to content

Commit

Permalink
[pdata] Deprecate map.Upsert/Insert/Update
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Aug 26, 2022
1 parent fe28ec9 commit 38c74ea
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Deprecate `component.NewExtensionFactoryWithStabilityLevel` in favor of `component.NewExtensionFactory` (#5917)
- Deprecate `plog.SeverityNumber[UPPERCASE]` constants (#5927)
- Deprecate `pcommon.Map.InsertNull` method (#5955)
- Deprecate `Insert`, `Update` and `Upsert` methods of `pcommon.Map` (#5975)
- Deprecate FlagsStruct types (#5933):
- `MetricDataPointFlagsStruct` -> `MetricDataPointFlags`
- `NewMetricDataPointFlagsStruct` -> `NewMetricDataPointFlags`
Expand All @@ -47,6 +48,8 @@
- Change pdata generated types to use type definition instead of aliases. (#5936)
- Improves documentation, and makes code easier to read/understand.
- Log `InstrumentationScope` attributes in `loggingexporter` (#5976)
- Add `pcommon.Value.SetEmpty[Map|Slice]Val` methods (#5975)
- Add `pcommon.Map.upsertEmpty` and `pcommon.Map.UpsertEmpty[Map|Slice]` methods (#5975)

### 🧰 Bug fixes 🧰

Expand Down
51 changes: 51 additions & 0 deletions pdata/pcommon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ func (v Value) SetBytesVal(bv ImmutableByteSlice) {
v.getOrig().Value = &otlpcommon.AnyValue_BytesValue{BytesValue: bv.getOrig()}
}

// SetEmptyMap sets tje value to an empty map and returns it.
// Calling this function on zero-initialized Value will cause a panic.
func (v Value) SetEmptyMapVal() Map {
kv := &otlpcommon.AnyValue_KvlistValue{KvlistValue: &otlpcommon.KeyValueList{}}
v.getOrig().Value = kv
return newMap(&kv.KvlistValue.Values)
}

// SetEmptySlice sets the value to an empty slice and returns it.
// Calling this function on zero-initialized Value will cause a panic.
func (v Value) SetEmptySliceVal() Slice {
av := &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{}}
v.getOrig().Value = av
return newSlice(&av.ArrayValue.Values)
}

// copyTo copies the value to Value. Will panic if dest is nil.
func (v Value) copyTo(dest *otlpcommon.AnyValue) {
switch ov := v.getOrig().Value.(type) {
Expand Down Expand Up @@ -648,6 +664,7 @@ func (m Map) RemoveIf(f func(string, Value) bool) {
//
// Important: this function should not be used if the caller has access to
// the raw value to avoid an extra allocation.
// Deprecated: [0.59.0] Use Insert<type> methods or UpsertEmpty with CopyTo instead.
func (m Map) Insert(k string, v Value) {
if _, existing := m.Get(k); !existing {
*m.getOrig() = append(*m.getOrig(), newAttributeKeyValue(k, v))
Expand Down Expand Up @@ -710,6 +727,7 @@ func (m Map) InsertBytes(k string, v ImmutableByteSlice) {
//
// Important: this function should not be used if the caller has access to
// the raw value to avoid an extra allocation.
// Deprecated: [0.59.0] Use Update<type> methods or Get with CopyTo instead.
func (m Map) Update(k string, v Value) {
if av, existing := m.Get(k); existing {
v.copyTo(av.getOrig())
Expand Down Expand Up @@ -764,6 +782,7 @@ func (m Map) UpdateBytes(k string, v ImmutableByteSlice) {
//
// Important: this function should not be used if the caller has access to
// the raw value to avoid an extra allocation.
// Deprecated: [0.59.0] Use Upsert<type> methods or UpsertEmpty with CopyTo instead.
func (m Map) Upsert(k string, v Value) {
if av, existing := m.Get(k); existing {
v.copyTo(av.getOrig())
Expand All @@ -772,6 +791,16 @@ func (m Map) Upsert(k string, v Value) {
}
}

// UpsertEmpty inserts or updates an empty value to the map under given key.
func (m Map) UpsertEmpty(k string) {
v := NewValueEmpty()
if av, existing := m.Get(k); existing {
*av.getOrig() = *v.getOrig()
} else {
*m.getOrig() = append(*m.getOrig(), newAttributeKeyValue(k, v))
}
}

// UpsertString performs the Insert or Update action. The Value is
// inserted to the map that did not originally have the key. The key/value is
// updated to the map where the key already existed.
Expand Down Expand Up @@ -827,6 +856,28 @@ func (m Map) UpsertBytes(k string, v ImmutableByteSlice) {
}
}

// UpsertEmptyMap updates or inserts an empty map under particular key and returns the map.
func (m Map) UpsertEmptyMap(k string) Map {
v := NewValueMap()
if av, existing := m.Get(k); existing {
*av.getOrig() = *v.getOrig()
} else {
*m.getOrig() = append(*m.getOrig(), newAttributeKeyValue(k, v))
}
return v.MapVal()
}

// UpsertEmptySlice updates or inserts an empty slice under particular key and returns the map.
func (m Map) UpsertEmptySlice(k string) Slice {
v := NewValueSlice()
if av, existing := m.Get(k); existing {
*av.getOrig() = *v.getOrig()
} else {
*m.getOrig() = append(*m.getOrig(), newAttributeKeyValue(k, v))
}
return v.SliceVal()
}

// Sort sorts the entries in the Map so two instances can be compared.
// Returns the same instance to allow nicer code like:
//
Expand Down
14 changes: 6 additions & 8 deletions pdata/ptrace/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,12 @@ var tracesOTLPFull = func() Traces {
sp.Attributes().UpsertInt("int", 1)
sp.Attributes().UpsertDouble("double", 1.1)
sp.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo")))
arr := pcommon.NewValueSlice()
arr.SliceVal().AppendEmpty().SetIntVal(1)
arr.SliceVal().AppendEmpty().SetStringVal("str")
sp.Attributes().Upsert("array", arr)
kvList := pcommon.NewValueMap()
kvList.MapVal().Upsert("int", pcommon.NewValueInt(1))
kvList.MapVal().Upsert("string", pcommon.NewValueString("string"))
sp.Attributes().Upsert("kvList", kvList)
sl := sp.Attributes().UpsertEmptySlice("array")
sl.AppendEmpty().SetIntVal(1)
sl.AppendEmpty().SetStringVal("str")
kvList := sp.Attributes().UpsertEmptyMap("kvList")
kvList.UpsertInt("int", 1)
kvList.UpsertString("string", "string")
// Add events.
event := sp.Events().AppendEmpty()
event.SetName("eventName")
Expand Down

0 comments on commit 38c74ea

Please sign in to comment.