审查视图

vendor/go.opentelemetry.io/otel/label/value.go 7.2 KB
tangxvhui authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.
唐旭辉 authored
15
package label // import "go.opentelemetry.io/otel/label"
tangxvhui authored
16 17 18 19

import (
	"encoding/json"
	"fmt"
唐旭辉 authored
20
	"reflect"
tangxvhui authored
21 22 23
	"strconv"
	"unsafe"
唐旭辉 authored
24
	"go.opentelemetry.io/otel/internal"
tangxvhui authored
25 26 27 28 29 30 31 32 33 34 35 36 37
)

//go:generate stringer -type=Type

// Type describes the type of the data Value holds.
type Type int

// Value represents the value part in key-value pairs.
type Value struct {
	vtype    Type
	numeric  uint64
	stringly string
	// TODO Lazy value type?
唐旭辉 authored
38 39

	array interface{}
tangxvhui authored
40 41 42
}

const (
唐旭辉 authored
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	// INVALID is used for a Value with no value set.
	INVALID Type = iota
	// BOOL is a boolean Type Value.
	BOOL
	// INT32 is a 32-bit signed integral Type Value.
	INT32
	// INT64 is a 64-bit signed integral Type Value.
	INT64
	// UINT32 is a 32-bit unsigned integral Type Value.
	UINT32
	// UINT64 is a 64-bit unsigned integral Type Value.
	UINT64
	// FLOAT32 is a 32-bit floating point Type Value.
	FLOAT32
	// FLOAT64 is a 64-bit floating point Type Value.
	FLOAT64
	// STRING is a string Type Value.
	STRING
	// ARRAY is an array Type Value used to store 1-dimensional slices or
	// arrays of bool, int, int32, int64, uint, uint32, uint64, float,
	// float32, float64, or string types.
	ARRAY
tangxvhui authored
65 66
)
唐旭辉 authored
67 68
// BoolValue creates a BOOL Value.
func BoolValue(v bool) Value {
tangxvhui authored
69 70 71 72 73 74
	return Value{
		vtype:   BOOL,
		numeric: internal.BoolToRaw(v),
	}
}
唐旭辉 authored
75 76
// Int64Value creates an INT64 Value.
func Int64Value(v int64) Value {
tangxvhui authored
77 78 79 80 81 82
	return Value{
		vtype:   INT64,
		numeric: internal.Int64ToRaw(v),
	}
}
唐旭辉 authored
83 84
// Uint64Value creates a UINT64 Value.
func Uint64Value(v uint64) Value {
tangxvhui authored
85 86 87 88 89 90
	return Value{
		vtype:   UINT64,
		numeric: internal.Uint64ToRaw(v),
	}
}
唐旭辉 authored
91 92
// Float64Value creates a FLOAT64 Value.
func Float64Value(v float64) Value {
tangxvhui authored
93 94 95 96 97 98
	return Value{
		vtype:   FLOAT64,
		numeric: internal.Float64ToRaw(v),
	}
}
唐旭辉 authored
99 100
// Int32Value creates an INT32 Value.
func Int32Value(v int32) Value {
tangxvhui authored
101 102 103 104 105 106
	return Value{
		vtype:   INT32,
		numeric: internal.Int32ToRaw(v),
	}
}
唐旭辉 authored
107 108
// Uint32Value creates a UINT32 Value.
func Uint32Value(v uint32) Value {
tangxvhui authored
109 110 111 112 113 114
	return Value{
		vtype:   UINT32,
		numeric: internal.Uint32ToRaw(v),
	}
}
唐旭辉 authored
115 116
// Float32Value creates a FLOAT32 Value.
func Float32Value(v float32) Value {
tangxvhui authored
117 118 119 120 121 122
	return Value{
		vtype:   FLOAT32,
		numeric: internal.Float32ToRaw(v),
	}
}
唐旭辉 authored
123 124
// StringValue creates a STRING Value.
func StringValue(v string) Value {
tangxvhui authored
125 126 127 128 129 130
	return Value{
		vtype:    STRING,
		stringly: v,
	}
}
唐旭辉 authored
131
// IntValue creates either an INT32 or an INT64 Value, depending on whether
tangxvhui authored
132
// the int type is 32 or 64 bits wide.
唐旭辉 authored
133
func IntValue(v int) Value {
tangxvhui authored
134
	if unsafe.Sizeof(v) == 4 {
唐旭辉 authored
135
		return Int32Value(int32(v))
tangxvhui authored
136
	}
唐旭辉 authored
137
	return Int64Value(int64(v))
tangxvhui authored
138 139
}
唐旭辉 authored
140 141 142
// UintValue creates either a UINT32 or a UINT64 Value, depending on whether
// the uint type is 32 or 64 bits wide.
func UintValue(v uint) Value {
tangxvhui authored
143
	if unsafe.Sizeof(v) == 4 {
唐旭辉 authored
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
		return Uint32Value(uint32(v))
	}
	return Uint64Value(uint64(v))
}

