package attrnorm
import (
"reflect"
"unsafe"
"go.opentelemetry.io/otel/attribute"
)
var (
keyValueType = reflect .TypeFor [attribute .KeyValue ]()
valueType = reflect .TypeFor [attribute .Value ]()
)
type rawValue struct {
vtype attribute .Type
numeric uint64
stringly string
slice any
}
type valueChanges uint8
const (
depthLimitChanged valueChanges = 1 << iota
deduplicationChanged
)
func ValueDedup (value attribute .Value ) (attribute .Value , bool ) {
switch value .Type () {
case attribute .SLICE :
return sliceValueDedup (value )
case attribute .MAP :
return mapValueDedup (value )
default :
return value , false
}
}
func ValueDedupLimitDepth (
value attribute .Value ,
depthLimit int ,
) (attribute .Value , bool , bool ) {
if depthLimit < 0 {
value , deduplicated := ValueDedup (value )
return value , false , deduplicated
}
switch value .Type () {
case attribute .BOOLSLICE ,
attribute .INT64SLICE ,
attribute .FLOAT64SLICE ,
attribute .STRINGSLICE ,
attribute .BYTESLICE :
if depthLimit == 0 {
return attribute .Value {}, true , false
}
return value , false , false
case attribute .SLICE , attribute .MAP :
default :
return value , false , false
}
value , changes := valueDedupLimitDepth (value , depthLimit )
return value , changes &depthLimitChanged != 0 , changes &deduplicationChanged != 0
}
func ValueLimitDepth (value attribute .Value , depthLimit int ) (attribute .Value , bool ) {
if depthLimit < 0 {
return value , false
}
switch value .Type () {
case attribute .BOOLSLICE ,
attribute .INT64SLICE ,
attribute .FLOAT64SLICE ,
attribute .STRINGSLICE ,
attribute .BYTESLICE :
if depthLimit == 0 {
return attribute .Value {}, true
}
return value , false
case attribute .SLICE , attribute .MAP :
default :
return value , false
}
return valueLimitDepth (value , depthLimit )
}
func KeyValueDedup (kv attribute .KeyValue ) (attribute .KeyValue , bool ) {
value , changed := ValueDedup (kv .Value )
if changed {
kv .Value = value
}
return kv , changed
}
func KeyValueDedupLimitDepth (
kv attribute .KeyValue ,
depthLimit int ,
) (attribute .KeyValue , bool , bool ) {
value , depthLimited , deduplicated := ValueDedupLimitDepth (kv .Value , depthLimit )
if depthLimited || deduplicated {
kv .Value = value
}
return kv , depthLimited , deduplicated
}
func KeyValueLimitDepth (kv attribute .KeyValue , depthLimit int ) (attribute .KeyValue , bool ) {
value , changed := ValueLimitDepth (kv .Value , depthLimit )
if changed {
kv .Value = value
}
return kv , changed
}
func KeyValuesDedup (kvs []attribute .KeyValue ) ([]attribute .KeyValue , bool ) {
var normalized []attribute .KeyValue
for i , kv := range kvs {
kv , changed := KeyValueDedup (kv )
if normalized != nil {
normalized [i ] = kv
continue
}
if !changed {
continue
}
normalized = make ([]attribute .KeyValue , len (kvs ))
copy (normalized , kvs [:i ])
normalized [i ] = kv
}
if normalized == nil {
return kvs , false
}
return normalized , true
}
func KeyValuesDedupLimitDepth (
kvs []attribute .KeyValue ,
depthLimit int ,
) ([]attribute .KeyValue , bool , bool ) {
if depthLimit < 0 {
kvs , deduplicated := KeyValuesDedup (kvs )
return kvs , false , deduplicated
}
var normalized []attribute .KeyValue
var allChanges valueChanges
for i , kv := range kvs {
value , changes := valueDedupLimitDepth (kv .Value , depthLimit )
allChanges |= changes
if changes != 0 {
kv .Value = value
}
if normalized != nil {
normalized [i ] = kv
continue
}
if changes == 0 {
continue
}
normalized = make ([]attribute .KeyValue , len (kvs ))
copy (normalized , kvs [:i ])
normalized [i ] = kv
}
if normalized == nil {
return kvs , false , false
}
return normalized ,
allChanges &depthLimitChanged != 0 ,
allChanges &deduplicationChanged != 0
}
func SetDedup (set attribute .Set ) (attribute .Set , bool ) {
length := set .Len ()
if length == 0 {
return set , false
}
var normalized []attribute .KeyValue
for i := range length {
kv , _ := set .Get (i )
kv , changed := KeyValueDedup (kv )
if normalized != nil {
normalized = append (normalized , kv )
continue
}
if !changed {
continue
}
normalized = make ([]attribute .KeyValue , 0 , length )
for j := range i {
prior , _ := set .Get (j )
normalized = append (normalized , prior )
}
normalized = append (normalized , kv )
}
if normalized == nil {
return set , false
}
return attribute .NewSet (normalized ...), true
}
func SetDedupLimitDepth (
set attribute .Set ,
depthLimit int ,
) (attribute .Set , bool , bool ) {
if depthLimit < 0 {
set , deduplicated := SetDedup (set )
return set , false , deduplicated
}
length := set .Len ()
if length == 0 {
return set , false , false
}
var normalized []attribute .KeyValue
var allChanges valueChanges
for i := range length {
kv , _ := set .Get (i )
value , changes := valueDedupLimitDepth (kv .Value , depthLimit )
allChanges |= changes
if changes != 0 {
kv .Value = value
}
if normalized != nil {
normalized = append (normalized , kv )
continue
}
if changes == 0 {
continue
}
normalized = make ([]attribute .KeyValue , 0 , length )
for j := range i {
prior , _ := set .Get (j )
normalized = append (normalized , prior )
}
normalized = append (normalized , kv )
}
if normalized == nil {
return set , false , false
}
return attribute .NewSet (normalized ...),
allChanges &depthLimitChanged != 0 ,
allChanges &deduplicationChanged != 0
}
func SetLimitDepth (set attribute .Set , depthLimit int ) (attribute .Set , bool ) {
if depthLimit < 0 {
return set , false
}
length := set .Len ()
if length == 0 {
return set , false
}
var normalized []attribute .KeyValue
for i := range length {
kv , _ := set .Get (i )
value , changed := valueLimitDepth (kv .Value , depthLimit )
if changed {
kv .Value = value
}
if normalized != nil {
normalized = append (normalized , kv )
continue
}
if !changed {
continue
}
normalized = make ([]attribute .KeyValue , 0 , length )
for j := range i {
prior , _ := set .Get (j )
normalized = append (normalized , prior )
}
normalized = append (normalized , kv )
}
if normalized == nil {
return set , false
}
return attribute .NewSet (normalized ...), true
}
func valueDedupLimitDepth(
value attribute .Value ,
remainingDepth int ,
) (attribute .Value , valueChanges ) {
switch value .Type () {
case attribute .BOOLSLICE ,
attribute .INT64SLICE ,
attribute .FLOAT64SLICE ,
attribute .STRINGSLICE ,
attribute .BYTESLICE :
if remainingDepth == 0 {
return attribute .Value {}, depthLimitChanged
}
case attribute .SLICE :
if remainingDepth == 0 {
return attribute .Value {}, depthLimitChanged
}
return sliceValueDedupLimitDepth (value , remainingDepth )
case attribute .MAP :
if remainingDepth == 0 {
return attribute .Value {}, depthLimitChanged
}
return mapValueDedupLimitDepth (value , remainingDepth )
}
return value , 0
}
func valueLimitDepth(
value attribute .Value ,
remainingDepth int ,
) (attribute .Value , bool ) {
switch value .Type () {
case attribute .BOOLSLICE ,
attribute .INT64SLICE ,
attribute .FLOAT64SLICE ,
attribute .STRINGSLICE ,
attribute .BYTESLICE :
if remainingDepth == 0 {
return attribute .Value {}, true
}
case attribute .SLICE :
if remainingDepth == 0 {
return attribute .Value {}, true
}
return sliceValueLimitDepth (value , remainingDepth )
case attribute .MAP :
if remainingDepth == 0 {
return attribute .Value {}, true
}
return mapValueLimitDepth (value , remainingDepth )
}
return value , false
}
func sliceValueDedupLimitDepth(
value attribute .Value ,
remainingDepth int ,
) (attribute .Value , valueChanges ) {
storage := valueStorage (value )
length := valueLen (storage )
if length <= 1 {
if length == 1 {
elem := valueAt (storage , 0 )
newElem , changes := valueDedupLimitDepth (
elem ,
remainingDepth -1 ,
)
if changes != 0 {
return attribute .SliceValue (newElem ), changes
}
}
return value , 0
}
var normalized []attribute .Value
var allChanges valueChanges
for i := range length {
elem := valueAt (storage , i )
newElem , changes := valueDedupLimitDepth (
elem ,
remainingDepth -1 ,
)
allChanges |= changes
if normalized != nil {
normalized [i ] = newElem
continue
}
if changes == 0 {
continue
}
normalized = make ([]attribute .Value , length )
for j := range i {
normalized [j ] = valueAt (storage , j )
}
normalized [i ] = newElem
}
if normalized == nil {
return value , 0
}
return attribute .SliceValue (normalized ...), allChanges
}
func sliceValueLimitDepth(
value attribute .Value ,
remainingDepth int ,
) (attribute .Value , bool ) {
storage := valueStorage (value )
length := valueLen (storage )
if length <= 1 {
if length == 1 {
elem := valueAt (storage , 0 )
newElem , changed := valueLimitDepth (elem , remainingDepth -1 )
if changed {
return attribute .SliceValue (newElem ), true
}
}
return value , false
}
var normalized []attribute .Value
for i := range length {
elem := valueAt (storage , i )
newElem , changed := valueLimitDepth (elem , remainingDepth -1 )
if normalized != nil {
normalized [i ] = newElem
continue
}
if !changed {
continue
}
normalized = make ([]attribute .Value , length )
for j := range i {
normalized [j ] = valueAt (storage , j )
}
normalized [i ] = newElem
}
if normalized == nil {
return value , false
}
return attribute .SliceValue (normalized ...), true
}
func mapValueDedupLimitDepth(
value attribute .Value ,
remainingDepth int ,
) (attribute .Value , valueChanges ) {
storage := valueStorage (value )
length := keyValueLen (storage )
if length <= 1 {
if length == 1 {
kv := keyValueAt (storage , 0 )
newValue , changes := valueDedupLimitDepth (
kv .Value ,
remainingDepth -1 ,
)
if changes != 0 {
kv .Value = newValue
return attribute .MapValue (kv ), changes
}
}
return value , 0
}
var normalized []attribute .KeyValue
var allChanges valueChanges
for i := 0 ; i < length ; {
first := keyValueAt (storage , i )
j := i + 1
for j < length && keyValueAt (storage , j ).Key == first .Key {
j ++
}
kv := keyValueAt (storage , j -1 )
newValue , changes := valueDedupLimitDepth (
kv .Value ,
remainingDepth -1 ,
)
if changes != 0 {
kv .Value = newValue
}
if j -i > 1 {
changes |= deduplicationChanged
}
allChanges |= changes
if normalized != nil {
normalized = append (normalized , kv )
} else if changes != 0 {
if i == 0 && j == length {
return attribute .MapValue (kv ), allChanges
}
normalized = make ([]attribute .KeyValue , 0 , length -(j -i -1 ))
for k := range i {
normalized = append (normalized , keyValueAt (storage , k ))
}
normalized = append (normalized , kv )
}
i = j
}
if normalized == nil {
return value , 0
}
return attribute .MapValue (normalized ...), allChanges
}
func mapValueLimitDepth(
value attribute .Value ,
remainingDepth int ,
) (attribute .Value , bool ) {
storage := valueStorage (value )
length := keyValueLen (storage )
if length <= 1 {
if length == 1 {
kv := keyValueAt (storage , 0 )
newValue , changed := valueLimitDepth (kv .Value , remainingDepth -1 )
if changed {
kv .Value = newValue
return attribute .MapValue (kv ), true
}
}
return value , false
}
var normalized []attribute .KeyValue
for i := range length {
kv := keyValueAt (storage , i )
newValue , changed := valueLimitDepth (kv .Value , remainingDepth -1 )
if changed {
kv .Value = newValue
}
if normalized != nil {
normalized [i ] = kv
continue
}
if !changed {
continue
}
normalized = make ([]attribute .KeyValue , length )
for j := range i {
normalized [j ] = keyValueAt (storage , j )
}
normalized [i ] = kv
}
if normalized == nil {
return value , false
}
return attribute .MapValue (normalized ...), true
}
func sliceValueDedup(value attribute .Value ) (attribute .Value , bool ) {
storage := valueStorage (value )
length := valueLen (storage )
var normalized []attribute .Value
for i := range length {
elem := valueAt (storage , i )
elem , changed := ValueDedup (elem )
if normalized != nil {
normalized [i ] = elem
continue
}
if !changed {
continue
}
normalized = make ([]attribute .Value , length )
for j := range i {
normalized [j ] = valueAt (storage , j )
}
normalized [i ] = elem
}
if normalized == nil {
return value , false
}
return attribute .SliceValue (normalized ...), true
}
func mapValueDedup(value attribute .Value ) (attribute .Value , bool ) {
storage := valueStorage (value )
length := keyValueLen (storage )
if length <= 1 {
if length == 1 {
kv , changed := KeyValueDedup (keyValueAt (storage , 0 ))
if changed {
return attribute .MapValue (kv ), true
}
}
return value , false
}
var normalized []attribute .KeyValue
for i := 0 ; i < length ; {
first := keyValueAt (storage , i )
j := i + 1
for j < length && keyValueAt (storage , j ).Key == first .Key {
j ++
}
kv , nestedChanged := KeyValueDedup (keyValueAt (storage , j -1 ))
changed := nestedChanged || j -i > 1
if normalized != nil {
normalized = append (normalized , kv )
} else if changed {
if i == 0 && j == length {
return attribute .MapValue (kv ), true
}
normalized = make ([]attribute .KeyValue , 0 , length -(j -i -1 ))
for k := range i {
normalized = append (normalized , keyValueAt (storage , k ))
}
normalized = append (normalized , kv )
}
i = j
}
if normalized == nil {
return value , false
}
return attribute .MapValue (normalized ...), true
}
func valueStorage(value attribute .Value ) any {
return (*rawValue )(
unsafe .Pointer (&value ),
).slice
}
func valueLen(storage any ) int {
switch storage .(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 (storage , valueType )
}
}
func valueAt(storage any , i int ) attribute .Value {
switch values := storage .(type ) {
case [1 ]attribute .Value :
return values [i ]
case [2 ]attribute .Value :
return values [i ]
case [3 ]attribute .Value :
return values [i ]
case [4 ]attribute .Value :
return values [i ]
case [5 ]attribute .Value :
return values [i ]
default :
return arrayAt [attribute .Value ](storage , valueType , i )
}
}
func keyValueLen(storage any ) int {
switch storage .(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 (storage , keyValueType )
}
}
func keyValueAt(storage any , i int ) attribute .KeyValue {
switch kvs := storage .(type ) {
case [1 ]attribute .KeyValue :
return kvs [i ]
case [2 ]attribute .KeyValue :
return kvs [i ]
case [3 ]attribute .KeyValue :
return kvs [i ]
case [4 ]attribute .KeyValue :
return kvs [i ]
case [5 ]attribute .KeyValue :
return kvs [i ]
default :
return arrayAt [attribute .KeyValue ](storage , keyValueType , i )
}
}
func arrayLen(storage any , elem reflect .Type ) int {
array := reflect .ValueOf (storage )
if array .Kind () != reflect .Array || array .Type ().Elem () != elem {
return 0
}
return array .Len ()
}
func arrayAt[T any ](storage any , elem reflect .Type , i int ) T {
array := reflect .ValueOf (storage )
if array .Kind () != reflect .Array || array .Type ().Elem () != elem || i < 0 || i >= array .Len () {
var zero T
return zero
}
return array .Index (i ).Interface ().(T )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .