2019-06-14 22:09:41 +02:00
|
|
|
// Copyright 2019, 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.
|
|
|
|
|
2019-10-10 23:30:22 +02:00
|
|
|
package distributedcontext
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
import (
|
2019-07-15 23:49:21 +02:00
|
|
|
"go.opentelemetry.io/api/core"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-10-10 23:30:22 +02:00
|
|
|
type entry struct {
|
2019-06-28 06:26:16 +02:00
|
|
|
value core.Value
|
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-10-10 23:30:22 +02:00
|
|
|
type rawMap map[core.Key]entry
|
2019-08-23 18:01:52 +02:00
|
|
|
|
|
|
|
type Map struct {
|
|
|
|
m rawMap
|
|
|
|
}
|
2019-07-01 20:27:52 +02:00
|
|
|
|
2019-08-23 18:01:52 +02:00
|
|
|
type MapUpdate struct {
|
2019-10-30 22:21:13 +02:00
|
|
|
SingleKV core.KeyValue
|
|
|
|
MultiKV []core.KeyValue
|
2019-08-23 18:01:52 +02:00
|
|
|
}
|
2019-07-01 20:27:52 +02:00
|
|
|
|
2019-08-23 18:01:52 +02:00
|
|
|
func newMap(raw rawMap) Map {
|
|
|
|
return Map{
|
|
|
|
m: raw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEmptyMap() Map {
|
|
|
|
return newMap(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMap(update MapUpdate) Map {
|
|
|
|
return NewEmptyMap().Apply(update)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Map) Apply(update MapUpdate) Map {
|
2019-10-30 22:21:13 +02:00
|
|
|
r := make(rawMap, len(m.m)+len(update.MultiKV))
|
2019-08-23 18:01:52 +02:00
|
|
|
for k, v := range m.m {
|
|
|
|
r[k] = v
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
2019-07-12 00:28:38 +02:00
|
|
|
if update.SingleKV.Key.Defined() {
|
2019-10-10 23:30:22 +02:00
|
|
|
r[update.SingleKV.Key] = entry{
|
2019-07-12 00:28:38 +02:00
|
|
|
value: update.SingleKV.Value,
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 00:28:38 +02:00
|
|
|
for _, kv := range update.MultiKV {
|
2019-10-10 23:30:22 +02:00
|
|
|
r[kv.Key] = entry{
|
2019-06-29 01:35:43 +02:00
|
|
|
value: kv.Value,
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 18:01:52 +02:00
|
|
|
if len(r) == 0 {
|
|
|
|
r = nil
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
2019-08-23 18:01:52 +02:00
|
|
|
return newMap(r)
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) Value(k core.Key) (core.Value, bool) {
|
|
|
|
entry, ok := m.m[k]
|
2019-06-14 20:37:05 +02:00
|
|
|
return entry.value, ok
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) HasValue(k core.Key) bool {
|
2019-06-29 01:35:43 +02:00
|
|
|
_, has := m.Value(k)
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) Len() int {
|
|
|
|
return len(m.m)
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) Foreach(f func(kv core.KeyValue) bool) {
|
|
|
|
for k, v := range m.m {
|
2019-06-29 01:35:43 +02:00
|
|
|
if !f(core.KeyValue{
|
|
|
|
Key: k,
|
|
|
|
Value: v.value,
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|