// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// DO NOT MODIFY. Generated by gotmpl.
// source: internal/shared/attrnorm/dedup.go.tmpl

// Package attrnorm normalizes attribute values.
package attrnorm import ( ) var ( keyValueType = reflect.TypeFor[attribute.KeyValue]() valueType = reflect.TypeFor[attribute.Value]() ) // rawValue mirrors attribute.Value. It is used only to read immutable slice // storage without calling AsMap or AsSlice on no-op paths. type rawValue struct { vtype attribute.Type numeric uint64 stringly string slice any } type valueChanges uint8 const ( depthLimitChanged valueChanges = 1 << iota deduplicationChanged ) // ValueDedup returns value with all map values deduplicated and whether // it changed. // // Duplicate map keys are resolved using last-value-wins semantics. func ( attribute.Value) (attribute.Value, bool) { switch .Type() { case attribute.SLICE: return sliceValueDedup() case attribute.MAP: return mapValueDedup() default: return , false } } // ValueDedupLimitDepth returns value with all map values // deduplicated and all array and map values limited to depthLimit. The boolean // results report depth limiting and deduplication changes, respectively. // // Duplicate map keys are resolved using last-value-wins semantics. Depth // starts at one for value and increments when descending into an array element // or map value. An array or map beyond a non-negative depthLimit is replaced by // an empty value. A negative depthLimit disables depth limiting. func ( attribute.Value, int, ) (attribute.Value, bool, bool) { if < 0 { , := ValueDedup() return , false, } // Keep the common scalar and primitive-array cases in this wrapper. Routing // them through the recursive, multi-result transform adds measurable // overhead to the dominant simple attribute path. switch .Type() { case attribute.BOOLSLICE, attribute.INT64SLICE, attribute.FLOAT64SLICE, attribute.STRINGSLICE, attribute.BYTESLICE: if == 0 { return attribute.Value{}, true, false } return , false, false case attribute.SLICE, attribute.MAP: default: return , false, false } , := valueDedupLimitDepth(, ) return , &depthLimitChanged != 0, &deduplicationChanged != 0 } // ValueLimitDepth returns value with all array and map values limited to // depthLimit. Map keys are not deduplicated. // // Depth starts at one for value and increments when descending into an array // element or map value. An array or map beyond a non-negative depthLimit is // replaced by an empty value. A negative depthLimit disables depth limiting. func ( attribute.Value, int) (attribute.Value, bool) { if < 0 { return , false } switch .Type() { case attribute.BOOLSLICE, attribute.INT64SLICE, attribute.FLOAT64SLICE, attribute.STRINGSLICE, attribute.BYTESLICE: if == 0 { return attribute.Value{}, true } return , false case attribute.SLICE, attribute.MAP: default: return , false } return valueLimitDepth(, ) } // KeyValueDedup returns kv with all map values deduplicated and whether // it changed. func ( attribute.KeyValue) (attribute.KeyValue, bool) { , := ValueDedup(.Value) if { .Value = } return , } // KeyValueDedupLimitDepth returns kv with all map values // deduplicated and all array and map values limited to depthLimit. The boolean // results report depth limiting and deduplication changes, respectively. func ( attribute.KeyValue, int, ) (attribute.KeyValue, bool, bool) { , , := ValueDedupLimitDepth(.Value, ) if || { .Value = } return , , } // KeyValueLimitDepth returns kv with all array and map values limited to // depthLimit. Map keys are not deduplicated. func ( attribute.KeyValue, int) (attribute.KeyValue, bool) { , := ValueLimitDepth(.Value, ) if { .Value = } return , } // KeyValuesDedup returns kvs with all map values deduplicated and whether // they changed. // // The returned slice is the original kvs slice if no value needs // deduplication. Top-level keys in kvs are not deduplicated. func ( []attribute.KeyValue) ([]attribute.KeyValue, bool) { // Preserve the caller's slice on the common no-op path. Once a changed // value is found, copy the prior values exactly once and fill the rest in // place as the scan continues. var []attribute.KeyValue for , := range { , := KeyValueDedup() if != nil { [] = continue } if ! { continue } = make([]attribute.KeyValue, len()) copy(, [:]) [] = } if == nil { return , false } return , true } // KeyValuesDedupLimitDepth returns kvs with all map values // deduplicated and all array and map values limited to depthLimit. The boolean // results report depth limiting and deduplication changes, respectively. // // The returned slice is the original kvs slice if no value needs // normalization. Top-level keys in kvs are not deduplicated. func ( []attribute.KeyValue, int, ) ([]attribute.KeyValue, bool, bool) { if < 0 { , := KeyValuesDedup() return , false, } var []attribute.KeyValue var valueChanges for , := range { , := valueDedupLimitDepth(.Value, ) |= if != 0 { .Value = } if != nil { [] = continue } if == 0 { continue } = make([]attribute.KeyValue, len()) copy(, [:]) [] = } if == nil { return , false, false } return , &depthLimitChanged != 0, &deduplicationChanged != 0 } // SetDedup returns set with all map values deduplicated and whether it // changed. // // The returned Set is the original set if no value needs deduplication. // Top-level key uniqueness remains attribute.Set's responsibility; this only // normalizes map attribute values. func ( attribute.Set) (attribute.Set, bool) { := .Len() if == 0 { return , false } // Most attribute sets contain no duplicate map keys. Delay allocation until // the first changed value so the no-op path returns the original Set. var []attribute.KeyValue for := range { , := .Get() , := KeyValueDedup() if != nil { = append(, ) continue } if ! { continue } = make([]attribute.KeyValue, 0, ) for := range { , := .Get() = append(, ) } = append(, ) } if == nil { return , false } return attribute.NewSet(...), true } // SetDedupLimitDepth returns set with all map values deduplicated and // all array and map values limited to depthLimit. The boolean results report // depth limiting and deduplication changes, respectively. // // The returned Set is the original set if no value needs normalization. // Top-level key uniqueness remains attribute.Set's responsibility; this only // normalizes attribute values. func ( attribute.Set, int, ) (attribute.Set, bool, bool) { if < 0 { , := SetDedup() return , false, } := .Len() if == 0 { return , false, false } var []attribute.KeyValue var valueChanges for := range { , := .Get() , := valueDedupLimitDepth(.Value, ) |= if != 0 { .Value = } if != nil { = append(, ) continue } if == 0 { continue } = make([]attribute.KeyValue, 0, ) for := range { , := .Get() = append(, ) } = append(, ) } if == nil { return , false, false } return attribute.NewSet(...), &depthLimitChanged != 0, &deduplicationChanged != 0 } // SetLimitDepth returns set with all array and map values limited to // depthLimit. Map keys are not deduplicated. // // The returned Set is the original set if no value changes. func ( attribute.Set, int) (attribute.Set, bool) { if < 0 { return , false } := .Len() if == 0 { return , false } var []attribute.KeyValue for := range { , := .Get() , := valueLimitDepth(.Value, ) if { .Value = } if != nil { = append(, ) continue } if ! { continue } = make([]attribute.KeyValue, 0, ) for := range { , := .Get() = append(, ) } = append(, ) } if == nil { return , false } return attribute.NewSet(...), true } // valueDedupLimitDepth combines depth limiting with recursive map // deduplication. remainingDepth is non-negative. It checks the remaining // budget before reading collection storage and lazily rebuilds only changed // ancestry. func valueDedupLimitDepth( attribute.Value, int, ) (attribute.Value, valueChanges) { switch .Type() { case attribute.BOOLSLICE, attribute.INT64SLICE, attribute.FLOAT64SLICE, attribute.STRINGSLICE, attribute.BYTESLICE: if == 0 { return attribute.Value{}, depthLimitChanged } case attribute.SLICE: if == 0 { return attribute.Value{}, depthLimitChanged } return sliceValueDedupLimitDepth(, ) case attribute.MAP: if == 0 { return attribute.Value{}, depthLimitChanged } return mapValueDedupLimitDepth(, ) } return , 0 } // valueLimitDepth limits collection depth while preserving duplicate map keys. func valueLimitDepth( attribute.Value, int, ) (attribute.Value, bool) { switch .Type() { case attribute.BOOLSLICE, attribute.INT64SLICE, attribute.FLOAT64SLICE, attribute.STRINGSLICE, attribute.BYTESLICE: if == 0 { return attribute.Value{}, true } case attribute.SLICE: if == 0 { return attribute.Value{}, true } return sliceValueLimitDepth(, ) case attribute.MAP: if == 0 { return attribute.Value{}, true } return mapValueLimitDepth(, ) } return , false } func sliceValueDedupLimitDepth( attribute.Value, int, ) (attribute.Value, valueChanges) { := valueStorage() := valueLen() if <= 1 { if == 1 { := valueAt(, 0) , := valueDedupLimitDepth( , -1, ) if != 0 { return attribute.SliceValue(), } } return , 0 } var []attribute.Value var valueChanges for := range { := valueAt(, ) , := valueDedupLimitDepth( , -1, ) |= if != nil { [] = continue } if == 0 { continue } = make([]attribute.Value, ) for := range { [] = valueAt(, ) } [] = } if == nil { return , 0 } return attribute.SliceValue(...), } func sliceValueLimitDepth( attribute.Value, int, ) (attribute.Value, bool) { := valueStorage() := valueLen() if <= 1 { if == 1 { := valueAt(, 0) , := valueLimitDepth(, -1) if { return attribute.SliceValue(), true } } return , false } var []attribute.Value for := range { := valueAt(, ) , := valueLimitDepth(, -1) if != nil { [] = continue } if ! { continue } = make([]attribute.Value, ) for := range { [] = valueAt(, ) } [] = } if == nil { return , false } return attribute.SliceValue(...), true } func mapValueDedupLimitDepth( attribute.Value, int, ) (attribute.Value, valueChanges) { := valueStorage() := keyValueLen() if <= 1 { if == 1 { := keyValueAt(, 0) , := valueDedupLimitDepth( .Value, -1, ) if != 0 { .Value = return attribute.MapValue(), } } return , 0 } var []attribute.KeyValue var valueChanges for := 0; < ; { // attribute.MapValue stores key-values sorted by key using a stable // sort. Only the last entry in an equal-key run survives, so avoid // traversing values that will be discarded. := keyValueAt(, ) := + 1 for < && keyValueAt(, ).Key == .Key { ++ } := keyValueAt(, -1) , := valueDedupLimitDepth( .Value, -1, ) if != 0 { .Value = } if - > 1 { |= deduplicationChanged } |= if != nil { = append(, ) } else if != 0 { if == 0 && == { return attribute.MapValue(), } = make([]attribute.KeyValue, 0, -(--1)) for := range { = append(, keyValueAt(, )) } = append(, ) } = } if == nil { return , 0 } return attribute.MapValue(...), } func mapValueLimitDepth( attribute.Value, int, ) (attribute.Value, bool) { := valueStorage() := keyValueLen() if <= 1 { if == 1 { := keyValueAt(, 0) , := valueLimitDepth(.Value, -1) if { .Value = return attribute.MapValue(), true } } return , false } var []attribute.KeyValue for := range { := keyValueAt(, ) , := valueLimitDepth(.Value, -1) if { .Value = } if != nil { [] = continue } if ! { continue } = make([]attribute.KeyValue, ) for := range { [] = keyValueAt(, ) } [] = } if == nil { return , false } return attribute.MapValue(...), true } func sliceValueDedup( attribute.Value) (attribute.Value, bool) { := valueStorage() := valueLen() // Slice values can contain map values, so recurse into each element while // keeping the original attribute.Value when no element changes. var []attribute.Value for := range { := valueAt(, ) , := ValueDedup() if != nil { [] = continue } if ! { continue } = make([]attribute.Value, ) for := range { [] = valueAt(, ) } [] = } if == nil { return , false } return attribute.SliceValue(...), true } func mapValueDedup( attribute.Value) (attribute.Value, bool) { := valueStorage() := keyValueLen() if <= 1 { // A single map entry cannot duplicate its own key, but its value might // contain a map or slice that needs recursive normalization. if == 1 { , := KeyValueDedup(keyValueAt(, 0)) if { return attribute.MapValue(), true } } return , false } var []attribute.KeyValue for := 0; < ; { // attribute.MapValue stores key-values sorted by key using a stable // sort. Equal keys therefore form a contiguous run, and the last // element in that run is the last value provided by the caller. := keyValueAt(, ) := + 1 for < && keyValueAt(, ).Key == .Key { ++ } , := KeyValueDedup(keyValueAt(, -1)) // j-i > 1 means the current key run contained duplicates. := || - > 1 if != nil { = append(, ) } else if { if == 0 && == { return attribute.MapValue(), true } = make([]attribute.KeyValue, 0, -(--1)) for := range { = append(, keyValueAt(, )) } = append(, ) } = } if == nil { return , false } return attribute.MapValue(...), true } func valueStorage( attribute.Value) any { // attribute.Value does not expose allocation-free map/slice iteration. // The raw mirror lets us read the immutable backing array directly and // reserve AsMap/AsSlice-style allocation for paths that actually change. return (*rawValue)( unsafe.Pointer(&), ).slice //nolint:gosec // Read-only mirror of attribute.Value for allocation-free iteration. } func valueLen( any) int { // attribute.Value stores small slices in fixed-size array values. Handle // the common sizes directly and fall back to reflection for larger arrays. switch .(type) { case [0]attribute.Value: return 0 case [1]attribute.Value: return 1 case [2]attribute.Value: return 2 case [3]attribute.Value: return 3 case [4]attribute.Value: return 4 case [5]attribute.Value: return 5 default: return arrayLen(, valueType) } } func valueAt( any, int) attribute.Value { switch values := .(type) { case [1]attribute.Value: return [] case [2]attribute.Value: return [] case [3]attribute.Value: return [] case [4]attribute.Value: return [] case [5]attribute.Value: return [] default: return arrayAt[attribute.Value](, valueType, ) } } func keyValueLen( any) int { // attribute.Value stores small maps in fixed-size key-value arrays. Handle // the common sizes directly and fall back to reflection for larger arrays. switch .(type) { case [0]attribute.KeyValue: return 0 case [1]attribute.KeyValue: return 1 case [2]attribute.KeyValue: return 2 case [3]attribute.KeyValue: return 3 case [4]attribute.KeyValue: return 4 case [5]attribute.KeyValue: return 5 default: return arrayLen(, keyValueType) } } func keyValueAt( any, int) attribute.KeyValue { switch kvs := .(type) { case [1]attribute.KeyValue: return [] case [2]attribute.KeyValue: return [] case [3]attribute.KeyValue: return [] case [4]attribute.KeyValue: return [] case [5]attribute.KeyValue: return [] default: return arrayAt[attribute.KeyValue](, keyValueType, ) } } func arrayLen( any, reflect.Type) int { // Be defensive around invalid or unexpected Value storage. Returning zero // makes malformed storage a no-op instead of panicking in telemetry paths. := reflect.ValueOf() if .Kind() != reflect.Array || .Type().Elem() != { return 0 } return .Len() } func arrayAt[ any]( any, reflect.Type, int) { // Match arrayLen's fail-closed behavior for unexpected storage. := reflect.ValueOf() if .Kind() != reflect.Array || .Type().Elem() != || < 0 || >= .Len() { var return } return .Index().Interface().() }