// Copyright The OpenTelemetry Authors// SPDX-License-Identifier: Apache-2.0
/*Package attribute provide several helper functions for some commonly usedlogic 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.typesliceEleminterface {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.switchlen() {case0:return [0]{}case1:return [1]{[0]}case2:return [2]{[0], [1]}case3:return [3]{[0], [1], [2]} }returnsliceValueReflect()}// 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]} }returnasSliceReflect[]()}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[]() {returnnil } := make([], .Len())iflen() > 0 { _ = reflect.Copy(reflect.ValueOf(), ) }return}
The pages are generated with Goldsv0.8.4. (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.