Skip to content

Commit

Permalink
index: Add *String functions
Browse files Browse the repository at this point in the history
Add *String counterparts (Int32 => Int32String) for implementing FromString
in Index[].

Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Oct 9, 2024
1 parent 4152d5e commit 58f01e1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
9 changes: 3 additions & 6 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]{
Expand Down
7 changes: 7 additions & 0 deletions index/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package index

import "strconv"

var (
trueKey = []byte{'T'}
falseKey = []byte{'F'}
Expand All @@ -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
}
53 changes: 53 additions & 0 deletions index/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package index

import (
"encoding/binary"
"strconv"
)

// The indexing functions on integers should use big-endian encoding.
Expand All @@ -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
}
16 changes: 16 additions & 0 deletions index/netip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 58f01e1

Please sign in to comment.