1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00

chore: move functionality from internal/rawhelpers.go to attribute (#6578)

Resolve #6524

---------

Signed-off-by: igrucci <babichevigor12@gmail.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
This commit is contained in:
igrucci
2025-04-10 19:04:50 +03:00
committed by GitHub
parent a486ca5722
commit cc43e01c27
4 changed files with 44 additions and 56 deletions

37
attribute/rawhelpers.go Normal file
View File

@@ -0,0 +1,37 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package attribute // import "go.opentelemetry.io/otel/attribute"
import (
"math"
)
func boolToRaw(b bool) uint64 { // nolint:revive // b is not a control flag.
if b {
return 1
}
return 0
}
func rawToBool(r uint64) bool {
return r != 0
}
func int64ToRaw(i int64) uint64 {
// Assumes original was a valid int64 (overflow not checked).
return uint64(i) // nolint: gosec
}
func rawToInt64(r uint64) int64 {
// Assumes original was a valid int64 (overflow not checked).
return int64(r) // nolint: gosec
}
func float64ToRaw(f float64) uint64 {
return math.Float64bits(f)
}
func rawToFloat64(r uint64) float64 {
return math.Float64frombits(r)
}

View File

@@ -10,7 +10,6 @@ import (
"strconv" "strconv"
attribute "go.opentelemetry.io/otel/attribute/internal" attribute "go.opentelemetry.io/otel/attribute/internal"
"go.opentelemetry.io/otel/internal"
) )
//go:generate stringer -type=Type //go:generate stringer -type=Type
@@ -51,7 +50,7 @@ const (
func BoolValue(v bool) Value { func BoolValue(v bool) Value {
return Value{ return Value{
vtype: BOOL, vtype: BOOL,
numeric: internal.BoolToRaw(v), numeric: boolToRaw(v),
} }
} }
@@ -82,7 +81,7 @@ func IntSliceValue(v []int) Value {
func Int64Value(v int64) Value { func Int64Value(v int64) Value {
return Value{ return Value{
vtype: INT64, vtype: INT64,
numeric: internal.Int64ToRaw(v), numeric: int64ToRaw(v),
} }
} }
@@ -95,7 +94,7 @@ func Int64SliceValue(v []int64) Value {
func Float64Value(v float64) Value { func Float64Value(v float64) Value {
return Value{ return Value{
vtype: FLOAT64, vtype: FLOAT64,
numeric: internal.Float64ToRaw(v), numeric: float64ToRaw(v),
} }
} }
@@ -125,7 +124,7 @@ func (v Value) Type() Type {
// AsBool returns the bool value. Make sure that the Value's type is // AsBool returns the bool value. Make sure that the Value's type is
// BOOL. // BOOL.
func (v Value) AsBool() bool { func (v Value) AsBool() bool {
return internal.RawToBool(v.numeric) return rawToBool(v.numeric)
} }
// AsBoolSlice returns the []bool value. Make sure that the Value's type is // AsBoolSlice returns the []bool value. Make sure that the Value's type is
@@ -144,7 +143,7 @@ func (v Value) asBoolSlice() []bool {
// AsInt64 returns the int64 value. Make sure that the Value's type is // AsInt64 returns the int64 value. Make sure that the Value's type is
// INT64. // INT64.
func (v Value) AsInt64() int64 { func (v Value) AsInt64() int64 {
return internal.RawToInt64(v.numeric) return rawToInt64(v.numeric)
} }
// AsInt64Slice returns the []int64 value. Make sure that the Value's type is // AsInt64Slice returns the []int64 value. Make sure that the Value's type is
@@ -163,7 +162,7 @@ func (v Value) asInt64Slice() []int64 {
// AsFloat64 returns the float64 value. Make sure that the Value's // AsFloat64 returns the float64 value. Make sure that the Value's
// type is FLOAT64. // type is FLOAT64.
func (v Value) AsFloat64() float64 { func (v Value) AsFloat64() float64 {
return internal.RawToFloat64(v.numeric) return rawToFloat64(v.numeric)
} }
// AsFloat64Slice returns the []float64 value. Make sure that the Value's type is // AsFloat64Slice returns the []float64 value. Make sure that the Value's type is

View File

@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors // Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// Package internal contains utility functions and internal implementations
package internal // import "go.opentelemetry.io/otel/internal" package internal // import "go.opentelemetry.io/otel/internal"
//go:generate gotmpl --body=./shared/matchers/expectation.go.tmpl "--data={}" --out=matchers/expectation.go //go:generate gotmpl --body=./shared/matchers/expectation.go.tmpl "--data={}" --out=matchers/expectation.go

View File

@@ -1,49 +0,0 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package internal provides non-public functionality.
package internal // import "go.opentelemetry.io/otel/internal"
import (
"math"
"unsafe"
)
func BoolToRaw(b bool) uint64 { // nolint:revive // b is not a control flag.
if b {
return 1
}
return 0
}
func RawToBool(r uint64) bool {
return r != 0
}
func Int64ToRaw(i int64) uint64 {
// Assumes original was a valid int64 (overflow not checked).
return uint64(i) // nolint: gosec
}
func RawToInt64(r uint64) int64 {
// Assumes original was a valid int64 (overflow not checked).
return int64(r) // nolint: gosec
}
func Float64ToRaw(f float64) uint64 {
return math.Float64bits(f)
}
func RawToFloat64(r uint64) float64 {
return math.Float64frombits(r)
}
func RawPtrToFloat64Ptr(r *uint64) *float64 {
// Assumes original was a valid *float64 (overflow not checked).
return (*float64)(unsafe.Pointer(r)) // nolint: gosec
}
func RawPtrToInt64Ptr(r *uint64) *int64 {
// Assumes original was a valid *int64 (overflow not checked).
return (*int64)(unsafe.Pointer(r)) // nolint: gosec
}