feat(orm): add ormkv.KeyCodec (#10640)
* feat(orm): add KeyCodec * WIP * code coverage * add DefaultValue test * fix range key check * revert DefaultValue * fix range check * Update orm/encoding/ormkv/key_codec.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * Update orm/encoding/ormkv/key_codec.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * Update orm/encoding/ormkv/key_codec.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * Update orm/encoding/ormkv/util.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * Update orm/encoding/ormkv/key_codec.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * Update orm/encoding/ormkv/key_codec.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * Update orm/encoding/ormkv/key_codec.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * address review comments * address review comments Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
This commit is contained in:
@@ -4,13 +4,13 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/protobuf/types/known/durationpb"
|
||||
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/types/known/durationpb"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
"pgregory.net/rapid"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/orm/encoding/ormfield"
|
||||
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
|
||||
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
|
||||
)
|
||||
|
||||
@@ -114,3 +114,43 @@ func GetTestField(fname protoreflect.Name) protoreflect.FieldDescriptor {
|
||||
a := &testpb.A{}
|
||||
return a.ProtoReflect().Descriptor().Fields().ByName(fname)
|
||||
}
|
||||
|
||||
type TestKeyCodec struct {
|
||||
KeySpecs []TestFieldSpec
|
||||
Codec *ormkv.KeyCodec
|
||||
}
|
||||
|
||||
var TestKeyCodecGen = rapid.Custom(func(t *rapid.T) TestKeyCodec {
|
||||
xs := rapid.SliceOfNDistinct(rapid.IntRange(0, len(TestFieldSpecs)-1), 0, 5, func(i int) int { return i }).
|
||||
Draw(t, "fieldSpecs").([]int)
|
||||
|
||||
var specs []TestFieldSpec
|
||||
var fields []protoreflect.FieldDescriptor
|
||||
|
||||
for _, x := range xs {
|
||||
spec := TestFieldSpecs[x]
|
||||
specs = append(specs, spec)
|
||||
fields = append(fields, GetTestField(spec.FieldName))
|
||||
}
|
||||
|
||||
prefix := rapid.SliceOfN(rapid.Byte(), 0, 5).Draw(t, "prefix").([]byte)
|
||||
|
||||
cdc, err := ormkv.NewKeyCodec(prefix, fields)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return TestKeyCodec{
|
||||
Codec: cdc,
|
||||
KeySpecs: specs,
|
||||
}
|
||||
})
|
||||
|
||||
func (k TestKeyCodec) Draw(t *rapid.T, id string) []protoreflect.Value {
|
||||
n := len(k.KeySpecs)
|
||||
keyValues := make([]protoreflect.Value, n)
|
||||
for i, k := range k.KeySpecs {
|
||||
keyValues[i] = protoreflect.ValueOf(k.Gen.Draw(t, fmt.Sprintf("%s[%d]", id, i)))
|
||||
}
|
||||
return keyValues
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user