mukan-sdk/x/group/internal/orm/key_codec.go
Mukan Erkin Törük abb1ff956e
Some checks are pending
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
refactor: complete sovereign stack cleanup — all github.com upstream refs purged
2026-05-11 03:46:06 +03:00

101 lines
2.9 KiB
Go

package orm
import (
"fmt"
errorsmod "cosmossdk.io/errors"
"git.cw.tr/mukan-network/mukan-sdk/x/group/errors"
)
// MaxBytesLen is the maximum allowed length for a key part of type []byte
const MaxBytesLen = 255
// buildKeyFromParts encodes and concatenates primary key and index parts.
// They can be []byte, string, and integer types. The function will return
// an error if there is a part of any other type.
// Key parts, except the last part, follow these rules:
// - []byte is encoded with a single byte length prefix
// - strings are null-terminated
// - integers are encoded using 8 byte big endian.
func buildKeyFromParts(parts []any) ([]byte, error) {
bytesSlice := make([][]byte, len(parts))
totalLen := 0
var err error
for i, part := range parts {
bytesSlice[i], err = keyPartBytes(part, len(parts) > 1 && i == len(parts)-1)
if err != nil {
return nil, err
}
totalLen += len(bytesSlice[i])
}
key := make([]byte, 0, totalLen)
for _, bs := range bytesSlice {
key = append(key, bs...)
}
return key, nil
}
func keyPartBytes(part any, last bool) ([]byte, error) {
switch v := part.(type) {
case []byte:
if last || len(v) == 0 {
return v, nil
}
return AddLengthPrefix(v), nil
case string:
if last || len(v) == 0 {
return []byte(v), nil
}
return NullTerminatedBytes(v), nil
case uint64:
return EncodeSequence(v), nil
default:
return nil, fmt.Errorf("type %T not allowed as key part", v)
}
}
// AddLengthPrefix prefixes the byte array with its length as 8 bytes. The function will panic
// if the bytes length is bigger than 255.
func AddLengthPrefix(bytes []byte) []byte {
byteLen := len(bytes)
if byteLen > MaxBytesLen {
panic(errorsmod.Wrap(errors.ErrORMKeyMaxLength, "Cannot create key part with an []byte of length greater than 255 bytes. Try again with a smaller []byte."))
}
prefixedBytes := make([]byte, 1+len(bytes))
copy(prefixedBytes, []byte{uint8(byteLen)})
copy(prefixedBytes[1:], bytes)
return prefixedBytes
}
// NullTerminatedBytes converts string to byte array and null terminate it
func NullTerminatedBytes(s string) []byte {
bytes := make([]byte, len(s)+1)
copy(bytes, s)
return bytes
}
// stripRowID returns the RowID from the indexKey based on secondaryIndexKey type.
// It is the reverse operation to buildKeyFromParts for index keys
// where the first part is the encoded secondaryIndexKey and the second part is the RowID.
func stripRowID(indexKey []byte, secondaryIndexKey any) (RowID, error) {
switch v := secondaryIndexKey.(type) {
case []byte:
searchableKeyLen := indexKey[0]
return indexKey[1+searchableKeyLen:], nil
case string:
searchableKeyLen := 0
for i, b := range indexKey {
if b == 0 {
searchableKeyLen = i
break
}
}
return indexKey[1+searchableKeyLen:], nil
case uint64:
return indexKey[EncodedSeqLength:], nil
default:
return nil, fmt.Errorf("type %T not allowed as index key", v)
}
}