From 4a2e8f48d6bd572d4b5445882f55e4b2087838ce Mon Sep 17 00:00:00 2001 From: Jussi Maki Date: Wed, 9 Oct 2024 17:06:03 +0200 Subject: [PATCH] index: Add *String functions Add *String counterparts (Int32 => Int32String) for implementing FromString in Index[]. Signed-off-by: Jussi Maki --- db_test.go | 9 +++------ index/bool.go | 7 +++++++ index/int.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ index/netip.go | 16 +++++++++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/db_test.go b/db_test.go index a12d105..6d44d88 100644 --- a/db_test.go +++ b/db_test.go @@ -66,12 +66,9 @@ var ( FromObject: func(t testObject) index.KeySet { return index.NewKeySet(index.Uint64(t.ID)) }, - FromKey: index.Uint64, - FromString: func(key string) (index.Key, error) { - v, err := strconv.ParseUint(key, 10, 64) - return index.Uint64(v), err - }, - Unique: true, + FromKey: index.Uint64, + FromString: index.Uint64String, + Unique: true, } tagsIndex = Index[testObject, string]{ diff --git a/index/bool.go b/index/bool.go index c60f7b9..8e37371 100644 --- a/index/bool.go +++ b/index/bool.go @@ -3,6 +3,8 @@ package index +import "strconv" + var ( trueKey = []byte{'T'} falseKey = []byte{'F'} @@ -14,3 +16,8 @@ func Bool(b bool) Key { } return falseKey } + +func BoolString(s string) (Key, error) { + b, err := strconv.ParseBool(s) + return Bool(b), err +} diff --git a/index/int.go b/index/int.go index 0b285c2..caf26d8 100644 --- a/index/int.go +++ b/index/int.go @@ -5,6 +5,7 @@ package index import ( "encoding/binary" + "strconv" ) // The indexing functions on integers should use big-endian encoding. @@ -19,26 +20,78 @@ func Int(n int) Key { return Int32(int32(n)) } +func IntString(s string) (Key, error) { + return Int32String(s) +} + func Int64(n int64) Key { return Uint64(uint64(n)) } +func Int64String(s string) (Key, error) { + n, err := strconv.ParseInt(s, 10, 64) + if err != nil { + return Key{}, err + } + return Uint64(uint64(n)), nil +} + func Int32(n int32) Key { return Uint32(uint32(n)) } +func Int32String(s string) (Key, error) { + n, err := strconv.ParseInt(s, 10, 32) + if err != nil { + return Key{}, err + } + return Uint32(uint32(n)), nil +} + func Int16(n int16) Key { return Uint16(uint16(n)) } +func Int16String(s string) (Key, error) { + n, err := strconv.ParseInt(s, 10, 16) + if err != nil { + return Key{}, err + } + return Uint16(uint16(n)), nil +} + func Uint64(n uint64) Key { return binary.BigEndian.AppendUint64(nil, n) } +func Uint64String(s string) (Key, error) { + n, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return Key{}, err + } + return Uint64(n), nil +} + func Uint32(n uint32) Key { return binary.BigEndian.AppendUint32(nil, n) } +func Uint32String(s string) (Key, error) { + n, err := strconv.ParseUint(s, 10, 32) + if err != nil { + return Key{}, err + } + return Uint32(uint32(n)), nil +} + func Uint16(n uint16) Key { return binary.BigEndian.AppendUint16(nil, n) } + +func Uint16String(s string) (Key, error) { + n, err := strconv.ParseUint(s, 10, 16) + if err != nil { + return Key{}, err + } + return Uint16(uint16(n)), nil +} diff --git a/index/netip.go b/index/netip.go index b8223d0..7a95f63 100644 --- a/index/netip.go +++ b/index/netip.go @@ -20,8 +20,24 @@ func NetIPAddr(addr netip.Addr) Key { return buf[:] } +func NetIPAddrString(s string) (Key, error) { + addr, err := netip.ParseAddr(s) + if err != nil { + return Key{}, err + } + return NetIPAddr(addr), nil +} + func NetIPPrefix(prefix netip.Prefix) Key { // Use the 16-byte form plus bits to have a constant-size key. addrBytes := prefix.Addr().As16() return append(addrBytes[:], uint8(prefix.Bits())) } + +func NetIPPrefixString(s string) (Key, error) { + prefix, err := netip.ParsePrefix(s) + if err != nil { + return Key{}, err + } + return NetIPPrefix(prefix), nil +}