// ArrayValue creates an ARRAY value from an array or slice.
// Only arrays or slices of bool, int, int32, int64, uint, uint32, uint64,
// float, float32, float64, or string types are allowed. Specifically, arrays
// and slices can not contain other arrays, slices, structs, or non-standard
// types. If the passed value is not an array or slice of these types an
// INVALID value is returned.
func ArrayValue(v interface{}) Value {
	switch reflect.TypeOf(v).Kind() {
	case reflect.Array, reflect.Slice:
		// get array type regardless of dimensions
		typ := reflect.TypeOf(v).Elem()
		kind := typ.Kind()
		switch kind {
		case reflect.Bool, reflect.Int, reflect.Int32, reflect.Int64,
			reflect.Float32, reflect.Float64, reflect.String,
			reflect.Uint, reflect.Uint32, reflect.Uint64:
			val := reflect.ValueOf(v)
			length := val.Len()
			frozen := reflect.Indirect(reflect.New(reflect.ArrayOf(length, typ)))
			reflect.Copy(frozen, val)
			return Value{
				vtype: ARRAY,
				array: frozen.Interface(),
			}
		default:
			return Value{vtype: INVALID}
		}
tangxvhui authored
176
	}
唐旭辉 authored
177
	return Value{vtype: INVALID}
tangxvhui authored
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
}

// Type returns a type of the Value.
func (v Value) Type() Type {
	return v.vtype
}

// AsBool returns the bool value. Make sure that the Value's type is
// BOOL.
func (v Value) AsBool() bool {
	return internal.RawToBool(v.numeric)
}

// AsInt32 returns the int32 value. Make sure that the Value's type is
// INT32.
func (v Value) AsInt32() int32 {
	return internal.RawToInt32(v.numeric)
}

// AsInt64 returns the int64 value. Make sure that the Value's type is
// INT64.
func (v Value) AsInt64() int64 {
	return internal.RawToInt64(v.numeric)
}

// AsUint32 returns the uint32 value. Make sure that the Value's type
// is UINT32.
func (v Value) AsUint32() uint32 {
	return internal.RawToUint32(v.numeric)
}

// AsUint64 returns the uint64 value. Make sure that the Value's type is
// UINT64.
func (v Value) AsUint64() uint64 {
	return internal.RawToUint64(v.numeric)
}

// AsFloat32 returns the float32 value. Make sure that the Value's
// type is FLOAT32.
func (v Value) AsFloat32() float32 {
	return internal.RawToFloat32(v.numeric)
}

// AsFloat64 returns the float64 value. Make sure that the Value's
// type is FLOAT64.
func (v Value) AsFloat64() float64 {
	return internal.RawToFloat64(v.numeric)
}

// AsString returns the string value. Make sure that the Value's type
// is STRING.
func (v Value) AsString() string {
	return v.stringly
}
唐旭辉 authored
233 234 235 236 237
// AsArray returns the array Value as an interface{}.
func (v Value) AsArray() interface{} {
	return v.array
}
tangxvhui authored
238 239 240 241 242
type unknownValueType struct{}

// AsInterface returns Value's data as interface{}.
func (v Value) AsInterface() interface{} {
	switch v.Type() {
唐旭辉 authored
243 244
	case ARRAY:
		return v.AsArray()
tangxvhui authored
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	case BOOL:
		return v.AsBool()
	case INT32:
		return v.AsInt32()
	case INT64:
		return v.AsInt64()
	case UINT32:
		return v.AsUint32()
	case UINT64:
		return v.AsUint64()
	case FLOAT32:
		return v.AsFloat32()
	case FLOAT64:
		return v.AsFloat64()
	case STRING:
		return v.stringly
	}
	return unknownValueType{}
}

// Emit returns a string representation of Value's data.
func (v Value) Emit() string {
	switch v.Type() {
唐旭辉 authored
268 269
	case ARRAY:
		return fmt.Sprint(v.array)
tangxvhui authored
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	case BOOL:
		return strconv.FormatBool(v.AsBool())
	case INT32:
		return strconv.FormatInt(int64(v.AsInt32()), 10)
	case INT64:
		return strconv.FormatInt(v.AsInt64(), 10)
	case UINT32:
		return strconv.FormatUint(uint64(v.AsUint32()), 10)
	case UINT64:
		return strconv.FormatUint(v.AsUint64(), 10)
	case FLOAT32:
		return fmt.Sprint(v.AsFloat32())
	case FLOAT64:
		return fmt.Sprint(v.AsFloat64())
	case STRING:
		return v.stringly
	default:
		return "unknown"
	}
}

// MarshalJSON returns the JSON encoding of the Value.
func (v Value) MarshalJSON() ([]byte, error) {
	var jsonVal struct {
		Type  string
		Value interface{}
	}
	jsonVal.Type = v.Type().String()
	jsonVal.Value = v.AsInterface()
	return json.Marshal(jsonVal)
}