github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.opentelemetry.io/otel/internal/attribute/attribute.go (about)

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  /*
    16  Package attribute provide several helper functions for some commonly used
    17  logic of processing attributes.
    18  */
    19  package attribute // import "go.opentelemetry.io/otel/internal/attribute"
    20  
    21  import (
    22  	"reflect"
    23  )
    24  
    25  // BoolSliceValue converts a bool slice into an array with same elements as slice.
    26  func BoolSliceValue(v []bool) interface{} {
    27  	var zero bool
    28  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
    29  	copy(cp.Elem().Slice(0, len(v)).Interface().([]bool), v)
    30  	return cp.Elem().Interface()
    31  }
    32  
    33  // Int64SliceValue converts an int64 slice into an array with same elements as slice.
    34  func Int64SliceValue(v []int64) interface{} {
    35  	var zero int64
    36  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
    37  	copy(cp.Elem().Slice(0, len(v)).Interface().([]int64), v)
    38  	return cp.Elem().Interface()
    39  }
    40  
    41  // Float64SliceValue converts a float64 slice into an array with same elements as slice.
    42  func Float64SliceValue(v []float64) interface{} {
    43  	var zero float64
    44  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
    45  	copy(cp.Elem().Slice(0, len(v)).Interface().([]float64), v)
    46  	return cp.Elem().Interface()
    47  }
    48  
    49  // StringSliceValue converts a string slice into an array with same elements as slice.
    50  func StringSliceValue(v []string) interface{} {
    51  	var zero string
    52  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
    53  	copy(cp.Elem().Slice(0, len(v)).Interface().([]string), v)
    54  	return cp.Elem().Interface()
    55  }
    56  
    57  // AsBoolSlice converts a bool array into a slice into with same elements as array.
    58  func AsBoolSlice(v interface{}) []bool {
    59  	rv := reflect.ValueOf(v)
    60  	if rv.Type().Kind() != reflect.Array {
    61  		return nil
    62  	}
    63  	var zero bool
    64  	correctLen := rv.Len()
    65  	correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero))
    66  	cpy := reflect.New(correctType)
    67  	_ = reflect.Copy(cpy.Elem(), rv)
    68  	return cpy.Elem().Slice(0, correctLen).Interface().([]bool)
    69  }
    70  
    71  // AsInt64Slice converts an int64 array into a slice into with same elements as array.
    72  func AsInt64Slice(v interface{}) []int64 {
    73  	rv := reflect.ValueOf(v)
    74  	if rv.Type().Kind() != reflect.Array {
    75  		return nil
    76  	}
    77  	var zero int64
    78  	correctLen := rv.Len()
    79  	correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero))
    80  	cpy := reflect.New(correctType)
    81  	_ = reflect.Copy(cpy.Elem(), rv)
    82  	return cpy.Elem().Slice(0, correctLen).Interface().([]int64)
    83  }
    84  
    85  // AsFloat64Slice converts a float64 array into a slice into with same elements as array.
    86  func AsFloat64Slice(v interface{}) []float64 {
    87  	rv := reflect.ValueOf(v)
    88  	if rv.Type().Kind() != reflect.Array {
    89  		return nil
    90  	}
    91  	var zero float64
    92  	correctLen := rv.Len()
    93  	correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero))
    94  	cpy := reflect.New(correctType)
    95  	_ = reflect.Copy(cpy.Elem(), rv)
    96  	return cpy.Elem().Slice(0, correctLen).Interface().([]float64)
    97  }
    98  
    99  // AsStringSlice converts a string array into a slice into with same elements as array.
   100  func AsStringSlice(v interface{}) []string {
   101  	rv := reflect.ValueOf(v)
   102  	if rv.Type().Kind() != reflect.Array {
   103  		return nil
   104  	}
   105  	var zero string
   106  	correctLen := rv.Len()
   107  	correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero))
   108  	cpy := reflect.New(correctType)
   109  	_ = reflect.Copy(cpy.Elem(), rv)
   110  	return cpy.Elem().Slice(0, correctLen).Interface().([]string)
   111  }