Debian Codegraph @

pdata/internal/profileid.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal // import "go.opentelemetry.io/collector/pdata/internal"
5
6 import (
7 "encoding/hex"
8 "errors"
9
10 "go.opentelemetry.io/collector/pdata/internal/json"
11 )
12
13 const profileIDSize = 16
14
15 var errUnmarshalProfileID = errors.New("unmarshal: invalid ProfileID length")
16
17 // ProfileID is a custom data type that is used for all profile_id fields in OTLP
18 // Protobuf messages.
19 type ProfileID [profileIDSize]byte
20
21 func DeleteProfileID(*ProfileID, bool) {}
22
23 func CopyProfileID(dest, src *ProfileID) {
24 *dest = *src
25 }
26
27 // IsEmpty returns true if id contains at leas one non-zero byte.
28 func (pid ProfileID) IsEmpty() bool {
29 return pid == [profileIDSize]byte{}
30 }
31
32 // SizeProto returns the size of the data to serialize in proto format.
33 func (pid ProfileID) SizeProto() int {
34 if pid.IsEmpty() {
35 return 0
36 }
37
38 return profileIDSize
39 }
40
41 // MarshalProto converts profile ID into a binary representation. Called by Protobuf serialization.
42 func (pid ProfileID) MarshalProto(buf []byte) int {
43 if pid.IsEmpty() {
44 return 0
45 }
46
47 return copy(buf[len(buf)-profileIDSize:], pid[:])
48 }
49
50 // UnmarshalProto inflates this profile ID from binary representation. Called by Protobuf serialization.
51 func (pid *ProfileID) UnmarshalProto(buf []byte) error {
52 if len(buf) == 0 {
53 *pid = [profileIDSize]byte{}
54 return nil
55 }
56
57 if len(buf) != profileIDSize {
58 return errUnmarshalProfileID
59 }
60
61 copy(pid[:], buf)
62 return nil
63 }
64
65 // MarshalJSON converts ProfileID into a hex string.
66 //
67 //nolint:govet
68 func (pid ProfileID) MarshalJSON(dest *json.Stream) {
69 dest.WriteString(hex.EncodeToString(pid[:]))
70 }
71
72 // UnmarshalJSON decodes ProfileID from hex string.
73 //
74 //nolint:govet
75 func (pid *ProfileID) UnmarshalJSON(iter *json.Iterator) {
76 *pid = [profileIDSize]byte{}
77 unmarshalJSON(pid[:], iter)
78 }
79
80 func GenTestProfileID() *ProfileID {
81 pid := ProfileID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})
82 return &pid
83 }