feat(orm): add ormkv.EntryCodec and ormkv.IndexCodec's (#10647)

* feat(orm): add KeyCodec

* WIP

* code coverage

* add DefaultValue test

* fix range key check

* revert DefaultValue

* fix range check

* feat(orm): add ormkv.Codec's

* WIP

* add UniqueKeyCodec

* add IndexKeyCodec

* fixes

* add SeqCodec

* add doc comments

* test fields

* refactor field names

* Update orm/encoding/ormkv/index_key.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* Update orm/encoding/ormkv/index_key.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* Update orm/encoding/ormkv/index_key.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* Update orm/encoding/ormkv/unique_key.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* add tests for entry strings

* address review comments

* fix non-deterministic string rendering and tests

* Update x/auth/middleware/priority_test.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* Update x/auth/middleware/priority_test.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
This commit is contained in:
Aaron Craelius
2021-12-02 22:21:57 -05:00
committed by GitHub
co-authored by Tyler
parent a61ca4fdd4
commit c41ac20c6c
15 changed files with 1134 additions and 116 deletions
+54 -20
View File
@@ -120,31 +120,45 @@ type TestKeyCodec struct {
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)
func TestFieldSpecsGen(minLen, maxLen int) *rapid.Generator {
return rapid.Custom(func(t *rapid.T) []TestFieldSpec {
xs := rapid.SliceOfNDistinct(rapid.IntRange(0, len(TestFieldSpecs)-1), minLen, maxLen, func(i int) int { return i }).
Draw(t, "fieldSpecIndexes").([]int)
var specs []TestFieldSpec
var fields []protoreflect.FieldDescriptor
var specs []TestFieldSpec
for _, x := range xs {
spec := TestFieldSpecs[x]
specs = append(specs, spec)
fields = append(fields, GetTestField(spec.FieldName))
}
for _, x := range xs {
spec := TestFieldSpecs[x]
specs = append(specs, spec)
}
prefix := rapid.SliceOfN(rapid.Byte(), 0, 5).Draw(t, "prefix").([]byte)
return specs
})
}
cdc, err := ormkv.NewKeyCodec(prefix, fields)
if err != nil {
panic(err)
}
func TestKeyCodecGen(minLen, maxLen int) *rapid.Generator {
return rapid.Custom(func(t *rapid.T) TestKeyCodec {
specs := TestFieldSpecsGen(minLen, maxLen).Draw(t, "fieldSpecs").([]TestFieldSpec)
return TestKeyCodec{
Codec: cdc,
KeySpecs: specs,
}
})
var fields []protoreflect.Name
for _, spec := range specs {
fields = append(fields, spec.FieldName)
}
prefix := rapid.SliceOfN(rapid.Byte(), 0, 5).Draw(t, "prefix").([]byte)
desc := (&testpb.A{}).ProtoReflect().Descriptor()
cdc, err := ormkv.NewKeyCodec(prefix, desc, 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)
@@ -154,3 +168,23 @@ func (k TestKeyCodec) Draw(t *rapid.T, id string) []protoreflect.Value {
}
return keyValues
}
var GenA = rapid.Custom(func(t *rapid.T) *testpb.A {
a := &testpb.A{}
ref := a.ProtoReflect()
for _, spec := range TestFieldSpecs {
field := GetTestField(spec.FieldName)
value := spec.Gen.Draw(t, string(spec.FieldName))
ref.Set(field, protoreflect.ValueOf(value))
}
return a
})
func ValuesOf(values ...interface{}) []protoreflect.Value {
n := len(values)
res := make([]protoreflect.Value, n)
for i := 0; i < n; i++ {
res[i] = protoreflect.ValueOf(values[i])
}
return res
}