-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
232 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package zapi | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/netip" | ||
|
||
"github.com/dropmorepackets/haproxy-go/pkg/encoding" | ||
) | ||
|
||
type KVEntry struct { | ||
name []byte | ||
|
||
dataType DataType | ||
|
||
byteVal []byte | ||
intVal int64 | ||
boolVar bool | ||
} | ||
|
||
func (k *KVEntry) Read(buf []byte) (int, error) { | ||
var offset int | ||
|
||
vLen, n, err := encoding.Varint(buf) | ||
offset += n | ||
if err != nil { | ||
return offset, err | ||
} | ||
|
||
k.name = buf[offset : offset+int(vLen)] | ||
offset += int(vLen) | ||
|
||
k.dataType = DataType(buf[offset] & DataTypeMask) | ||
// just always decode the boolVar even tho its wrong. | ||
k.boolVar = buf[offset]&DataFlagTrue > 0 | ||
offset++ | ||
|
||
switch k.dataType { | ||
case DataTypeNull, DataTypeBool: | ||
// noop | ||
|
||
case DataTypeInt32, DataTypeInt64, | ||
DataTypeUInt32, DataTypeUInt64: | ||
var v uint64 | ||
v, n, err = encoding.Varint(buf[offset:]) | ||
offset += n | ||
if err != nil { | ||
return offset, err | ||
} | ||
k.intVal = int64(v) | ||
|
||
case DataTypeIPV4: | ||
k.byteVal = buf[offset : offset+net.IPv4len] | ||
offset += net.IPv4len | ||
|
||
case DataTypeIPV6: | ||
k.byteVal = buf[offset : offset+net.IPv6len] | ||
offset += net.IPv6len | ||
|
||
case DataTypeString, DataTypeBinary: | ||
vLen, n, err = encoding.Varint(buf[offset:]) | ||
offset += n | ||
if err != nil { | ||
return offset, err | ||
} | ||
|
||
k.byteVal = buf[offset : offset+int(vLen)] | ||
offset += int(vLen) | ||
|
||
default: | ||
return offset, fmt.Errorf("unknown data type: %x", k.dataType) | ||
} | ||
|
||
return offset, nil | ||
} | ||
|
||
func (k *KVEntry) NameBytes() []byte { | ||
return k.name | ||
} | ||
|
||
func (k *KVEntry) ValueBytes() []byte { | ||
return k.byteVal | ||
} | ||
|
||
func (k *KVEntry) ValueInt() int64 { | ||
return k.intVal | ||
} | ||
|
||
func (k *KVEntry) ValueBool() bool { | ||
return k.boolVar | ||
} | ||
|
||
func (k *KVEntry) ValueAddr() netip.Addr { | ||
addr, ok := netip.AddrFromSlice(k.byteVal) | ||
if !ok { | ||
panic("invalid addr decode") | ||
} | ||
return addr | ||
} | ||
|
||
func (k *KVEntry) NameEquals(s string) bool { | ||
// bytes.Equal describes this operation as alloc free | ||
return string(k.name) == s | ||
} | ||
|
||
func (k *KVEntry) Type() DataType { | ||
return k.dataType | ||
} | ||
|
||
// Value returns the typed value for the KVEntry. It can allocate memory | ||
// which is why assertions and direct type access is recommended. | ||
func (k *KVEntry) Value() any { | ||
switch k.dataType { | ||
case DataTypeNull: | ||
return nil | ||
case DataTypeBool: | ||
return k.boolVar | ||
case DataTypeInt32: | ||
return int32(k.intVal) | ||
case DataTypeInt64: | ||
return k.intVal | ||
case DataTypeUInt32: | ||
return uint32(k.intVal) | ||
case DataTypeUInt64: | ||
return uint64(k.intVal) | ||
case DataTypeIPV4, DataTypeIPV6: | ||
addr, ok := netip.AddrFromSlice(k.byteVal) | ||
if !ok { | ||
panic("invalid addr decode") | ||
} | ||
return addr | ||
case DataTypeString: | ||
return string(k.byteVal) | ||
case DataTypeBinary: | ||
return k.byteVal | ||
default: | ||
panic("unknown datatype") | ||
} | ||
} | ||
|
||
func (k *KVEntry) Reset() { | ||
k.name = nil | ||
k.dataType = 0 | ||
k.byteVal = nil | ||
k.boolVar = false | ||
k.intVal = 0 | ||
} |
Oops, something went wrong.