attribute.go 3.8 KB

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