| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package telemetry // import "go.opentelemetry.io/otel/trace/internal/telemetry" |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"strconv" |
| 9 |
) |
| 10 |
|
| 11 |
// protoInt64 represents the protobuf encoding of integers which can be either |
| 12 |
// strings or integers. |
| 13 |
type protoInt64 int64 |
| 14 |
|
| 15 |
// Int64 returns the protoInt64 as an int64. |
| 16 |
func (i *protoInt64) Int64() int64 { return int64(*i) } |
| 17 |
|
| 18 |
// UnmarshalJSON decodes both strings and integers. |
| 19 |
func (i *protoInt64) UnmarshalJSON(data []byte) error { |
| 20 |
if data[0] == '"' { |
| 21 |
var str string |
| 22 |
if err := json.Unmarshal(data, &str); err != nil { |
| 23 |
return err |
| 24 |
} |
| 25 |
parsedInt, err := strconv.ParseInt(str, 10, 64) |
| 26 |
if err != nil { |
| 27 |
return err |
| 28 |
} |
| 29 |
*i = protoInt64(parsedInt) |
| 30 |
} else { |
| 31 |
var parsedInt int64 |
| 32 |
if err := json.Unmarshal(data, &parsedInt); err != nil { |
| 33 |
return err |
| 34 |
} |
| 35 |
*i = protoInt64(parsedInt) |
| 36 |
} |
| 37 |
return nil |
| 38 |
} |
| 39 |
|
| 40 |
// protoUint64 represents the protobuf encoding of integers which can be either |
| 41 |
// strings or integers. |
| 42 |
type protoUint64 uint64 |
| 43 |
|
| 44 |
// Int64 returns the protoUint64 as a uint64. |
| 45 |
func (i *protoUint64) Uint64() uint64 { return uint64(*i) } |
| 46 |
|
| 47 |
// UnmarshalJSON decodes both strings and integers. |
| 48 |
func (i *protoUint64) UnmarshalJSON(data []byte) error { |
| 49 |
if data[0] == '"' { |
| 50 |
var str string |
| 51 |
if err := json.Unmarshal(data, &str); err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
parsedUint, err := strconv.ParseUint(str, 10, 64) |
| 55 |
if err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
*i = protoUint64(parsedUint) |
| 59 |
} else { |
| 60 |
var parsedUint uint64 |
| 61 |
if err := json.Unmarshal(data, &parsedUint); err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
*i = protoUint64(parsedUint) |
| 65 |
} |
| 66 |
return nil |
| 67 |
} |