| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Code generated by "internal/cmd/pdatagen/main.go". DO NOT EDIT. |
| 5 |
// To regenerate this file run "make genpdata". |
| 6 |
|
| 7 |
package internal |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"sync" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/pdata/internal/json" |
| 14 |
"go.opentelemetry.io/collector/pdata/internal/proto" |
| 15 |
) |
| 16 |
|
| 17 |
// Resource is a message representing the resource information. |
| 18 |
type Resource struct { |
| 19 |
Attributes []KeyValue |
| 20 |
DroppedAttributesCount uint32 |
| 21 |
EntityRefs []*EntityRef |
| 22 |
} |
| 23 |
|
| 24 |
var ( |
| 25 |
protoPoolResource = sync.Pool{ |
| 26 |
New: func() any { |
| 27 |
return &Resource{} |
| 28 |
}, |
| 29 |
} |
| 30 |
) |
| 31 |
|
| 32 |
func NewResource() *Resource { |
| 33 |
if !UseProtoPooling.IsEnabled() { |
| 34 |
return &Resource{} |
| 35 |
} |
| 36 |
return protoPoolResource.Get().(*Resource) |
| 37 |
} |
| 38 |
|
| 39 |
func DeleteResource(orig *Resource, nullable bool) { |
| 40 |
if orig == nil { |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
if !UseProtoPooling.IsEnabled() { |
| 45 |
orig.Reset() |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
for i := range orig.Attributes { |
| 50 |
DeleteKeyValue(&orig.Attributes[i], false) |
| 51 |
} |
| 52 |
for i := range orig.EntityRefs { |
| 53 |
DeleteEntityRef(orig.EntityRefs[i], true) |
| 54 |
} |
| 55 |
|
| 56 |
orig.Reset() |
| 57 |
if nullable { |
| 58 |
protoPoolResource.Put(orig) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func CopyResource(dest, src *Resource) *Resource { |
| 63 |
// If copying to same object, just return. |
| 64 |
if src == dest { |
| 65 |
return dest |
| 66 |
} |
| 67 |
|
| 68 |
if src == nil { |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
if dest == nil { |
| 73 |
dest = NewResource() |
| 74 |
} |
| 75 |
dest.Attributes = CopyKeyValueSlice(dest.Attributes, src.Attributes) |
| 76 |
|
| 77 |
dest.DroppedAttributesCount = src.DroppedAttributesCount |
| 78 |
|
| 79 |
dest.EntityRefs = CopyEntityRefPtrSlice(dest.EntityRefs, src.EntityRefs) |
| 80 |
|
| 81 |
return dest |
| 82 |
} |
| 83 |
|
| 84 |
func CopyResourceSlice(dest, src []Resource) []Resource { |
| 85 |
var newDest []Resource |
| 86 |
if cap(dest) < len(src) { |
| 87 |
newDest = make([]Resource, len(src)) |
| 88 |
} else { |
| 89 |
newDest = dest[:len(src)] |
| 90 |
// Cleanup the rest of the elements so GC can free the memory. |
| 91 |
// This can happen when len(src) < len(dest) < cap(dest). |
| 92 |
for i := len(src); i < len(dest); i++ { |
| 93 |
DeleteResource(&dest[i], false) |
| 94 |
} |
| 95 |
} |
| 96 |
for i := range src { |
| 97 |
CopyResource(&newDest[i], &src[i]) |
| 98 |
} |
| 99 |
return newDest |
| 100 |
} |
| 101 |
|
| 102 |
func CopyResourcePtrSlice(dest, src []*Resource) []*Resource { |
| 103 |
var newDest []*Resource |
| 104 |
if cap(dest) < len(src) { |
| 105 |
newDest = make([]*Resource, len(src)) |
| 106 |
// Copy old pointers to re-use. |
| 107 |
copy(newDest, dest) |
| 108 |
// Add new pointers for missing elements from len(dest) to len(srt). |
| 109 |
for i := len(dest); i < len(src); i++ { |
| 110 |
newDest[i] = NewResource() |
| 111 |
} |
| 112 |
} else { |
| 113 |
newDest = dest[:len(src)] |
| 114 |
// Cleanup the rest of the elements so GC can free the memory. |
| 115 |
// This can happen when len(src) < len(dest) < cap(dest). |
| 116 |
for i := len(src); i < len(dest); i++ { |
| 117 |
DeleteResource(dest[i], true) |
| 118 |
dest[i] = nil |
| 119 |
} |
| 120 |
// Add new pointers for missing elements. |
| 121 |
// This can happen when len(dest) < len(src) < cap(dest). |
| 122 |
for i := len(dest); i < len(src); i++ { |
| 123 |
newDest[i] = NewResource() |
| 124 |
} |
| 125 |
} |
| 126 |
for i := range src { |
| 127 |
CopyResource(newDest[i], src[i]) |
| 128 |
} |
| 129 |
return newDest |
| 130 |
} |
| 131 |
|
| 132 |
func (orig *Resource) Reset() { |
| 133 |
*orig = Resource{} |
| 134 |
} |
| 135 |
|
| 136 |
// MarshalJSON marshals all properties from the current struct to the destination stream. |
| 137 |
func (orig *Resource) MarshalJSON(dest *json.Stream) { |
| 138 |
dest.WriteObjectStart() |
| 139 |
if len(orig.Attributes) > 0 { |
| 140 |
dest.WriteObjectField("attributes") |
| 141 |
dest.WriteArrayStart() |
| 142 |
orig.Attributes[0].MarshalJSON(dest) |
| 143 |
for i := 1; i < len(orig.Attributes); i++ { |
| 144 |
dest.WriteMore() |
| 145 |
orig.Attributes[i].MarshalJSON(dest) |
| 146 |
} |
| 147 |
dest.WriteArrayEnd() |
| 148 |
} |
| 149 |
if orig.DroppedAttributesCount != uint32(0) { |
| 150 |
dest.WriteObjectField("droppedAttributesCount") |
| 151 |
dest.WriteUint32(orig.DroppedAttributesCount) |
| 152 |
} |
| 153 |
if len(orig.EntityRefs) > 0 { |
| 154 |
dest.WriteObjectField("entityRefs") |
| 155 |
dest.WriteArrayStart() |
| 156 |
orig.EntityRefs[0].MarshalJSON(dest) |
| 157 |
for i := 1; i < len(orig.EntityRefs); i++ { |
| 158 |
dest.WriteMore() |
| 159 |
orig.EntityRefs[i].MarshalJSON(dest) |
| 160 |
} |
| 161 |
dest.WriteArrayEnd() |
| 162 |
} |
| 163 |
dest.WriteObjectEnd() |
| 164 |
} |
| 165 |
|
| 166 |
// UnmarshalJSON unmarshals all properties from the current struct from the source iterator. |
| 167 |
func (orig *Resource) UnmarshalJSON(iter *json.Iterator) { |
| 168 |
for f := iter.ReadObject(); f != ""; f = iter.ReadObject() { |
| 169 |
switch f { |
| 170 |
case "attributes": |
| 171 |
for iter.ReadArray() { |
| 172 |
orig.Attributes = append(orig.Attributes, KeyValue{}) |
| 173 |
orig.Attributes[len(orig.Attributes)-1].UnmarshalJSON(iter) |
| 174 |
} |
| 175 |
|
| 176 |
case "droppedAttributesCount", "dropped_attributes_count": |
| 177 |
orig.DroppedAttributesCount = iter.ReadUint32() |
| 178 |
case "entityRefs", "entity_refs": |
| 179 |
for iter.ReadArray() { |
| 180 |
orig.EntityRefs = append(orig.EntityRefs, NewEntityRef()) |
| 181 |
orig.EntityRefs[len(orig.EntityRefs)-1].UnmarshalJSON(iter) |
| 182 |
} |
| 183 |
|
| 184 |
default: |
| 185 |
iter.Skip() |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func (orig *Resource) SizeProto() int { |
| 191 |
var n int |
| 192 |
var l int |
| 193 |
_ = l |
| 194 |
for i := range orig.Attributes { |
| 195 |
l = orig.Attributes[i].SizeProto() |
| 196 |
n += 1 + proto.Sov(uint64(l)) + l |
| 197 |
} |
| 198 |
if orig.DroppedAttributesCount != 0 { |
| 199 |
n += 1 + proto.Sov(uint64(orig.DroppedAttributesCount)) |
| 200 |
} |
| 201 |
for i := range orig.EntityRefs { |
| 202 |
l = orig.EntityRefs[i].SizeProto() |
| 203 |
n += 1 + proto.Sov(uint64(l)) + l |
| 204 |
} |
| 205 |
return n |
| 206 |
} |
| 207 |
|
| 208 |
func (orig *Resource) MarshalProto(buf []byte) int { |
| 209 |
pos := len(buf) |
| 210 |
var l int |
| 211 |
_ = l |
| 212 |
for i := len(orig.Attributes) - 1; i >= 0; i-- { |
| 213 |
l = orig.Attributes[i].MarshalProto(buf[:pos]) |
| 214 |
pos -= l |
| 215 |
pos = proto.EncodeVarint(buf, pos, uint64(l)) |
| 216 |
pos-- |
| 217 |
buf[pos] = 0xa |
| 218 |
} |
| 219 |
if orig.DroppedAttributesCount != 0 { |
| 220 |
pos = proto.EncodeVarint(buf, pos, uint64(orig.DroppedAttributesCount)) |
| 221 |
pos-- |
| 222 |
buf[pos] = 0x10 |
| 223 |
} |
| 224 |
for i := len(orig.EntityRefs) - 1; i >= 0; i-- { |
| 225 |
l = orig.EntityRefs[i].MarshalProto(buf[:pos]) |
| 226 |
pos -= l |
| 227 |
pos = proto.EncodeVarint(buf, pos, uint64(l)) |
| 228 |
pos-- |
| 229 |
buf[pos] = 0x1a |
| 230 |
} |
| 231 |
return len(buf) - pos |
| 232 |
} |
| 233 |
|
| 234 |
func (orig *Resource) UnmarshalProto(buf []byte) error { |
| 235 |
var err error |
| 236 |
var fieldNum int32 |
| 237 |
var wireType proto.WireType |
| 238 |
|
| 239 |
l := len(buf) |
| 240 |
pos := 0 |
| 241 |
for pos < l { |
| 242 |
// If in a group parsing, move to the next tag. |
| 243 |
fieldNum, wireType, pos, err = proto.ConsumeTag(buf, pos) |
| 244 |
if err != nil { |
| 245 |
return err |
| 246 |
} |
| 247 |
switch fieldNum { |
| 248 |
|
| 249 |
case 1: |
| 250 |
if wireType != proto.WireTypeLen { |
| 251 |
return fmt.Errorf("proto: wrong wireType = %d for field Attributes", wireType) |
| 252 |
} |
| 253 |
var length int |
| 254 |
length, pos, err = proto.ConsumeLen(buf, pos) |
| 255 |
if err != nil { |
| 256 |
return err |
| 257 |
} |
| 258 |
startPos := pos - length |
| 259 |
orig.Attributes = append(orig.Attributes, KeyValue{}) |
| 260 |
err = orig.Attributes[len(orig.Attributes)-1].UnmarshalProto(buf[startPos:pos]) |
| 261 |
if err != nil { |
| 262 |
return err |
| 263 |
} |
| 264 |
|
| 265 |
case 2: |
| 266 |
if wireType != proto.WireTypeVarint { |
| 267 |
return fmt.Errorf("proto: wrong wireType = %d for field DroppedAttributesCount", wireType) |
| 268 |
} |
| 269 |
var num uint64 |
| 270 |
num, pos, err = proto.ConsumeVarint(buf, pos) |
| 271 |
if err != nil { |
| 272 |
return err |
| 273 |
} |
| 274 |
|
| 275 |
orig.DroppedAttributesCount = uint32(num) |
| 276 |
|
| 277 |
case 3: |
| 278 |
if wireType != proto.WireTypeLen { |
| 279 |
return fmt.Errorf("proto: wrong wireType = %d for field EntityRefs", wireType) |
| 280 |
} |
| 281 |
var length int |
| 282 |
length, pos, err = proto.ConsumeLen(buf, pos) |
| 283 |
if err != nil { |
| 284 |
return err |
| 285 |
} |
| 286 |
startPos := pos - length |
| 287 |
orig.EntityRefs = append(orig.EntityRefs, NewEntityRef()) |
| 288 |
err = orig.EntityRefs[len(orig.EntityRefs)-1].UnmarshalProto(buf[startPos:pos]) |
| 289 |
if err != nil { |
| 290 |
return err |
| 291 |
} |
| 292 |
default: |
| 293 |
pos, err = proto.ConsumeUnknown(buf, pos, wireType) |
| 294 |
if err != nil { |
| 295 |
return err |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
return nil |
| 300 |
} |
| 301 |
|
| 302 |
func GenTestResource() *Resource { |
| 303 |
orig := NewResource() |
| 304 |
orig.Attributes = []KeyValue{{}, *GenTestKeyValue()} |
| 305 |
orig.DroppedAttributesCount = uint32(13) |
| 306 |
orig.EntityRefs = []*EntityRef{{}, GenTestEntityRef()} |
| 307 |
return orig |
| 308 |
} |
| 309 |
|
| 310 |
func GenTestResourcePtrSlice() []*Resource { |
| 311 |
orig := make([]*Resource, 5) |
| 312 |
orig[0] = NewResource() |
| 313 |
orig[1] = GenTestResource() |
| 314 |
orig[2] = NewResource() |
| 315 |
orig[3] = GenTestResource() |
| 316 |
orig[4] = NewResource() |
| 317 |
return orig |
| 318 |
} |
| 319 |
|
| 320 |
func GenTestResourceSlice() []Resource { |
| 321 |
orig := make([]Resource, 5) |
| 322 |
orig[1] = *GenTestResource() |
| 323 |
orig[3] = *GenTestResource() |
| 324 |
return orig |
| 325 |
} |