-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attribute: fix slice related function bug (#3252)
* attribute: fix slice related function bug Signed-off-by: Ziqi Zhao <[email protected]> Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Chester Cheung <[email protected]>
- Loading branch information
1 parent
e4bdfe7
commit a8b9ddc
Showing
5 changed files
with
150 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/* | ||
Package attribute provide several helper functions for some commonly used | ||
logic of processing attributes. | ||
*/ | ||
package attribute // import "go.opentelemetry.io/otel/internal/attribute" | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// SliceValue convert a slice into an array with same elements as slice. | ||
func SliceValue[T bool | int64 | float64 | string](v []T) any { | ||
var zero T | ||
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))) | ||
copy(cp.Elem().Slice(0, len(v)).Interface().([]T), v) | ||
return cp.Elem().Interface() | ||
} | ||
|
||
// AsSlice convert an array into a slice into with same elements as array. | ||
func AsSlice[T bool | int64 | float64 | string](v any) []T { | ||
rv := reflect.ValueOf(v) | ||
if rv.Type().Kind() != reflect.Array { | ||
return nil | ||
} | ||
var zero T | ||
correctLen := rv.Len() | ||
correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero)) | ||
cpy := reflect.New(correctType) | ||
_ = reflect.Copy(cpy.Elem(), rv) | ||
return cpy.Elem().Slice(0, correctLen).Interface().([]T) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters