feat: Add legacy format key codec for bytes in collections (#17104)
This commit is contained in:
parent
7a778f5c90
commit
c968fe9e4b
@ -44,6 +44,12 @@ var (
|
||||
// state backwards compatibility.
|
||||
// Deprecated: use collections.Uint64Key instead.
|
||||
LEUint64Key collcodec.KeyCodec[uint64] = leUint64Key{}
|
||||
|
||||
// LengthPrefixedBytesKey is a collections KeyCodec to work with []byte.
|
||||
// Deprecated: exists only for state compatibility reasons, should not be
|
||||
// used for new storage keys using []byte. Please use the BytesKey provided
|
||||
// in the collections package.
|
||||
LengthPrefixedBytesKey collcodec.KeyCodec[[]byte] = lengthPrefixedBytesKey{collections.BytesKey}
|
||||
)
|
||||
|
||||
type addressUnion interface {
|
||||
@ -135,6 +141,28 @@ func LengthPrefixedAddressKey[T addressUnion](keyCodec collcodec.KeyCodec[T]) co
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated: lengthPrefixedBytesKey is a special key codec used to retain state backwards compatibility
|
||||
// when a bytes key is used as an index key.
|
||||
type lengthPrefixedBytesKey struct {
|
||||
collcodec.KeyCodec[[]byte]
|
||||
}
|
||||
|
||||
func (g lengthPrefixedBytesKey) Encode(buffer, key []byte) (int, error) {
|
||||
return g.EncodeNonTerminal(buffer, key)
|
||||
}
|
||||
|
||||
func (g lengthPrefixedBytesKey) Decode(buffer []byte) (int, []byte, error) {
|
||||
return g.DecodeNonTerminal(buffer)
|
||||
}
|
||||
|
||||
func (g lengthPrefixedBytesKey) Size(key []byte) int {
|
||||
return g.SizeNonTerminal(key)
|
||||
}
|
||||
|
||||
func (g lengthPrefixedBytesKey) KeyType() string {
|
||||
return "index_key/" + g.KeyCodec.KeyType()
|
||||
}
|
||||
|
||||
// Collection Codecs
|
||||
|
||||
type intValueCodec struct{}
|
||||
|
||||
@ -30,6 +30,10 @@ func TestCollectionsCorrectness(t *testing.T) {
|
||||
t.Run("Time", func(t *testing.T) {
|
||||
colltest.TestKeyCodec(t, TimeKey, time.Time{})
|
||||
})
|
||||
|
||||
t.Run("BytesIndexingKey", func(t *testing.T) {
|
||||
colltest.TestKeyCodec(t, LengthPrefixedBytesKey, []byte{})
|
||||
})
|
||||
}
|
||||
|
||||
func TestLEUint64Key(t *testing.T) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user