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

/* Package attribute provide several helper functions for some commonly used logic of processing attributes. */
package attribute // import "go.opentelemetry.io/otel/attribute/internal" import ( ) // sliceElem is the exact set of element types stored in attribute slice values. // Using a closed set prevents accidental instantiations for unsupported types. type sliceElem interface { bool | int64 | float64 | string } // SliceValue converts a slice into an array with the same elements. func [ sliceElem]( []) any { // Keep only the common tiny-slice cases out of reflection. Extending this // much further increases code size for diminishing benefit while larger // slices still need the generic reflective path to preserve comparability. // This matches the short lengths that show up most often in local // benchmarks and semantic convention examples while leaving larger, less // predictable slices on the generic reflective path. switch len() { case 0: return [0]{} case 1: return [1]{[0]} case 2: return [2]{[0], [1]} case 3: return [3]{[0], [1], [2]} } return sliceValueReflect() } // AsSlice converts an array into a slice with the same elements. func [ sliceElem]( any) [] { // Mirror the small fixed-array fast path used by SliceValue. switch a := .(type) { case [0]: return []{} case [1]: return []{[0]} case [2]: return []{[0], [1]} case [3]: return []{[0], [1], [2]} } return asSliceReflect[]() } func [ sliceElem]( []) any { := reflect.New(reflect.ArrayOf(len(), reflect.TypeFor[]())).Elem() reflect.Copy(, reflect.ValueOf()) return .Interface() } func [ sliceElem]( any) [] { := reflect.ValueOf() if !.IsValid() || .Kind() != reflect.Array || .Type().Elem() != reflect.TypeFor[]() { return nil } := make([], .Len()) if len() > 0 { _ = reflect.Copy(reflect.ValueOf(), ) } return }