-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into alex/fix_systemtest
- Loading branch information
Showing
24 changed files
with
401 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package codec | ||
|
||
import "fmt" | ||
|
||
// NameableKeyCodec is a KeyCodec that can be named. | ||
type NameableKeyCodec[T any] interface { | ||
KeyCodec[T] | ||
|
||
// WithName returns the KeyCodec with the provided name. | ||
WithName(name string) KeyCodec[T] | ||
} | ||
|
||
// NameableValueCodec is a ValueCodec that can be named. | ||
type NameableValueCodec[T any] interface { | ||
ValueCodec[T] | ||
|
||
// WithName returns the ValueCodec with the provided name. | ||
WithName(name string) ValueCodec[T] | ||
} | ||
|
||
// NamedKeyCodec wraps a KeyCodec with a name. | ||
// The underlying key codec MUST have exactly one field in its schema. | ||
type NamedKeyCodec[T any] struct { | ||
KeyCodec[T] | ||
|
||
// Name is the name of the KeyCodec in the schema. | ||
Name string | ||
} | ||
|
||
// SchemaCodec returns the schema codec for the named key codec. | ||
func (n NamedKeyCodec[T]) SchemaCodec() (SchemaCodec[T], error) { | ||
cdc, err := KeySchemaCodec[T](n.KeyCodec) | ||
if err != nil { | ||
return SchemaCodec[T]{}, err | ||
} | ||
return withName(cdc, n.Name) | ||
} | ||
|
||
// NamedValueCodec wraps a ValueCodec with a name. | ||
// The underlying value codec MUST have exactly one field in its schema. | ||
type NamedValueCodec[T any] struct { | ||
ValueCodec[T] | ||
|
||
// Name is the name of the ValueCodec in the schema. | ||
Name string | ||
} | ||
|
||
// SchemaCodec returns the schema codec for the named value codec. | ||
func (n NamedValueCodec[T]) SchemaCodec() (SchemaCodec[T], error) { | ||
cdc, err := ValueSchemaCodec[T](n.ValueCodec) | ||
if err != nil { | ||
return SchemaCodec[T]{}, err | ||
} | ||
return withName(cdc, n.Name) | ||
} | ||
|
||
func withName[T any](cdc SchemaCodec[T], name string) (SchemaCodec[T], error) { | ||
if len(cdc.Fields) != 1 { | ||
return SchemaCodec[T]{}, fmt.Errorf("expected exactly one field to be named, got %d", len(cdc.Fields)) | ||
} | ||
cdc.Fields[0].Name = name | ||
return cdc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package collections | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"cosmossdk.io/collections/codec" | ||
) | ||
|
||
func TestNaming(t *testing.T) { | ||
expectKeyCodecName(t, "u16", Uint16Key.WithName("u16")) | ||
expectKeyCodecName(t, "u32", Uint32Key.WithName("u32")) | ||
expectKeyCodecName(t, "u64", Uint64Key.WithName("u64")) | ||
expectKeyCodecName(t, "i32", Int32Key.WithName("i32")) | ||
expectKeyCodecName(t, "i64", Int64Key.WithName("i64")) | ||
expectKeyCodecName(t, "str", StringKey.WithName("str")) | ||
expectKeyCodecName(t, "bytes", BytesKey.WithName("bytes")) | ||
expectKeyCodecName(t, "bool", BoolKey.WithName("bool")) | ||
|
||
expectValueCodecName(t, "vu16", Uint16Value.WithName("vu16")) | ||
expectValueCodecName(t, "vu32", Uint32Value.WithName("vu32")) | ||
expectValueCodecName(t, "vu64", Uint64Value.WithName("vu64")) | ||
expectValueCodecName(t, "vi32", Int32Value.WithName("vi32")) | ||
expectValueCodecName(t, "vi64", Int64Value.WithName("vi64")) | ||
expectValueCodecName(t, "vstr", StringValue.WithName("vstr")) | ||
expectValueCodecName(t, "vbytes", BytesValue.WithName("vbytes")) | ||
expectValueCodecName(t, "vbool", BoolValue.WithName("vbool")) | ||
|
||
expectKeyCodecNames(t, NamedPairKeyCodec[bool, string]("abc", BoolKey, "def", StringKey), "abc", "def") | ||
expectKeyCodecNames(t, NamedTripleKeyCodec[bool, string, int32]("abc", BoolKey, "def", StringKey, "ghi", Int32Key), "abc", "def", "ghi") | ||
expectKeyCodecNames(t, NamedQuadKeyCodec[bool, string, int32, uint64]("abc", BoolKey, "def", StringKey, "ghi", Int32Key, "jkl", Uint64Key), "abc", "def", "ghi", "jkl") | ||
} | ||
|
||
func expectKeyCodecName[T any](t *testing.T, name string, cdc codec.KeyCodec[T]) { | ||
schema, err := codec.KeySchemaCodec(cdc) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(schema.Fields)) | ||
require.Equal(t, name, schema.Fields[0].Name) | ||
} | ||
|
||
func expectValueCodecName[T any](t *testing.T, name string, cdc codec.ValueCodec[T]) { | ||
schema, err := codec.ValueSchemaCodec(cdc) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(schema.Fields)) | ||
require.Equal(t, name, schema.Fields[0].Name) | ||
} | ||
|
||
func expectKeyCodecNames[T any](t *testing.T, cdc codec.KeyCodec[T], names ...string) { | ||
schema, err := codec.KeySchemaCodec(cdc) | ||
require.NoError(t, err) | ||
require.Equal(t, len(names), len(schema.Fields)) | ||
for i, name := range names { | ||
require.Equal(t, name, schema.Fields[i].Name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.