Skip to content

Commit

Permalink
[pdata] Rename Map.Upsert* methods to Map.Put* (#6064)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax authored Sep 14, 2022
1 parent d56b7b7 commit 243362e
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 186 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
- `pcommon.ImmutableFloat64Slice` is deprecated in favor of `pcommon.Float64Slice`
- `pcommon.ImmutableUInt64Slice` is deprecated in favor of `pcommon.UInt64Slice`
- Temporarily deprecate `pcommon.NewValueBytes` that will be replaced with `pcommon.NewValueBytesEmpty` in 0.60.0
- Deprecate `pcommon.Map.UpsertBytes` in favor of `pcommon.Map.UpsertEmptyBytes`
- Deprecate `pcommon.Map.UpsertBytes` in favor of `pcommon.Map.PutEmptyBytes` (#6064)
- Deprecate `pcommon.Value.SetBytesVal` in favor of `pcommon.Value.SetEmptyBytesVal`
- Deprecate `pcommon.New[Slice|Map]FromRaw` functions in favor of `New[Slice|Map]().FromRaw` (#6045)
- Deprecate `pcommon.Map.Insert*` methods (#6051)
- Deprecate `pcommon.Map.Upsert*` methods in favor of `pcommon.Map.Put*` (#6064)

### 💡 Enhancements 💡

Expand Down
18 changes: 9 additions & 9 deletions exporter/exporterhelper/internal/persistent_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ func TestPersistentQueue_ConsumersProducers(t *testing.T) {
func newTraces(numTraces int, numSpans int) ptrace.Traces {
traces := ptrace.NewTraces()
batch := traces.ResourceSpans().AppendEmpty()
batch.Resource().Attributes().UpsertString("resource-attr", "some-resource")
batch.Resource().Attributes().UpsertInt("num-traces", int64(numTraces))
batch.Resource().Attributes().UpsertInt("num-spans", int64(numSpans))
batch.Resource().Attributes().PutString("resource-attr", "some-resource")
batch.Resource().Attributes().PutInt("num-traces", int64(numTraces))
batch.Resource().Attributes().PutInt("num-spans", int64(numSpans))

for i := 0; i < numTraces; i++ {
traceID := pcommon.TraceID([16]byte{1, 2, 3, byte(i)})
Expand All @@ -176,12 +176,12 @@ func newTraces(numTraces int, numSpans int) ptrace.Traces {
span.SetTraceID(traceID)
span.SetSpanID([8]byte{1, 2, 3, byte(j)})
span.SetName("should-not-be-changed")
span.Attributes().UpsertInt("int-attribute", int64(j))
span.Attributes().UpsertString("str-attribute-1", "foobar")
span.Attributes().UpsertString("str-attribute-2", "fdslafjasdk12312312jkl")
span.Attributes().UpsertString("str-attribute-3", "AbcDefGeKKjkfdsafasdfsdasdf")
span.Attributes().UpsertString("str-attribute-4", "xxxxxx")
span.Attributes().UpsertString("str-attribute-5", "abcdef")
span.Attributes().PutInt("int-attribute", int64(j))
span.Attributes().PutString("str-attribute-1", "foobar")
span.Attributes().PutString("str-attribute-2", "fdslafjasdk12312312jkl")
span.Attributes().PutString("str-attribute-3", "AbcDefGeKKjkfdsafasdfsdasdf")
span.Attributes().PutString("str-attribute-4", "xxxxxx")
span.Attributes().PutString("str-attribute-5", "abcdef")
}
}

Expand Down
4 changes: 2 additions & 2 deletions exporter/loggingexporter/internal/otlptext/databuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func TestNestedArraySerializesCorrectly(t *testing.T) {
func TestNestedMapSerializesCorrectly(t *testing.T) {
ava := pcommon.NewValueMap()
av := ava.MapVal()
av.UpsertString("foo", "test")
av.PutString("foo", "test")

av.UpsertEmptyMap("zoo").UpsertInt("bar", 13)
av.PutEmptyMap("zoo").PutInt("bar", 13)

expected := `{
-> foo: STRING(test)
Expand Down
8 changes: 4 additions & 4 deletions exporter/otlphttpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ func TestIssue_4221(t *testing.T) {

md := ptrace.NewTraces()
rms := md.ResourceSpans().AppendEmpty()
rms.Resource().Attributes().UpsertString("service.name", "uop.stage-eu-1")
rms.Resource().Attributes().UpsertString("outsystems.module.version", "903386")
rms.Resource().Attributes().PutString("service.name", "uop.stage-eu-1")
rms.Resource().Attributes().PutString("outsystems.module.version", "903386")
ils := rms.ScopeSpans().AppendEmpty()
ils.Scope().SetName("uop_canaries")
ils.Scope().SetVersion("1")
Expand All @@ -291,8 +291,8 @@ func TestIssue_4221(t *testing.T) {
assert.Equal(t, "e5513c32795c41b9", span.SpanID().HexString())

span.SetEndTimestamp(1634684637873000000)
span.Attributes().UpsertInt("span_index", 3)
span.Attributes().UpsertString("code.function", "myFunction36")
span.Attributes().PutInt("span_index", 3)
span.Attributes().PutString("code.function", "myFunction36")
span.SetStartTimestamp(1634684637873000000)

assert.NoError(t, exp.ConsumeTraces(context.Background(), md))
Expand Down
16 changes: 8 additions & 8 deletions internal/testdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,40 @@ const (

func initResourceAttributes1(dest pcommon.Map) {
dest.Clear()
dest.UpsertString("resource-attr", "resource-attr-val-1")
dest.PutString("resource-attr", "resource-attr-val-1")
}

func initSpanEventAttributes(dest pcommon.Map) {
dest.Clear()
dest.UpsertString("span-event-attr", "span-event-attr-val")
dest.PutString("span-event-attr", "span-event-attr-val")
}

func initSpanLinkAttributes(dest pcommon.Map) {
dest.Clear()
dest.UpsertString("span-link-attr", "span-link-attr-val")
dest.PutString("span-link-attr", "span-link-attr-val")
}

func initMetricExemplarAttributes(dest pcommon.Map) {
dest.Clear()
dest.UpsertString("exemplar-attachment", "exemplar-attachment-value")
dest.PutString("exemplar-attachment", "exemplar-attachment-value")
}

func initMetricAttributes1(dest pcommon.Map) {
dest.Clear()
dest.UpsertString("label-1", "label-value-1")
dest.PutString("label-1", "label-value-1")
}

func initMetricAttributes12(dest pcommon.Map) {
initMetricAttributes1(dest)
dest.UpsertString(testLabelKey2, testLabelValue2)
dest.PutString(testLabelKey2, testLabelValue2)
}

func initMetricAttributes13(dest pcommon.Map) {
initMetricAttributes1(dest)
dest.UpsertString("label-3", "label-value-3")
dest.PutString("label-3", "label-value-3")
}

func initMetricAttributes2(dest pcommon.Map) {
dest.Clear()
dest.UpsertString(testLabelKey2, testLabelValue2)
dest.PutString(testLabelKey2, testLabelValue2)
}
8 changes: 4 additions & 4 deletions internal/testdata/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func fillLogOne(log plog.LogRecord) {
log.SetTraceID([16]byte{0x08, 0x04, 0x02, 0x01})

attrs := log.Attributes()
attrs.UpsertString("app", "server")
attrs.UpsertInt("instance_num", 1)
attrs.PutString("app", "server")
attrs.PutInt("instance_num", 1)

log.Body().SetStringVal("This is a log message")
}
Expand All @@ -63,8 +63,8 @@ func fillLogTwo(log plog.LogRecord) {
log.SetSeverityText("Info")

attrs := log.Attributes()
attrs.UpsertString("customer", "acme")
attrs.UpsertString("env", "dev")
attrs.PutString("customer", "acme")
attrs.PutString("env", "dev")

log.Body().SetStringVal("something happened")
}
87 changes: 61 additions & 26 deletions pdata/pcommon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func (m Map) RemoveIf(f func(string, Value) bool) {
//
// _, ok := m.Get(k)
// if !ok {
// v.CopyTo(m.UpsertEmpty(k)) // or use m.UpsertEmpty<Type> for complex types.
// v.CopyTo(m.PutEmpty(k)) // or use m.PutEmpty<Type> for complex types.
// }
func (m Map) Insert(k string, v Value) {
if _, existing := m.Get(k); !existing {
Expand All @@ -693,11 +693,11 @@ func (m Map) Insert(k string, v Value) {
// No action is applied to the map where the key already exists.
//
// Deprecated: [0.60.0] Replace it with the following function calls if you need to make sure that existing value is
// not overridden, otherwise just use UpsertString.
// not overridden, otherwise just use PutString.
//
// _, ok := m.Get(k)
// if !ok {
// m.UpsertString(k)
// m.PutString(k)
// }
func (m Map) InsertString(k string, v string) {
if _, existing := m.Get(k); !existing {
Expand All @@ -709,11 +709,11 @@ func (m Map) InsertString(k string, v string) {
// No action is applied to the map where the key already exists.
//
// Deprecated: [0.60.0] Replace it with the following function calls if you need to make sure that existing value is
// not overridden, otherwise just use UpsertInt.
// not overridden, otherwise just use PutInt.
//
// _, ok := m.Get(k)
// if !ok {
// m.UpsertInt(k)
// m.PutInt(k)
// }
func (m Map) InsertInt(k string, v int64) {
if _, existing := m.Get(k); !existing {
Expand All @@ -725,11 +725,11 @@ func (m Map) InsertInt(k string, v int64) {
// No action is applied to the map where the key already exists.
//
// Deprecated: [0.60.0] Replace it with the following function calls if you need to make sure that existing value is
// not overridden, otherwise just use UpsertDouble.
// not overridden, otherwise just use PutDouble.
//
// _, ok := m.Get(k)
// if !ok {
// m.UpsertDouble(k)
// m.PutDouble(k)
// }
func (m Map) InsertDouble(k string, v float64) {
if _, existing := m.Get(k); !existing {
Expand All @@ -741,11 +741,11 @@ func (m Map) InsertDouble(k string, v float64) {
// No action is applied to the map where the key already exists.
//
// Deprecated: [0.60.0] Replace it with the following function calls if you need to make sure that existing value is
// not overridden, otherwise just use UpsertBool.
// not overridden, otherwise just use PutBool.
//
// _, ok := m.Get(k)
// if !ok {
// m.UpsertBool(k)
// m.PutBool(k)
// }
func (m Map) InsertBool(k string, v bool) {
if _, existing := m.Get(k); !existing {
Expand Down Expand Up @@ -822,9 +822,9 @@ func (m Map) UpdateBytes(k string, v ImmutableByteSlice) {
}
}

// UpsertEmpty inserts or updates an empty value to the map under given key
// PutEmpty inserts or updates an empty value to the map under given key
// and return the updated/inserted value.
func (m Map) UpsertEmpty(k string) Value {
func (m Map) PutEmpty(k string) Value {
if av, existing := m.Get(k); existing {
av.getOrig().Value = nil
return newValue(av.getOrig())
Expand All @@ -833,43 +833,43 @@ func (m Map) UpsertEmpty(k string) Value {
return newValue(&(*m.getOrig())[len(*m.getOrig())-1].Value)
}

// UpsertString performs the Insert or Update action. The Value is
// PutString 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.
func (m Map) UpsertString(k string, v string) {
func (m Map) PutString(k string, v string) {
if av, existing := m.Get(k); existing {
av.SetStringVal(v)
} else {
*m.getOrig() = append(*m.getOrig(), newAttributeKeyValueString(k, v))
}
}

// UpsertInt performs the Insert or Update action. The int Value is
// PutInt performs the Insert or Update action. The int 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.
func (m Map) UpsertInt(k string, v int64) {
func (m Map) PutInt(k string, v int64) {
if av, existing := m.Get(k); existing {
av.SetIntVal(v)
} else {
*m.getOrig() = append(*m.getOrig(), newAttributeKeyValueInt(k, v))
}
}

// UpsertDouble performs the Insert or Update action. The double Value is
// PutDouble performs the Insert or Update action. The double 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.
func (m Map) UpsertDouble(k string, v float64) {
func (m Map) PutDouble(k string, v float64) {
if av, existing := m.Get(k); existing {
av.SetDoubleVal(v)
} else {
*m.getOrig() = append(*m.getOrig(), newAttributeKeyValueDouble(k, v))
}
}

// UpsertBool performs the Insert or Update action. The bool Value is
// PutBool performs the Insert or Update action. The bool 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.
func (m Map) UpsertBool(k string, v bool) {
func (m Map) PutBool(k string, v bool) {
if av, existing := m.Get(k); existing {
av.SetBoolVal(v)
} else {
Expand All @@ -880,7 +880,7 @@ func (m Map) UpsertBool(k string, v bool) {
// UpsertBytes performs the Insert or Update action. The ImmutableByteSlice 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.
// Deprecated: [0.60.0] Use UpsertEmptyBytes().FromRaw(v) instead.
// Deprecated: [0.60.0] Use PutEmptyBytes().FromRaw(v) instead.
func (m Map) UpsertBytes(k string, v ByteSlice) {
if av, existing := m.Get(k); existing {
av.SetBytesVal(v)
Expand All @@ -889,8 +889,8 @@ func (m Map) UpsertBytes(k string, v ByteSlice) {
}
}

// UpsertEmptyBytes inserts or updates an empty byte slice under given key and returns it.
func (m Map) UpsertEmptyBytes(k string) ByteSlice {
// PutEmptyBytes inserts or updates an empty byte slice under given key and returns it.
func (m Map) PutEmptyBytes(k string) ByteSlice {
bv := otlpcommon.AnyValue_BytesValue{}
if av, existing := m.Get(k); existing {
av.getOrig().Value = &bv
Expand All @@ -900,8 +900,8 @@ func (m Map) UpsertEmptyBytes(k string) ByteSlice {
return ByteSlice(internal.NewByteSlice(&bv.BytesValue))
}

// UpsertEmptyMap inserts or updates an empty map under given key and returns it.
func (m Map) UpsertEmptyMap(k string) Map {
// PutEmptyMap inserts or updates an empty map under given key and returns it.
func (m Map) PutEmptyMap(k string) Map {
kvl := otlpcommon.AnyValue_KvlistValue{KvlistValue: &otlpcommon.KeyValueList{Values: []otlpcommon.KeyValue(nil)}}
if av, existing := m.Get(k); existing {
av.getOrig().Value = &kvl
Expand All @@ -911,8 +911,8 @@ func (m Map) UpsertEmptyMap(k string) Map {
return Map(internal.NewMap(&kvl.KvlistValue.Values))
}

// UpsertEmptySlice inserts or updates an empty clice under given key and returns it.
func (m Map) UpsertEmptySlice(k string) Slice {
// PutEmptySlice inserts or updates an empty slice under given key and returns it.
func (m Map) PutEmptySlice(k string) Slice {
vl := otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{Values: []otlpcommon.AnyValue(nil)}}
if av, existing := m.Get(k); existing {
av.getOrig().Value = &vl
Expand All @@ -922,6 +922,41 @@ func (m Map) UpsertEmptySlice(k string) Slice {
return Slice(internal.NewSlice(&vl.ArrayValue.Values))
}

// Deprecated: [0.60.0] Use PutEmpty instead.
func (m Map) UpsertEmpty(k string) Value {
return m.PutEmpty(k)
}

// Deprecated: [0.60.0] Use PutString instead.
func (m Map) UpsertString(k string, v string) {
m.PutString(k, v)
}

// Deprecated: [0.60.0] Use func PutInt instead.
func (m Map) UpsertInt(k string, v int64) {
m.PutInt(k, v)
}

// Deprecated: [0.60.0] Use PutDouble instead.
func (m Map) UpsertDouble(k string, v float64) {
m.PutDouble(k, v)
}

// Deprecated: [0.60.0] Use PutBool instead.
func (m Map) UpsertBool(k string, v bool) {
m.PutBool(k, v)
}

// Deprecated: [0.60.0] Use PutEmptyMap instead.
func (m Map) UpsertEmptyMap(k string) Map {
return m.PutEmptyMap(k)
}

// Deprecated: [0.60.0] Use PutEmptySlice instead.
func (m Map) UpsertEmptySlice(k string) Slice {
return m.PutEmptySlice(k)
}

// Sort sorts the entries in the Map so two instances can be compared.
// Returns the same instance to allow nicer code like:
//
Expand Down
Loading

0 comments on commit 243362e

Please sign in to comment.