-
Notifications
You must be signed in to change notification settings - Fork 0
/
part.go
41 lines (34 loc) · 1.09 KB
/
part.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package protoparts
import (
"bytes"
"fmt"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/reflect/protoreflect"
)
// A Part represents a field within a binary Protocol Buffer message.
type Part struct {
Path Path
Type protowire.Type
// KeyType contains the wire type of the key, if this Part represents an entry within a map. -1 if not.
KeyType protowire.Type
// Bytes contains the Protobuf-encoded field value
Bytes []byte
// Md contains the message descriptor that the Path can be found in
Md protoreflect.MessageDescriptor
}
func (p Part) String() string {
return fmt.Sprintf("%v=%x", p.Path, p.Bytes)
}
func (p Part) fd() protoreflect.FieldDescriptor {
return fieldDescriptorInMessage(p.Md, p.Path)
}
// Equal returns whether this and the passed Part are equivalent.
func (p Part) Equal(p2 Part) bool {
return p.Path.Equal(p2.Path) &&
bytes.Equal(p.Bytes, p2.Bytes) &&
p.Md.FullName() == p2.Md.FullName()
}
// Encode adds the necessary prefix to Bytes to make a valid Protocol Buffer for just this field.
func (p Part) Encode() []byte {
return Parts{p}.Join()
}