-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiretype.go
106 lines (94 loc) · 2.88 KB
/
wiretype.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package polo
// WireType is an enum for different wire types.
// A wire type indicates the kind of encoding for some data.
type WireType byte
const (
// WireNull represents a null wire. Used for consuming field orders without data.
WireNull WireType = 0
// WireFalse represents a Boolean False
WireFalse WireType = 1
// WireTrue represents a Boolean True
WireTrue WireType = 2
// WirePosInt represents a Binary encoded +ve Integer in BigEndian Order.
WirePosInt WireType = 3
// WireNegInt represents a Binary encoded -ve Integer in BigEndian Order.
// The number is encoded as its absolute value and must be multiplied with -1 to get its actual value.
WireNegInt WireType = 4
// WireRaw represents some POLO encoded bytes
WireRaw WireType = 5
// WireWord represents UTF-8 encoded string/bytes
WireWord WireType = 6
// WireFloat represents some floating point data encoded in the IEEE754 standard. (floats)
WireFloat WireType = 7
// WireDoc represents some doc encoded data (string keyed maps, tagged structs and Document objects)
WireDoc WireType = 13
// WirePack represents some pack encoded data (slices, arrays, maps, structs)
WirePack WireType = 14
// WireLoad represents a load tag for compound wire type
WireLoad WireType = 15
)
// String returns a string representation of the wiretype.
//
// If the wiretype is not recognized, "unknown" is returned.
// Implements the Stringer interface for wiretype.
func (wiretype WireType) String() string {
switch wiretype {
case WireNull:
return "null"
case WireFalse:
return "false"
case WireTrue:
return "true"
case WirePosInt:
return "posint"
case WireNegInt:
return "negint"
case WireRaw:
return "raw"
case WireWord:
return "word"
case WireFloat:
return "float"
case WireDoc:
return "document"
case WirePack:
return "pack"
case WireLoad:
return "load"
default:
if wiretype > 15 {
return "unknown"
} else {
return "reserved"
}
}
}
// IsNull returns whether a given wiretype is null.
// A wiretype is null if it is WireNull, has a value greater than 15 or is between 8 and 12 (reserved)
func (wiretype WireType) IsNull() bool {
if wiretype == WireNull || !wiretype.IsValid() {
return true
} else {
return false
}
}
// IsValid returns whether a wiretype is valid.
// A wiretype is valid if its value is less than 15 and not one of the reserved types
func (wiretype WireType) IsValid() bool {
if wiretype > 15 || (wiretype >= 8 && wiretype <= 12) {
return false
} else {
return true
}
}
// IsCompound returns whether a given wiretype is a compound type. (contains a load inside it)
func (wiretype WireType) IsCompound() bool {
return wiretype == WirePack || wiretype == WireDoc
}
// IsWireType returns whether the POLO data is of a certain wire type
func IsWireType[T Raw | Any | []byte](wire T, kind WireType) bool {
if len(wire) == 0 {
return kind == WireNull
}
return wire[0]&15 == byte(kind)
}