1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-14 10:13:10 +02:00
opentelemetry-go/api/distributedcontext/map.go

95 lines
1.8 KiB
Go
Raw Normal View History

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.
package distributedcontext
import (
"go.opentelemetry.io/api/core"
)
type entry struct {
2019-06-28 06:26:16 +02:00
value core.Value
}
type rawMap map[core.Key]entry
type Map struct {
m rawMap
}
2019-07-01 20:27:52 +02:00
type MapUpdate struct {
SingleKV core.KeyValue
MultiKV []core.KeyValue
}
2019-07-01 20:27: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 {
r := make(rawMap, len(m.m)+len(update.MultiKV))
for k, v := range m.m {
r[k] = v
}
if update.SingleKV.Key.Defined() {
r[update.SingleKV.Key] = entry{
value: update.SingleKV.Value,
}
}
for _, kv := range update.MultiKV {
r[kv.Key] = entry{
value: kv.Value,
}
}
if len(r) == 0 {
r = nil
}
return newMap(r)
}
func (m Map) Value(k core.Key) (core.Value, bool) {
entry, ok := m.m[k]
return entry.value, ok
}
func (m Map) HasValue(k core.Key) bool {
_, has := m.Value(k)
return has
}
func (m Map) Len() int {
return len(m.m)
}
func (m Map) Foreach(f func(kv core.KeyValue) bool) {
for k, v := range m.m {
if !f(core.KeyValue{
Key: k,
Value: v.value,
}) {
return
}
}
}