Debian Codegraph @

pdata/internal/generated_proto_sum.go

raw
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 // Sum represents the type of a numeric metric that is calculated as a sum of all reported measurements over a time interval.
18 type Sum struct {
19 DataPoints []*NumberDataPoint
20 AggregationTemporality AggregationTemporality
21 IsMonotonic bool
22 }
23
24 var (
25 protoPoolSum = sync.Pool{
26 New: func() any {
27 return &Sum{}
28 },
29 }
30 )
31
32 func NewSum() *Sum {
33 if !UseProtoPooling.IsEnabled() {
34 return &Sum{}
35 }
36 return protoPoolSum.Get().(*Sum)
37 }
38
39 func DeleteSum(orig *Sum, 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.DataPoints {
50 DeleteNumberDataPoint(orig.DataPoints[i], true)
51 }
52
53 orig.Reset()
54 if nullable {
55 protoPoolSum.Put(orig)
56 }
57 }
58
59 func CopySum(dest, src *Sum) *Sum {
60 // If copying to same object, just return.
61 if src == dest {
62 return dest
63 }
64
65 if src == nil {
66 return nil
67 }
68
69 if dest == nil {
70 dest = NewSum()
71 }
72 dest.DataPoints = CopyNumberDataPointPtrSlice(dest.DataPoints, src.DataPoints)
73
74 dest.AggregationTemporality = src.AggregationTemporality
75
76 dest.IsMonotonic = src.IsMonotonic
77
78 return dest
79 }
80
81 func CopySumSlice(dest, src []Sum) []Sum {
82 var newDest []Sum
83 if cap(dest) < len(src) {
84 newDest = make([]Sum, len(src))
85 } else {
86 newDest = dest[:len(src)]
87 // Cleanup the rest of the elements so GC can free the memory.
88 // This can happen when len(src) < len(dest) < cap(dest).
89 for i := len(src); i < len(dest); i++ {
90 DeleteSum(&dest[i], false)
91 }
92 }
93 for i := range src {
94 CopySum(&newDest[i], &src[i])
95 }
96 return newDest
97 }
98
99 func CopySumPtrSlice(dest, src []*Sum) []*Sum {
100 var newDest []*Sum
101 if cap(dest) < len(src) {
102 newDest = make([]*Sum, len(src))
103 // Copy old pointers to re-use.
104 copy(newDest, dest)
105 // Add new pointers for missing elements from len(dest) to len(srt).
106 for i := len(dest); i < len(src); i++ {
107 newDest[i] = NewSum()
108 }
109 } else {
110 newDest = dest[:len(src)]
111 // Cleanup the rest of the elements so GC can free the memory.
112 // This can happen when len(src) < len(dest) < cap(dest).
113 for i := len(src); i < len(dest); i++ {
114 DeleteSum(dest[i], true)
115 dest[i] = nil
116 }
117 // Add new pointers for missing elements.
118 // This can happen when len(dest) < len(src) < cap(dest).
119 for i := len(dest); i < len(src); i++ {
120 newDest[i] = NewSum()
121 }
122 }
123 for i := range src {
124 CopySum(newDest[i], src[i])
125 }
126 return newDest
127 }
128
129 func (orig *Sum) Reset() {
130 *orig = Sum{}
131 }
132
133 // MarshalJSON marshals all properties from the current struct to the destination stream.
134 func (orig *Sum) MarshalJSON(dest *json.Stream) {
135 dest.WriteObjectStart()
136 if len(orig.DataPoints) > 0 {
137 dest.WriteObjectField("dataPoints")
138 dest.WriteArrayStart()
139 orig.DataPoints[0].MarshalJSON(dest)
140 for i := 1; i < len(orig.DataPoints); i++ {
141 dest.WriteMore()
142 orig.DataPoints[i].MarshalJSON(dest)
143 }
144 dest.WriteArrayEnd()
145 }
146
147 if int32(orig.AggregationTemporality) != 0 {
148 dest.WriteObjectField("aggregationTemporality")
149 dest.WriteInt32(int32(orig.AggregationTemporality))
150 }
151 if orig.IsMonotonic != false {
152 dest.WriteObjectField("isMonotonic")
153 dest.WriteBool(orig.IsMonotonic)
154 }
155 dest.WriteObjectEnd()
156 }
157
158 // UnmarshalJSON unmarshals all properties from the current struct from the source iterator.
159 func (orig *Sum) UnmarshalJSON(iter *json.Iterator) {
160 for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
161 switch f {
162 case "dataPoints", "data_points":
163 for iter.ReadArray() {
164 orig.DataPoints = append(orig.DataPoints, NewNumberDataPoint())
165 orig.DataPoints[len(orig.DataPoints)-1].UnmarshalJSON(iter)
166 }
167
168 case "aggregationTemporality", "aggregation_temporality":
169 orig.AggregationTemporality = AggregationTemporality(iter.ReadEnumValue(AggregationTemporality_value))
170 case "isMonotonic", "is_monotonic":
171 orig.IsMonotonic = iter.ReadBool()
172 default:
173 iter.Skip()
174 }
175 }
176 }
177
178 func (orig *Sum) SizeProto() int {
179 var n int
180 var l int
181 _ = l
182 for i := range orig.DataPoints {
183 l = orig.DataPoints[i].SizeProto()
184 n += 1 + proto.Sov(uint64(l)) + l
185 }
186 if orig.AggregationTemporality != 0 {
187 n += 1 + proto.Sov(uint64(orig.AggregationTemporality))
188 }
189 if orig.IsMonotonic {
190 n += 2
191 }
192 return n
193 }
194
195 func (orig *Sum) MarshalProto(buf []byte) int {
196 pos := len(buf)
197 var l int
198 _ = l
199 for i := len(orig.DataPoints) - 1; i >= 0; i-- {
200 l = orig.DataPoints[i].MarshalProto(buf[:pos])
201 pos -= l
202 pos = proto.EncodeVarint(buf, pos, uint64(l))
203 pos--
204 buf[pos] = 0xa
205 }
206 if orig.AggregationTemporality != 0 {
207 pos = proto.EncodeVarint(buf, pos, uint64(orig.AggregationTemporality))
208 pos--
209 buf[pos] = 0x10
210 }
211 if orig.IsMonotonic {
212 pos--
213 if orig.IsMonotonic {
214 buf[pos] = 1
215 } else {
216 buf[pos] = 0
217 }
218 pos--
219 buf[pos] = 0x18
220 }
221 return len(buf) - pos
222 }
223
224 func (orig *Sum) UnmarshalProto(buf []byte) error {
225 var err error
226 var fieldNum int32
227 var wireType proto.WireType
228
229 l := len(buf)
230 pos := 0
231 for pos < l {
232 // If in a group parsing, move to the next tag.
233 fieldNum, wireType, pos, err = proto.ConsumeTag(buf, pos)
234 if err != nil {
235 return err
236 }
237 switch fieldNum {
238
239 case 1:
240 if wireType != proto.WireTypeLen {
241 return fmt.Errorf("proto: wrong wireType = %d for field DataPoints", wireType)
242 }
243 var length int
244 length, pos, err = proto.ConsumeLen(buf, pos)
245 if err != nil {
246 return err
247 }
248 startPos := pos - length
249 orig.DataPoints = append(orig.DataPoints, NewNumberDataPoint())
250 err = orig.DataPoints[len(orig.DataPoints)-1].UnmarshalProto(buf[startPos:pos])
251 if err != nil {
252 return err
253 }
254
255 case 2:
256 if wireType != proto.WireTypeVarint {
257 return fmt.Errorf("proto: wrong wireType = %d for field AggregationTemporality", wireType)
258 }
259 var num uint64
260 num, pos, err = proto.ConsumeVarint(buf, pos)
261 if err != nil {
262 return err
263 }
264
265 orig.AggregationTemporality = AggregationTemporality(num)
266
267 case 3:
268 if wireType != proto.WireTypeVarint {
269 return fmt.Errorf("proto: wrong wireType = %d for field IsMonotonic", wireType)
270 }
271 var num uint64
272 num, pos, err = proto.ConsumeVarint(buf, pos)
273 if err != nil {
274 return err
275 }
276
277 orig.IsMonotonic = num != 0
278 default:
279 pos, err = proto.ConsumeUnknown(buf, pos, wireType)
280 if err != nil {
281 return err
282 }
283 }
284 }
285 return nil
286 }
287
288 func GenTestSum() *Sum {
289 orig := NewSum()
290 orig.DataPoints = []*NumberDataPoint{{}, GenTestNumberDataPoint()}
291 orig.AggregationTemporality = AggregationTemporality(13)
292 orig.IsMonotonic = true
293 return orig
294 }
295
296 func GenTestSumPtrSlice() []*Sum {
297 orig := make([]*Sum, 5)
298 orig[0] = NewSum()
299 orig[1] = GenTestSum()
300 orig[2] = NewSum()
301 orig[3] = GenTestSum()
302 orig[4] = NewSum()
303 return orig
304 }
305
306 func GenTestSumSlice() []Sum {
307 orig := make([]Sum, 5)
308 orig[1] = *GenTestSum()
309 orig[3] = *GenTestSum()
310 return orig
311 }