|
| 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 json // import "go.opentelemetry.io/collector/pdata/internal/json" |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/base64" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + jsoniter "github.com/json-iterator/go" |
| 22 | + |
| 23 | + otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" |
| 24 | +) |
| 25 | + |
| 26 | +// ReadAttribute Unmarshal JSON data and return otlpcommon.KeyValue |
| 27 | +func ReadAttribute(iter *jsoniter.Iterator) otlpcommon.KeyValue { |
| 28 | + kv := otlpcommon.KeyValue{} |
| 29 | + iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool { |
| 30 | + switch f { |
| 31 | + case "key": |
| 32 | + kv.Key = iter.ReadString() |
| 33 | + case "value": |
| 34 | + iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool { |
| 35 | + kv.Value = readAnyValue(iter, f) |
| 36 | + return true |
| 37 | + }) |
| 38 | + default: |
| 39 | + iter.Skip() |
| 40 | + return true |
| 41 | + } |
| 42 | + return true |
| 43 | + }) |
| 44 | + return kv |
| 45 | +} |
| 46 | + |
| 47 | +// ReadInt64 Unmarshal JSON data and return int64 |
| 48 | +func ReadInt64(iter *jsoniter.Iterator) int64 { |
| 49 | + return iter.ReadAny().ToInt64() |
| 50 | +} |
| 51 | + |
| 52 | +func readAnyValue(iter *jsoniter.Iterator, f string) otlpcommon.AnyValue { |
| 53 | + switch f { |
| 54 | + case "stringValue", "string_value": |
| 55 | + return otlpcommon.AnyValue{ |
| 56 | + Value: &otlpcommon.AnyValue_StringValue{ |
| 57 | + StringValue: iter.ReadString(), |
| 58 | + }, |
| 59 | + } |
| 60 | + case "boolValue", "bool_value": |
| 61 | + return otlpcommon.AnyValue{ |
| 62 | + Value: &otlpcommon.AnyValue_BoolValue{ |
| 63 | + BoolValue: iter.ReadBool(), |
| 64 | + }, |
| 65 | + } |
| 66 | + case "intValue", "int_value": |
| 67 | + return otlpcommon.AnyValue{ |
| 68 | + Value: &otlpcommon.AnyValue_IntValue{ |
| 69 | + IntValue: ReadInt64(iter), |
| 70 | + }, |
| 71 | + } |
| 72 | + case "doubleValue", "double_value": |
| 73 | + return otlpcommon.AnyValue{ |
| 74 | + Value: &otlpcommon.AnyValue_DoubleValue{ |
| 75 | + DoubleValue: iter.ReadFloat64(), |
| 76 | + }, |
| 77 | + } |
| 78 | + case "bytesValue", "bytes_value": |
| 79 | + v, err := base64.StdEncoding.DecodeString(iter.ReadString()) |
| 80 | + if err != nil { |
| 81 | + iter.ReportError("bytesValue", fmt.Sprintf("base64 decode:%v", err)) |
| 82 | + return otlpcommon.AnyValue{} |
| 83 | + } |
| 84 | + return otlpcommon.AnyValue{ |
| 85 | + Value: &otlpcommon.AnyValue_BytesValue{ |
| 86 | + BytesValue: v, |
| 87 | + }, |
| 88 | + } |
| 89 | + case "arrayValue", "array_value": |
| 90 | + return otlpcommon.AnyValue{ |
| 91 | + Value: &otlpcommon.AnyValue_ArrayValue{ |
| 92 | + ArrayValue: readArray(iter), |
| 93 | + }, |
| 94 | + } |
| 95 | + case "kvlistValue", "kvlist_value": |
| 96 | + return otlpcommon.AnyValue{ |
| 97 | + Value: &otlpcommon.AnyValue_KvlistValue{ |
| 98 | + KvlistValue: readKvlistValue(iter), |
| 99 | + }, |
| 100 | + } |
| 101 | + default: |
| 102 | + return otlpcommon.AnyValue{} |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func readArray(iter *jsoniter.Iterator) *otlpcommon.ArrayValue { |
| 107 | + v := &otlpcommon.ArrayValue{} |
| 108 | + iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool { |
| 109 | + switch f { |
| 110 | + case "values": |
| 111 | + iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool { |
| 112 | + iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool { |
| 113 | + v.Values = append(v.Values, readAnyValue(iter, f)) |
| 114 | + return true |
| 115 | + }) |
| 116 | + return true |
| 117 | + }) |
| 118 | + default: |
| 119 | + iter.Skip() |
| 120 | + return true |
| 121 | + } |
| 122 | + return true |
| 123 | + }) |
| 124 | + return v |
| 125 | +} |
| 126 | + |
| 127 | +func readKvlistValue(iter *jsoniter.Iterator) *otlpcommon.KeyValueList { |
| 128 | + v := &otlpcommon.KeyValueList{} |
| 129 | + iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool { |
| 130 | + switch f { |
| 131 | + case "values": |
| 132 | + iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool { |
| 133 | + v.Values = append(v.Values, ReadAttribute(iter)) |
| 134 | + return true |
| 135 | + }) |
| 136 | + default: |
| 137 | + iter.Skip() |
| 138 | + return true |
| 139 | + } |
| 140 | + return true |
| 141 | + }) |
| 142 | + return v |
| 143 | +} |
0 commit comments