attribute.go raw

   1  // Copyright The OpenTelemetry Authors
   2  // SPDX-License-Identifier: Apache-2.0
   3  
   4  /*
   5  Package attribute provide several helper functions for some commonly used
   6  logic of processing attributes.
   7  */
   8  package attribute // import "go.opentelemetry.io/otel/attribute/internal"
   9  
  10  import (
  11  	"reflect"
  12  )
  13  
  14  // BoolSliceValue converts a bool slice into an array with same elements as slice.
  15  func BoolSliceValue(v []bool) any {
  16  	var zero bool
  17  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
  18  	reflect.Copy(cp, reflect.ValueOf(v))
  19  	return cp.Interface()
  20  }
  21  
  22  // Int64SliceValue converts an int64 slice into an array with same elements as slice.
  23  func Int64SliceValue(v []int64) any {
  24  	var zero int64
  25  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
  26  	reflect.Copy(cp, reflect.ValueOf(v))
  27  	return cp.Interface()
  28  }
  29  
  30  // Float64SliceValue converts a float64 slice into an array with same elements as slice.
  31  func Float64SliceValue(v []float64) any {
  32  	var zero float64
  33  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
  34  	reflect.Copy(cp, reflect.ValueOf(v))
  35  	return cp.Interface()
  36  }
  37  
  38  // StringSliceValue converts a string slice into an array with same elements as slice.
  39  func StringSliceValue(v []string) any {
  40  	var zero string
  41  	cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
  42  	reflect.Copy(cp, reflect.ValueOf(v))
  43  	return cp.Interface()
  44  }
  45  
  46  // AsBoolSlice converts a bool array into a slice into with same elements as array.
  47  func AsBoolSlice(v any) []bool {
  48  	rv := reflect.ValueOf(v)
  49  	if rv.Type().Kind() != reflect.Array {
  50  		return nil
  51  	}
  52  	cpy := make([]bool, rv.Len())
  53  	if len(cpy) > 0 {
  54  		_ = reflect.Copy(reflect.ValueOf(cpy), rv)
  55  	}
  56  	return cpy
  57  }
  58  
  59  // AsInt64Slice converts an int64 array into a slice into with same elements as array.
  60  func AsInt64Slice(v any) []int64 {
  61  	rv := reflect.ValueOf(v)
  62  	if rv.Type().Kind() != reflect.Array {
  63  		return nil
  64  	}
  65  	cpy := make([]int64, rv.Len())
  66  	if len(cpy) > 0 {
  67  		_ = reflect.Copy(reflect.ValueOf(cpy), rv)
  68  	}
  69  	return cpy
  70  }
  71  
  72  // AsFloat64Slice converts a float64 array into a slice into with same elements as array.
  73  func AsFloat64Slice(v any) []float64 {
  74  	rv := reflect.ValueOf(v)
  75  	if rv.Type().Kind() != reflect.Array {
  76  		return nil
  77  	}
  78  	cpy := make([]float64, rv.Len())
  79  	if len(cpy) > 0 {
  80  		_ = reflect.Copy(reflect.ValueOf(cpy), rv)
  81  	}
  82  	return cpy
  83  }
  84  
  85  // AsStringSlice converts a string array into a slice into with same elements as array.
  86  func AsStringSlice(v any) []string {
  87  	rv := reflect.ValueOf(v)
  88  	if rv.Type().Kind() != reflect.Array {
  89  		return nil
  90  	}
  91  	cpy := make([]string, rv.Len())
  92  	if len(cpy) > 0 {
  93  		_ = reflect.Copy(reflect.ValueOf(cpy), rv)
  94  	}
  95  	return cpy
  96  }
  97