feat: Add legacy format key codec for bytes in collections (#17104)

This commit is contained in:
Likhita Polavarapu 2023-08-07 15:16:26 +05:30 committed by GitHub
parent 7a778f5c90
commit c968fe9e4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -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{}

View File

@ -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) {