Skip to content

Commit

Permalink
Update int.go
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobei0715 authored Oct 18, 2024
1 parent 4e35447 commit 427e427
Showing 1 changed file with 18 additions and 61 deletions.
79 changes: 18 additions & 61 deletions collections/codec/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,17 @@ import (
"strconv"
)

// NewInt64Key returns a NameableKeyCodec instance for encoding and decoding int64 keys.
func NewInt64Key[T ~int64]() NameableKeyCodec[T] { return int64Key[T]{} }

type int64Key[T ~int64] struct{}

func (i int64Key[T]) Encode(buffer []byte, key T) (int, error) {
binary.BigEndian.PutUint64(buffer, (uint64)(key))
buffer[0] ^= 0x80
return 8, nil
}

func (i int64Key[T]) Decode(buffer []byte) (int, T, error) {
if len(buffer) < 8 {
return 0, 0, fmt.Errorf("%w: invalid buffer size, wanted: 8", ErrEncoding)
}
u := uint64(buffer[7]) | uint64(buffer[6])<<8 | uint64(buffer[5])<<16 | uint64(buffer[4])<<24 |
uint64(buffer[3])<<32 | uint64(buffer[2])<<40 | uint64(buffer[1])<<48 | uint64(buffer[0]^0x80)<<56

return 8, (T)(u), nil
}

func (i int64Key[T]) Size(_ T) int { return 8 }

// EncodeJSON encodes an int64 value into JSON format.
func (i int64Key[T]) EncodeJSON(value T) ([]byte, error) {
return []byte(`"` + strconv.FormatInt((int64)(value), 10) + `"`), nil
}

// DecodeJSON decodes an int64 value from JSON format.
func (i int64Key[T]) DecodeJSON(b []byte) (T, error) {
var s string
err := json.Unmarshal(b, &s)
Expand All @@ -46,55 +31,34 @@ func (i int64Key[T]) DecodeJSON(b []byte) (T, error) {
return (T)(k), nil
}

func (i int64Key[T]) Stringify(key T) string { return strconv.FormatInt((int64)(key), 10) }
// Stringify converts an int64 value to its string representation.
func (i int64Key[T]) Stringify(key T) string {
return strconv.FormatInt((int64)(key), 10)
}

// KeyType returns the type identifier for an int64 key.
func (i int64Key[T]) KeyType() string {
return "int64"
}

func (i int64Key[T]) EncodeNonTerminal(buffer []byte, key T) (int, error) {
return i.Encode(buffer, key)
}

func (i int64Key[T]) DecodeNonTerminal(buffer []byte) (int, T, error) {
return i.Decode(buffer)
}

func (i int64Key[T]) SizeNonTerminal(_ T) int {
return 8
}

// WithName returns a KeyCodec instance with a specified name for int64 keys.
func (i int64Key[T]) WithName(name string) KeyCodec[T] {
return NamedKeyCodec[T]{KeyCodec: i, Name: name}
}

// NewInt32Key returns a NameableKeyCodec instance for encoding and decoding int32 keys.
func NewInt32Key[T ~int32]() NameableKeyCodec[T] {
return int32Key[T]{}
}

type int32Key[T ~int32] struct{}

func (i int32Key[T]) Encode(buffer []byte, key T) (int, error) {
binary.BigEndian.PutUint32(buffer, (uint32)(key))
buffer[0] ^= 0x80
return 4, nil
}

func (i int32Key[T]) Decode(buffer []byte) (int, T, error) {
if len(buffer) < 4 {
return 0, 0, fmt.Errorf("%w: invalid buffer size, wanted: 4", ErrEncoding)
}
u := uint32(buffer[3]) | uint32(buffer[2])<<8 | uint32(buffer[1])<<16 | uint32(buffer[0]^0x80)<<24

return 4, (T)(u), nil
}

func (i int32Key[T]) Size(_ T) int { return 4 }

// EncodeJSON encodes an int32 value into JSON format.
func (i int32Key[T]) EncodeJSON(value T) ([]byte, error) {
return []byte(`"` + strconv.FormatInt((int64)(value), 10) + `"`), nil
}

// DecodeJSON decodes an int32 value from JSON format.
func (i int32Key[T]) DecodeJSON(b []byte) (T, error) {
var s string
err := json.Unmarshal(b, &s)
Expand All @@ -108,24 +72,17 @@ func (i int32Key[T]) DecodeJSON(b []byte) (T, error) {
return (T)(k), nil
}

func (i int32Key[T]) Stringify(key T) string { return strconv.FormatInt((int64)(key), 10) }
// Stringify converts an int32 value to its string representation.
func (i int32Key[T]) Stringify(key T) string {
return strconv.FormatInt((int64)(key), 10)
}

// KeyType returns the type identifier for an int32 key.
func (i int32Key[T]) KeyType() string {
return "int32"
}

func (i int32Key[T]) EncodeNonTerminal(buffer []byte, key T) (int, error) {
return i.Encode(buffer, key)
}

func (i int32Key[T]) DecodeNonTerminal(buffer []byte) (int, T, error) {
return i.Decode(buffer)
}

func (i int32Key[T]) SizeNonTerminal(_ T) int {
return 4
}

// WithName returns a KeyCodec instance with a specified name for int32 keys.
func (i int32Key[T]) WithName(name string) KeyCodec[T] {
return NamedKeyCodec[T]{KeyCodec: i, Name: name}
}

0 comments on commit 427e427

Please sign in to comment.