refactor(orm)!: update to new module schema descriptor (#11273)
## Description Follow-up to #11119 to update the ORM code to use these definitions. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
This commit is contained in:
+10
-13
@@ -2,7 +2,6 @@ package ormdb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"math"
|
||||
|
||||
@@ -20,12 +19,11 @@ import (
|
||||
)
|
||||
|
||||
type fileDescriptorDBOptions struct {
|
||||
Prefix []byte
|
||||
ID uint32
|
||||
TypeResolver ormtable.TypeResolver
|
||||
JSONValidator func(proto.Message) error
|
||||
GetBackend func(context.Context) (ormtable.Backend, error)
|
||||
GetReadBackend func(context.Context) (ormtable.ReadBackend, error)
|
||||
Prefix []byte
|
||||
ID uint32
|
||||
TypeResolver ormtable.TypeResolver
|
||||
JSONValidator func(proto.Message) error
|
||||
BackendResolver ormtable.BackendResolver
|
||||
}
|
||||
|
||||
type fileDescriptorDB struct {
|
||||
@@ -63,12 +61,11 @@ func newFileDescriptorDB(fileDescriptor protoreflect.FileDescriptor, options fil
|
||||
}
|
||||
|
||||
table, err := ormtable.Build(ormtable.Options{
|
||||
Prefix: prefix,
|
||||
MessageType: messageType,
|
||||
TypeResolver: resolver,
|
||||
JSONValidator: options.JSONValidator,
|
||||
GetReadBackend: options.GetReadBackend,
|
||||
GetBackend: options.GetBackend,
|
||||
Prefix: prefix,
|
||||
MessageType: messageType,
|
||||
TypeResolver: resolver,
|
||||
JSONValidator: options.JSONValidator,
|
||||
BackendResolver: options.BackendResolver,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
+34
-37
@@ -6,6 +6,10 @@ import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
|
||||
"google.golang.org/protobuf/reflect/protoregistry"
|
||||
|
||||
ormv1alpha1 "github.com/cosmos/cosmos-sdk/api/cosmos/orm/v1alpha1"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/orm/types/ormjson"
|
||||
|
||||
"google.golang.org/protobuf/reflect/protodesc"
|
||||
@@ -21,17 +25,6 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
|
||||
)
|
||||
|
||||
// ModuleSchema describes the ORM schema for a module.
|
||||
type ModuleSchema struct {
|
||||
// FileDescriptors are the file descriptors that contain ORM tables to use in this schema.
|
||||
// Each file descriptor must have an unique non-zero uint32 ID associated with it.
|
||||
FileDescriptors map[uint32]protoreflect.FileDescriptor
|
||||
|
||||
// Prefix is an optional prefix to prepend to all keys. It is recommended
|
||||
// to leave it empty.
|
||||
Prefix []byte
|
||||
}
|
||||
|
||||
// ModuleDB defines the ORM database type to be used by modules.
|
||||
type ModuleDB interface {
|
||||
ormtable.Schema
|
||||
@@ -74,17 +67,13 @@ type ModuleDBOptions struct {
|
||||
// will be used
|
||||
JSONValidator func(proto.Message) error
|
||||
|
||||
// GetBackend is the function used to retrieve the table backend.
|
||||
// See ormtable.Options.GetBackend for more details.
|
||||
GetBackend func(context.Context) (ormtable.Backend, error)
|
||||
|
||||
// GetReadBackend is the function used to retrieve a table read backend.
|
||||
// See ormtable.Options.GetReadBackend for more details.
|
||||
GetReadBackend func(context.Context) (ormtable.ReadBackend, error)
|
||||
// GetBackendResolver returns a backend resolver for the requested storage
|
||||
// type or an error if this type of storage isn't supported.
|
||||
GetBackendResolver func(ormv1alpha1.StorageType) (ormtable.BackendResolver, error)
|
||||
}
|
||||
|
||||
// NewModuleDB constructs a ModuleDB instance from the provided schema and options.
|
||||
func NewModuleDB(schema ModuleSchema, options ModuleDBOptions) (ModuleDB, error) {
|
||||
func NewModuleDB(schema *ormv1alpha1.ModuleSchemaDescriptor, options ModuleDBOptions) (ModuleDB, error) {
|
||||
prefix := schema.Prefix
|
||||
db := &moduleDB{
|
||||
prefix: prefix,
|
||||
@@ -92,29 +81,37 @@ func NewModuleDB(schema ModuleSchema, options ModuleDBOptions) (ModuleDB, error)
|
||||
tablesByName: map[protoreflect.FullName]ormtable.Table{},
|
||||
}
|
||||
|
||||
for id, fileDescriptor := range schema.FileDescriptors {
|
||||
fileResolver := options.FileResolver
|
||||
if fileResolver == nil {
|
||||
fileResolver = protoregistry.GlobalFiles
|
||||
}
|
||||
|
||||
for _, entry := range schema.SchemaFile {
|
||||
var backendResolver ormtable.BackendResolver
|
||||
var err error
|
||||
if options.GetBackendResolver != nil {
|
||||
backendResolver, err = options.GetBackendResolver(entry.StorageType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
id := entry.Id
|
||||
fileDescriptor, err := fileResolver.FindFileByPath(entry.ProtoFileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if id == 0 {
|
||||
return nil, ormerrors.InvalidFileDescriptorID.Wrapf("for %s", fileDescriptor.Path())
|
||||
}
|
||||
|
||||
opts := fileDescriptorDBOptions{
|
||||
ID: id,
|
||||
Prefix: prefix,
|
||||
TypeResolver: options.TypeResolver,
|
||||
JSONValidator: options.JSONValidator,
|
||||
GetBackend: options.GetBackend,
|
||||
GetReadBackend: options.GetReadBackend,
|
||||
}
|
||||
|
||||
if options.FileResolver != nil {
|
||||
// if a FileResolver is provided, we use that to resolve the file
|
||||
// and not the one provided as a different pinned file descriptor
|
||||
// may have been provided
|
||||
var err error
|
||||
fileDescriptor, err = options.FileResolver.FindFileByPath(fileDescriptor.Path())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ID: id,
|
||||
Prefix: prefix,
|
||||
TypeResolver: options.TypeResolver,
|
||||
JSONValidator: options.JSONValidator,
|
||||
BackendResolver: backendResolver,
|
||||
}
|
||||
|
||||
fdSchema, err := newFileDescriptorDB(fileDescriptor, opts)
|
||||
|
||||
@@ -8,11 +8,12 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
ormv1alpha1 "github.com/cosmos/cosmos-sdk/api/cosmos/orm/v1alpha1"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/orm/testing/ormmocks"
|
||||
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"gotest.tools/v3/assert"
|
||||
"gotest.tools/v3/golden"
|
||||
|
||||
@@ -28,9 +29,12 @@ import (
|
||||
// These tests use a simulated bank keeper. Addresses and balances use
|
||||
// string and uint64 types respectively for simplicity.
|
||||
|
||||
var TestBankSchema = ormdb.ModuleSchema{
|
||||
FileDescriptors: map[uint32]protoreflect.FileDescriptor{
|
||||
1: testpb.File_testpb_bank_proto,
|
||||
var TestBankSchema = &ormv1alpha1.ModuleSchemaDescriptor{
|
||||
SchemaFile: []*ormv1alpha1.ModuleSchemaDescriptor_FileEntry{
|
||||
{
|
||||
Id: 1,
|
||||
ProtoFileName: testpb.File_testpb_bank_proto.Path(),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -333,3 +337,33 @@ func TestHooks(t *testing.T) {
|
||||
)
|
||||
assert.NilError(t, k.Burn(ctx, acct1, denom, 5))
|
||||
}
|
||||
|
||||
func TestGetBackendResolver(t *testing.T) {
|
||||
backend := ormtest.NewMemoryBackend()
|
||||
getResolver := func(storageType ormv1alpha1.StorageType) (ormtable.BackendResolver, error) {
|
||||
switch storageType {
|
||||
case ormv1alpha1.StorageType_STORAGE_TYPE_MEMORY:
|
||||
return func(ctx context.Context) (ormtable.ReadBackend, error) {
|
||||
return backend, nil
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %s unsupported", storageType)
|
||||
}
|
||||
}
|
||||
_, err := ormdb.NewModuleDB(TestBankSchema, ormdb.ModuleDBOptions{
|
||||
GetBackendResolver: getResolver,
|
||||
})
|
||||
assert.ErrorContains(t, err, "unsupported")
|
||||
|
||||
_, err = ormdb.NewModuleDB(&ormv1alpha1.ModuleSchemaDescriptor{SchemaFile: []*ormv1alpha1.ModuleSchemaDescriptor_FileEntry{
|
||||
{
|
||||
Id: 1,
|
||||
ProtoFileName: testpb.File_testpb_bank_proto.Path(),
|
||||
StorageType: ormv1alpha1.StorageType_STORAGE_TYPE_MEMORY,
|
||||
},
|
||||
},
|
||||
}, ormdb.ModuleDBOptions{
|
||||
GetBackendResolver: getResolver,
|
||||
})
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user