refactor(schema)!: rename ObjectType -> StateObjectType (#21691)
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
This commit is contained in:
parent
397ff0bd22
commit
ae40e809b9
@ -79,11 +79,11 @@ func Example_objectIndexer_createTableSql_vote_no_retain_delete() {
|
||||
// GRANT SELECT ON TABLE "test_vote" TO PUBLIC;
|
||||
}
|
||||
|
||||
func exampleCreateTable(objectType schema.ObjectType) {
|
||||
func exampleCreateTable(objectType schema.StateObjectType) {
|
||||
exampleCreateTableOpt(objectType, false)
|
||||
}
|
||||
|
||||
func exampleCreateTableOpt(objectType schema.ObjectType, noRetainDelete bool) {
|
||||
func exampleCreateTableOpt(objectType schema.StateObjectType, noRetainDelete bool) {
|
||||
tm := newObjectIndexer("test", objectType, options{
|
||||
logger: logutil.NoopLogger{},
|
||||
disableRetainDeletions: noRetainDelete,
|
||||
|
||||
@ -4,10 +4,10 @@ import "cosmossdk.io/schema"
|
||||
|
||||
var ExampleSchema schema.ModuleSchema
|
||||
|
||||
var AllKindsObject schema.ObjectType
|
||||
var AllKindsObject schema.StateObjectType
|
||||
|
||||
func init() {
|
||||
AllKindsObject = schema.ObjectType{
|
||||
AllKindsObject = schema.StateObjectType{
|
||||
Name: "all_kinds",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
@ -45,7 +45,7 @@ func init() {
|
||||
)
|
||||
}
|
||||
|
||||
var SingletonObject = schema.ObjectType{
|
||||
var SingletonObject = schema.StateObjectType{
|
||||
Name: "singleton",
|
||||
ValueFields: []schema.Field{
|
||||
{
|
||||
@ -65,7 +65,7 @@ var SingletonObject = schema.ObjectType{
|
||||
},
|
||||
}
|
||||
|
||||
var VoteObject = schema.ObjectType{
|
||||
var VoteObject = schema.StateObjectType{
|
||||
Name: "vote",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
|
||||
@ -40,7 +40,7 @@ func (m *moduleIndexer) initializeSchema(ctx context.Context, conn dbConn) error
|
||||
}
|
||||
|
||||
// create tables for all object types
|
||||
m.schema.ObjectTypes(func(typ schema.ObjectType) bool {
|
||||
m.schema.StateObjectTypes(func(typ schema.StateObjectType) bool {
|
||||
tm := newObjectIndexer(m.moduleName, typ, m.options)
|
||||
m.tables[typ.Name] = tm
|
||||
err = tm.createTable(ctx, conn)
|
||||
|
||||
@ -9,14 +9,14 @@ import (
|
||||
// objectIndexer is a helper struct that generates SQL for a given object type.
|
||||
type objectIndexer struct {
|
||||
moduleName string
|
||||
typ schema.ObjectType
|
||||
typ schema.StateObjectType
|
||||
valueFields map[string]schema.Field
|
||||
allFields map[string]schema.Field
|
||||
options options
|
||||
}
|
||||
|
||||
// newObjectIndexer creates a new objectIndexer for the given object type.
|
||||
func newObjectIndexer(moduleName string, typ schema.ObjectType, options options) *objectIndexer {
|
||||
func newObjectIndexer(moduleName string, typ schema.StateObjectType, options options) *objectIndexer {
|
||||
allFields := make(map[string]schema.Field)
|
||||
valueFields := make(map[string]schema.Field)
|
||||
|
||||
|
||||
@ -70,11 +70,11 @@ func (tm *objectIndexer) existsSqlAndParams(w io.Writer, key interface{}) ([]int
|
||||
return keyParams, err
|
||||
}
|
||||
|
||||
func (tm *objectIndexer) get(ctx context.Context, conn dbConn, key interface{}) (schema.ObjectUpdate, bool, error) {
|
||||
func (tm *objectIndexer) get(ctx context.Context, conn dbConn, key interface{}) (schema.StateObjectUpdate, bool, error) {
|
||||
buf := new(strings.Builder)
|
||||
params, err := tm.getSqlAndParams(buf, key)
|
||||
if err != nil {
|
||||
return schema.ObjectUpdate{}, false, err
|
||||
return schema.StateObjectUpdate{}, false, err
|
||||
}
|
||||
|
||||
sqlStr := buf.String()
|
||||
@ -147,7 +147,7 @@ func (tm *objectIndexer) selectAllClause(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tm *objectIndexer) readRow(row interface{ Scan(...interface{}) error }) (schema.ObjectUpdate, bool, error) {
|
||||
func (tm *objectIndexer) readRow(row interface{ Scan(...interface{}) error }) (schema.StateObjectUpdate, bool, error) {
|
||||
var res []interface{}
|
||||
for _, f := range tm.typ.KeyFields {
|
||||
res = append(res, tm.colBindValue(f))
|
||||
@ -164,16 +164,16 @@ func (tm *objectIndexer) readRow(row interface{ Scan(...interface{}) error }) (s
|
||||
err := row.Scan(res...)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return schema.ObjectUpdate{}, false, err
|
||||
return schema.StateObjectUpdate{}, false, err
|
||||
}
|
||||
return schema.ObjectUpdate{}, false, err
|
||||
return schema.StateObjectUpdate{}, false, err
|
||||
}
|
||||
|
||||
var keys []interface{}
|
||||
for _, field := range tm.typ.KeyFields {
|
||||
x, err := tm.readCol(field, res[0])
|
||||
if err != nil {
|
||||
return schema.ObjectUpdate{}, false, err
|
||||
return schema.StateObjectUpdate{}, false, err
|
||||
}
|
||||
keys = append(keys, x)
|
||||
res = res[1:]
|
||||
@ -188,7 +188,7 @@ func (tm *objectIndexer) readRow(row interface{ Scan(...interface{}) error }) (s
|
||||
for _, field := range tm.typ.ValueFields {
|
||||
x, err := tm.readCol(field, res[0])
|
||||
if err != nil {
|
||||
return schema.ObjectUpdate{}, false, err
|
||||
return schema.StateObjectUpdate{}, false, err
|
||||
}
|
||||
values = append(values, x)
|
||||
res = res[1:]
|
||||
@ -199,7 +199,7 @@ func (tm *objectIndexer) readRow(row interface{ Scan(...interface{}) error }) (s
|
||||
value = values[0]
|
||||
}
|
||||
|
||||
update := schema.ObjectUpdate{
|
||||
update := schema.StateObjectUpdate{
|
||||
TypeName: tm.typ.Name,
|
||||
Key: key,
|
||||
Value: value,
|
||||
|
||||
@ -100,15 +100,15 @@ type objectView struct {
|
||||
conn dbConn
|
||||
}
|
||||
|
||||
func (tm *objectView) ObjectType() schema.ObjectType {
|
||||
func (tm *objectView) ObjectType() schema.StateObjectType {
|
||||
return tm.typ
|
||||
}
|
||||
|
||||
func (tm *objectView) GetObject(key interface{}) (update schema.ObjectUpdate, found bool, err error) {
|
||||
func (tm *objectView) GetObject(key interface{}) (update schema.StateObjectUpdate, found bool, err error) {
|
||||
return tm.get(tm.ctx, tm.conn, key)
|
||||
}
|
||||
|
||||
func (tm *objectView) AllState(f func(schema.ObjectUpdate, error) bool) {
|
||||
func (tm *objectView) AllState(f func(schema.StateObjectUpdate, error) bool) {
|
||||
buf := new(strings.Builder)
|
||||
err := tm.selectAllSql(buf)
|
||||
if err != nil {
|
||||
|
||||
@ -131,7 +131,7 @@ type ObjectUpdateData struct {
|
||||
ModuleName string
|
||||
|
||||
// Updates are the object updates.
|
||||
Updates []schema.ObjectUpdate
|
||||
Updates []schema.StateObjectUpdate
|
||||
}
|
||||
|
||||
// CommitData represents commit data. It is empty for now, but fields could be added later.
|
||||
|
||||
@ -13,18 +13,18 @@ type ModuleCodec struct {
|
||||
// Schema is the schema for the module. It is required.
|
||||
Schema ModuleSchema
|
||||
|
||||
// KVDecoder is a function that decodes a key-value pair into an ObjectUpdate.
|
||||
// KVDecoder is a function that decodes a key-value pair into an StateObjectUpdate.
|
||||
// If it is nil, the module doesn't support state decoding directly.
|
||||
KVDecoder KVDecoder
|
||||
}
|
||||
|
||||
// KVDecoder is a function that decodes a key-value pair into one or more ObjectUpdate's.
|
||||
// KVDecoder is a function that decodes a key-value pair into one or more StateObjectUpdate's.
|
||||
// If the KV-pair doesn't represent object updates, the function should return nil as the first
|
||||
// and no error. The error result should only be non-nil when the decoder expected
|
||||
// to parse a valid update and was unable to. In the case of an error, the decoder may return
|
||||
// a non-nil value for the first return value, which can indicate which parts of the update
|
||||
// were decodable to aid debugging.
|
||||
type KVDecoder = func(KVPairUpdate) ([]ObjectUpdate, error)
|
||||
type KVDecoder = func(KVPairUpdate) ([]StateObjectUpdate, error)
|
||||
|
||||
// KVPairUpdate represents a key-value pair set or delete.
|
||||
type KVPairUpdate = struct {
|
||||
|
||||
@ -29,7 +29,7 @@ func TestMiddleware(t *testing.T) {
|
||||
|
||||
tl.oneMod.SetValue("abc")
|
||||
|
||||
expectedBank := []schema.ObjectUpdate{
|
||||
expectedBank := []schema.StateObjectUpdate{
|
||||
{
|
||||
TypeName: "supply",
|
||||
Key: []interface{}{"foo"},
|
||||
@ -56,7 +56,7 @@ func TestMiddleware(t *testing.T) {
|
||||
t.Fatalf("expected %v, got %v", expectedBank, tl.bankUpdates)
|
||||
}
|
||||
|
||||
expectedOne := []schema.ObjectUpdate{
|
||||
expectedOne := []schema.StateObjectUpdate{
|
||||
{TypeName: "item", Value: "abc"},
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ func TestMiddleware_filtered(t *testing.T) {
|
||||
t.Fatalf("expected no bank updates")
|
||||
}
|
||||
|
||||
expectedOne := []schema.ObjectUpdate{
|
||||
expectedOne := []schema.StateObjectUpdate{
|
||||
{TypeName: "item", Value: "abc"},
|
||||
}
|
||||
|
||||
@ -108,7 +108,7 @@ func TestSync(t *testing.T) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
|
||||
expected := []schema.ObjectUpdate{
|
||||
expected := []schema.StateObjectUpdate{
|
||||
{
|
||||
TypeName: "balances",
|
||||
Key: []interface{}{"alice", "foo"},
|
||||
@ -130,7 +130,7 @@ func TestSync(t *testing.T) {
|
||||
t.Fatalf("expected %v, got %v", expected, tl.bankUpdates)
|
||||
}
|
||||
|
||||
expectedOne := []schema.ObjectUpdate{
|
||||
expectedOne := []schema.StateObjectUpdate{
|
||||
{TypeName: "item", Value: "def"},
|
||||
}
|
||||
|
||||
@ -157,7 +157,7 @@ func TestSync_filtered(t *testing.T) {
|
||||
t.Fatalf("expected no bank updates")
|
||||
}
|
||||
|
||||
expectedOne := []schema.ObjectUpdate{
|
||||
expectedOne := []schema.StateObjectUpdate{
|
||||
{TypeName: "item", Value: "def"},
|
||||
}
|
||||
|
||||
@ -168,8 +168,8 @@ func TestSync_filtered(t *testing.T) {
|
||||
|
||||
type testFixture struct {
|
||||
appdata.Listener
|
||||
bankUpdates []schema.ObjectUpdate
|
||||
oneValueUpdates []schema.ObjectUpdate
|
||||
bankUpdates []schema.StateObjectUpdate
|
||||
oneValueUpdates []schema.StateObjectUpdate
|
||||
resolver DecoderResolver
|
||||
multiStore *testMultiStore
|
||||
bankMod *exampleBankModule
|
||||
@ -371,7 +371,7 @@ func (e exampleBankModule) subBalance(acct, denom string, amount uint64) error {
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
exampleBankSchema, err = schema.CompileModuleSchema(schema.ObjectType{
|
||||
exampleBankSchema, err = schema.CompileModuleSchema(schema.StateObjectType{
|
||||
Name: "balances",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
@ -400,7 +400,7 @@ var exampleBankSchema schema.ModuleSchema
|
||||
func (e exampleBankModule) ModuleCodec() (schema.ModuleCodec, error) {
|
||||
return schema.ModuleCodec{
|
||||
Schema: exampleBankSchema,
|
||||
KVDecoder: func(update schema.KVPairUpdate) ([]schema.ObjectUpdate, error) {
|
||||
KVDecoder: func(update schema.KVPairUpdate) ([]schema.StateObjectUpdate, error) {
|
||||
key := string(update.Key)
|
||||
value, err := strconv.ParseUint(string(update.Value), 10, 64)
|
||||
if err != nil {
|
||||
@ -408,14 +408,14 @@ func (e exampleBankModule) ModuleCodec() (schema.ModuleCodec, error) {
|
||||
}
|
||||
if strings.HasPrefix(key, "balance/") {
|
||||
parts := strings.Split(key, "/")
|
||||
return []schema.ObjectUpdate{{
|
||||
return []schema.StateObjectUpdate{{
|
||||
TypeName: "balances",
|
||||
Key: []interface{}{parts[1], parts[2]},
|
||||
Value: value,
|
||||
}}, nil
|
||||
} else if strings.HasPrefix(key, "supply/") {
|
||||
parts := strings.Split(key, "/")
|
||||
return []schema.ObjectUpdate{{
|
||||
return []schema.StateObjectUpdate{{
|
||||
TypeName: "supply",
|
||||
Key: []interface{}{parts[1]},
|
||||
Value: value,
|
||||
@ -435,7 +435,7 @@ type oneValueModule struct {
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
oneValueModSchema, err = schema.CompileModuleSchema(schema.ObjectType{
|
||||
oneValueModSchema, err = schema.CompileModuleSchema(schema.StateObjectType{
|
||||
Name: "item",
|
||||
ValueFields: []schema.Field{
|
||||
{Name: "value", Kind: schema.StringKind},
|
||||
@ -451,11 +451,11 @@ var oneValueModSchema schema.ModuleSchema
|
||||
func (i oneValueModule) ModuleCodec() (schema.ModuleCodec, error) {
|
||||
return schema.ModuleCodec{
|
||||
Schema: oneValueModSchema,
|
||||
KVDecoder: func(update schema.KVPairUpdate) ([]schema.ObjectUpdate, error) {
|
||||
KVDecoder: func(update schema.KVPairUpdate) ([]schema.StateObjectUpdate, error) {
|
||||
if string(update.Key) != "key" {
|
||||
return nil, fmt.Errorf("unexpected key: %v", update.Key)
|
||||
}
|
||||
return []schema.ObjectUpdate{
|
||||
return []schema.StateObjectUpdate{
|
||||
{TypeName: "item", Value: string(update.Value)},
|
||||
}, nil
|
||||
},
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
type modA struct{}
|
||||
|
||||
func (m modA) ModuleCodec() (schema.ModuleCodec, error) {
|
||||
modSchema, err := schema.CompileModuleSchema(schema.ObjectType{Name: "A", KeyFields: []schema.Field{{Name: "field1", Kind: schema.StringKind}}})
|
||||
modSchema, err := schema.CompileModuleSchema(schema.StateObjectType{Name: "A", KeyFields: []schema.Field{{Name: "field1", Kind: schema.StringKind}}})
|
||||
if err != nil {
|
||||
return schema.ModuleCodec{}, err
|
||||
}
|
||||
@ -22,7 +22,7 @@ func (m modA) ModuleCodec() (schema.ModuleCodec, error) {
|
||||
type modB struct{}
|
||||
|
||||
func (m modB) ModuleCodec() (schema.ModuleCodec, error) {
|
||||
modSchema, err := schema.CompileModuleSchema(schema.ObjectType{Name: "B", KeyFields: []schema.Field{{Name: "field2", Kind: schema.StringKind}}})
|
||||
modSchema, err := schema.CompileModuleSchema(schema.StateObjectType{Name: "B", KeyFields: []schema.Field{{Name: "field2", Kind: schema.StringKind}}})
|
||||
if err != nil {
|
||||
return schema.ModuleCodec{}, err
|
||||
}
|
||||
@ -45,7 +45,7 @@ func TestModuleSetDecoderResolver_IterateAll(t *testing.T) {
|
||||
objectTypes := map[string]bool{}
|
||||
err := testResolver.IterateAll(func(moduleName string, cdc schema.ModuleCodec) error {
|
||||
cdc.Schema.AllTypes(func(t schema.Type) bool {
|
||||
objTyp, ok := t.(schema.ObjectType)
|
||||
objTyp, ok := t.(schema.StateObjectType)
|
||||
if ok {
|
||||
objectTypes[objTyp.Name] = true
|
||||
}
|
||||
|
||||
@ -4,14 +4,14 @@ import "cosmossdk.io/schema"
|
||||
|
||||
// ModuleSchemaDiff represents the difference between two module schemas.
|
||||
type ModuleSchemaDiff struct {
|
||||
// AddedObjectTypes is a list of object types that were added.
|
||||
AddedObjectTypes []schema.ObjectType
|
||||
// AddedStateObjectTypes is a list of object types that were added.
|
||||
AddedStateObjectTypes []schema.StateObjectType
|
||||
|
||||
// ChangedObjectTypes is a list of object types that were changed.
|
||||
ChangedObjectTypes []ObjectTypeDiff
|
||||
// ChangedStateObjectTypes is a list of object types that were changed.
|
||||
ChangedStateObjectTypes []StateObjectTypeDiff
|
||||
|
||||
// RemovedObjectTypes is a list of object types that were removed.
|
||||
RemovedObjectTypes []schema.ObjectType
|
||||
// RemovedStateObjectTypes is a list of object types that were removed.
|
||||
RemovedStateObjectTypes []schema.StateObjectType
|
||||
|
||||
// AddedEnumTypes is a list of enum types that were added.
|
||||
AddedEnumTypes []schema.EnumType
|
||||
@ -41,23 +41,23 @@ type ModuleSchemaDiff struct {
|
||||
func CompareModuleSchemas(oldSchema, newSchema schema.ModuleSchema) ModuleSchemaDiff {
|
||||
diff := ModuleSchemaDiff{}
|
||||
|
||||
oldSchema.ObjectTypes(func(oldObj schema.ObjectType) bool {
|
||||
newObj, found := newSchema.LookupObjectType(oldObj.Name)
|
||||
oldSchema.StateObjectTypes(func(oldObj schema.StateObjectType) bool {
|
||||
newObj, found := newSchema.LookupStateObjectType(oldObj.Name)
|
||||
if !found {
|
||||
diff.RemovedObjectTypes = append(diff.RemovedObjectTypes, oldObj)
|
||||
diff.RemovedStateObjectTypes = append(diff.RemovedStateObjectTypes, oldObj)
|
||||
return true
|
||||
}
|
||||
objDiff := compareObjectType(oldObj, newObj)
|
||||
if !objDiff.Empty() {
|
||||
diff.ChangedObjectTypes = append(diff.ChangedObjectTypes, objDiff)
|
||||
diff.ChangedStateObjectTypes = append(diff.ChangedStateObjectTypes, objDiff)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
newSchema.ObjectTypes(func(newObj schema.ObjectType) bool {
|
||||
_, found := oldSchema.LookupObjectType(newObj.TypeName())
|
||||
newSchema.StateObjectTypes(func(newObj schema.StateObjectType) bool {
|
||||
_, found := oldSchema.LookupStateObjectType(newObj.TypeName())
|
||||
if !found {
|
||||
diff.AddedObjectTypes = append(diff.AddedObjectTypes, newObj)
|
||||
diff.AddedStateObjectTypes = append(diff.AddedStateObjectTypes, newObj)
|
||||
}
|
||||
return true
|
||||
})
|
||||
@ -87,9 +87,9 @@ func CompareModuleSchemas(oldSchema, newSchema schema.ModuleSchema) ModuleSchema
|
||||
}
|
||||
|
||||
func (m ModuleSchemaDiff) Empty() bool {
|
||||
return len(m.AddedObjectTypes) == 0 &&
|
||||
len(m.ChangedObjectTypes) == 0 &&
|
||||
len(m.RemovedObjectTypes) == 0 &&
|
||||
return len(m.AddedStateObjectTypes) == 0 &&
|
||||
len(m.ChangedStateObjectTypes) == 0 &&
|
||||
len(m.RemovedStateObjectTypes) == 0 &&
|
||||
len(m.AddedEnumTypes) == 0 &&
|
||||
len(m.ChangedEnumTypes) == 0 &&
|
||||
len(m.RemovedEnumTypes) == 0
|
||||
@ -102,11 +102,11 @@ func (m ModuleSchemaDiff) Empty() bool {
|
||||
func (m ModuleSchemaDiff) HasCompatibleChanges() bool {
|
||||
// object and enum types can be added but not removed
|
||||
// changed object and enum types must have compatible changes
|
||||
if len(m.RemovedObjectTypes) != 0 || len(m.RemovedEnumTypes) != 0 {
|
||||
if len(m.RemovedStateObjectTypes) != 0 || len(m.RemovedEnumTypes) != 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, objectType := range m.ChangedObjectTypes {
|
||||
for _, objectType := range m.ChangedStateObjectTypes {
|
||||
if !objectType.HasCompatibleChanges() {
|
||||
return false
|
||||
}
|
||||
|
||||
@ -18,11 +18,11 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "no change",
|
||||
oldSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
oldSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
@ -33,12 +33,12 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
{
|
||||
name: "object type added",
|
||||
oldSchema: requireModuleSchema(t),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
diff: ModuleSchemaDiff{
|
||||
AddedObjectTypes: []schema.ObjectType{
|
||||
AddedStateObjectTypes: []schema.StateObjectType{
|
||||
{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
@ -49,13 +49,13 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "object type removed",
|
||||
oldSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
oldSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
newSchema: requireModuleSchema(t),
|
||||
diff: ModuleSchemaDiff{
|
||||
RemovedObjectTypes: []schema.ObjectType{
|
||||
RemovedStateObjectTypes: []schema.StateObjectType{
|
||||
{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
@ -66,16 +66,16 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "object type changed, key field added",
|
||||
oldSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
oldSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}, {Name: "key2", Kind: schema.StringKind}},
|
||||
}),
|
||||
diff: ModuleSchemaDiff{
|
||||
ChangedObjectTypes: []ObjectTypeDiff{
|
||||
ChangedStateObjectTypes: []StateObjectTypeDiff{
|
||||
{
|
||||
Name: "object1",
|
||||
KeyFieldsDiff: FieldsDiff{
|
||||
@ -90,17 +90,17 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "object type changed, nullable value field added",
|
||||
oldSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
oldSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
ValueFields: []schema.Field{{Name: "value1", Kind: schema.StringKind, Nullable: true}},
|
||||
}),
|
||||
diff: ModuleSchemaDiff{
|
||||
ChangedObjectTypes: []ObjectTypeDiff{
|
||||
ChangedStateObjectTypes: []StateObjectTypeDiff{
|
||||
{
|
||||
Name: "object1",
|
||||
ValueFieldsDiff: FieldsDiff{
|
||||
@ -113,17 +113,17 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "object type changed, non-nullable value field added",
|
||||
oldSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
oldSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
|
||||
ValueFields: []schema.Field{{Name: "value1", Kind: schema.StringKind}},
|
||||
}),
|
||||
diff: ModuleSchemaDiff{
|
||||
ChangedObjectTypes: []ObjectTypeDiff{
|
||||
ChangedStateObjectTypes: []StateObjectTypeDiff{
|
||||
{
|
||||
Name: "object1",
|
||||
ValueFieldsDiff: FieldsDiff{
|
||||
@ -136,16 +136,16 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "object type changed, fields reordered",
|
||||
oldSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
oldSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}, {Name: "key2", Kind: schema.StringKind}},
|
||||
}),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key2", Kind: schema.StringKind}, {Name: "key1", Kind: schema.StringKind}},
|
||||
}),
|
||||
diff: ModuleSchemaDiff{
|
||||
ChangedObjectTypes: []ObjectTypeDiff{
|
||||
ChangedStateObjectTypes: []StateObjectTypeDiff{
|
||||
{
|
||||
Name: "object1",
|
||||
KeyFieldsDiff: FieldsDiff{
|
||||
@ -159,11 +159,11 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "enum type added, nullable value field added",
|
||||
oldSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
oldSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.Int32Kind}},
|
||||
}),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.Int32Kind}},
|
||||
ValueFields: []schema.Field{
|
||||
@ -177,7 +177,7 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
schema.EnumType{Name: "enum1", Values: []schema.EnumValueDefinition{{Name: "a", Value: 1}, {Name: "b", Value: 2}}}),
|
||||
diff: ModuleSchemaDiff{
|
||||
ChangedObjectTypes: []ObjectTypeDiff{
|
||||
ChangedStateObjectTypes: []StateObjectTypeDiff{
|
||||
{
|
||||
Name: "object1",
|
||||
ValueFieldsDiff: FieldsDiff{
|
||||
@ -201,7 +201,7 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
{
|
||||
name: "enum type removed",
|
||||
oldSchema: requireModuleSchema(t,
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.Int32Kind}},
|
||||
ValueFields: []schema.Field{
|
||||
@ -213,12 +213,12 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
},
|
||||
},
|
||||
schema.EnumType{Name: "enum1", Values: []schema.EnumValueDefinition{{Name: "a", Value: 1}, {Name: "b", Value: 2}}}),
|
||||
newSchema: requireModuleSchema(t, schema.ObjectType{
|
||||
newSchema: requireModuleSchema(t, schema.StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.Int32Kind}},
|
||||
}),
|
||||
diff: ModuleSchemaDiff{
|
||||
ChangedObjectTypes: []ObjectTypeDiff{
|
||||
ChangedStateObjectTypes: []StateObjectTypeDiff{
|
||||
{
|
||||
Name: "object1",
|
||||
ValueFieldsDiff: FieldsDiff{
|
||||
@ -277,27 +277,27 @@ func TestCompareModuleSchemas(t *testing.T) {
|
||||
{
|
||||
name: "object type and enum type name switched",
|
||||
oldSchema: requireModuleSchema(t,
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "foo",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.EnumKind, ReferencedType: "bar"}},
|
||||
},
|
||||
schema.EnumType{Name: "bar", Values: []schema.EnumValueDefinition{{Name: "a", Value: 1}}},
|
||||
),
|
||||
newSchema: requireModuleSchema(t,
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "bar",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.EnumKind, ReferencedType: "foo"}},
|
||||
},
|
||||
schema.EnumType{Name: "foo", Values: []schema.EnumValueDefinition{{Name: "a", Value: 1}}},
|
||||
),
|
||||
diff: ModuleSchemaDiff{
|
||||
RemovedObjectTypes: []schema.ObjectType{
|
||||
RemovedStateObjectTypes: []schema.StateObjectType{
|
||||
{
|
||||
Name: "foo",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.EnumKind, ReferencedType: "bar"}},
|
||||
},
|
||||
},
|
||||
AddedObjectTypes: []schema.ObjectType{
|
||||
AddedStateObjectTypes: []schema.StateObjectType{
|
||||
{
|
||||
Name: "bar",
|
||||
KeyFields: []schema.Field{{Name: "key1", Kind: schema.EnumKind, ReferencedType: "foo"}},
|
||||
|
||||
@ -2,10 +2,10 @@ package diff
|
||||
|
||||
import "cosmossdk.io/schema"
|
||||
|
||||
// ObjectTypeDiff represents the difference between two object types.
|
||||
// StateObjectTypeDiff represents the difference between two object types.
|
||||
// The Empty method of KeyFieldsDiff and ValueFieldsDiff can be used to determine
|
||||
// if there were any changes to the key fields or value fields.
|
||||
type ObjectTypeDiff struct {
|
||||
type StateObjectTypeDiff struct {
|
||||
// Name is the name of the object type.
|
||||
Name string
|
||||
|
||||
@ -39,8 +39,8 @@ type FieldsDiff struct {
|
||||
NewOrder []string
|
||||
}
|
||||
|
||||
func compareObjectType(oldObj, newObj schema.ObjectType) ObjectTypeDiff {
|
||||
diff := ObjectTypeDiff{
|
||||
func compareObjectType(oldObj, newObj schema.StateObjectType) StateObjectTypeDiff {
|
||||
diff := StateObjectTypeDiff{
|
||||
Name: oldObj.TypeName(),
|
||||
}
|
||||
|
||||
@ -101,13 +101,13 @@ func compareFields(oldFields, newFields []schema.Field) FieldsDiff {
|
||||
}
|
||||
|
||||
// Empty returns true if the object type diff has no changes.
|
||||
func (o ObjectTypeDiff) Empty() bool {
|
||||
func (o StateObjectTypeDiff) Empty() bool {
|
||||
return o.KeyFieldsDiff.Empty() && o.ValueFieldsDiff.Empty()
|
||||
}
|
||||
|
||||
// HasCompatibleChanges returns true if the diff contains only compatible changes.
|
||||
// The only supported compatible change is adding nullable value fields.
|
||||
func (o ObjectTypeDiff) HasCompatibleChanges() bool {
|
||||
func (o StateObjectTypeDiff) HasCompatibleChanges() bool {
|
||||
if !o.KeyFieldsDiff.Empty() {
|
||||
return false
|
||||
}
|
||||
@ -10,33 +10,33 @@ import (
|
||||
func Test_objectTypeDiff(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
oldType schema.ObjectType
|
||||
newType schema.ObjectType
|
||||
diff ObjectTypeDiff
|
||||
trueF func(ObjectTypeDiff) bool
|
||||
oldType schema.StateObjectType
|
||||
newType schema.StateObjectType
|
||||
diff StateObjectTypeDiff
|
||||
trueF func(StateObjectTypeDiff) bool
|
||||
hasCompatibleChanges bool
|
||||
}{
|
||||
{
|
||||
name: "no change",
|
||||
oldType: schema.ObjectType{
|
||||
oldType: schema.StateObjectType{
|
||||
KeyFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}},
|
||||
},
|
||||
newType: schema.ObjectType{
|
||||
newType: schema.StateObjectType{
|
||||
KeyFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}},
|
||||
},
|
||||
diff: ObjectTypeDiff{},
|
||||
trueF: ObjectTypeDiff.Empty,
|
||||
diff: StateObjectTypeDiff{},
|
||||
trueF: StateObjectTypeDiff.Empty,
|
||||
hasCompatibleChanges: true,
|
||||
},
|
||||
{
|
||||
name: "key fields changed",
|
||||
oldType: schema.ObjectType{
|
||||
oldType: schema.StateObjectType{
|
||||
KeyFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}},
|
||||
},
|
||||
newType: schema.ObjectType{
|
||||
newType: schema.StateObjectType{
|
||||
KeyFields: []schema.Field{{Name: "id", Kind: schema.StringKind}},
|
||||
},
|
||||
diff: ObjectTypeDiff{
|
||||
diff: StateObjectTypeDiff{
|
||||
KeyFieldsDiff: FieldsDiff{
|
||||
Changed: []FieldDiff{
|
||||
{
|
||||
@ -47,18 +47,18 @@ func Test_objectTypeDiff(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
trueF: func(d ObjectTypeDiff) bool { return !d.KeyFieldsDiff.Empty() },
|
||||
trueF: func(d StateObjectTypeDiff) bool { return !d.KeyFieldsDiff.Empty() },
|
||||
hasCompatibleChanges: false,
|
||||
},
|
||||
{
|
||||
name: "value fields changed",
|
||||
oldType: schema.ObjectType{
|
||||
oldType: schema.StateObjectType{
|
||||
ValueFields: []schema.Field{{Name: "name", Kind: schema.StringKind}},
|
||||
},
|
||||
newType: schema.ObjectType{
|
||||
newType: schema.StateObjectType{
|
||||
ValueFields: []schema.Field{{Name: "name", Kind: schema.Int32Kind}},
|
||||
},
|
||||
diff: ObjectTypeDiff{
|
||||
diff: StateObjectTypeDiff{
|
||||
ValueFieldsDiff: FieldsDiff{
|
||||
Changed: []FieldDiff{
|
||||
{
|
||||
@ -69,52 +69,52 @@ func Test_objectTypeDiff(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
trueF: func(d ObjectTypeDiff) bool { return !d.ValueFieldsDiff.Empty() },
|
||||
trueF: func(d StateObjectTypeDiff) bool { return !d.ValueFieldsDiff.Empty() },
|
||||
hasCompatibleChanges: false,
|
||||
},
|
||||
{
|
||||
name: "nullable value field added",
|
||||
oldType: schema.ObjectType{
|
||||
oldType: schema.StateObjectType{
|
||||
ValueFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}},
|
||||
},
|
||||
newType: schema.ObjectType{
|
||||
newType: schema.StateObjectType{
|
||||
ValueFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}, {Name: "name", Kind: schema.StringKind, Nullable: true}},
|
||||
},
|
||||
diff: ObjectTypeDiff{
|
||||
diff: StateObjectTypeDiff{
|
||||
ValueFieldsDiff: FieldsDiff{
|
||||
Added: []schema.Field{{Name: "name", Kind: schema.StringKind, Nullable: true}},
|
||||
},
|
||||
},
|
||||
trueF: func(d ObjectTypeDiff) bool { return !d.ValueFieldsDiff.Empty() },
|
||||
trueF: func(d StateObjectTypeDiff) bool { return !d.ValueFieldsDiff.Empty() },
|
||||
hasCompatibleChanges: true,
|
||||
},
|
||||
{
|
||||
name: "non-nullable value field added",
|
||||
oldType: schema.ObjectType{
|
||||
oldType: schema.StateObjectType{
|
||||
ValueFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}},
|
||||
},
|
||||
newType: schema.ObjectType{
|
||||
newType: schema.StateObjectType{
|
||||
ValueFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}, {Name: "name", Kind: schema.StringKind}},
|
||||
},
|
||||
diff: ObjectTypeDiff{
|
||||
diff: StateObjectTypeDiff{
|
||||
ValueFieldsDiff: FieldsDiff{
|
||||
Added: []schema.Field{{Name: "name", Kind: schema.StringKind}},
|
||||
},
|
||||
},
|
||||
trueF: func(d ObjectTypeDiff) bool { return !d.ValueFieldsDiff.Empty() },
|
||||
trueF: func(d StateObjectTypeDiff) bool { return !d.ValueFieldsDiff.Empty() },
|
||||
hasCompatibleChanges: false,
|
||||
},
|
||||
{
|
||||
name: "fields reordered",
|
||||
oldType: schema.ObjectType{
|
||||
oldType: schema.StateObjectType{
|
||||
KeyFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}, {Name: "name", Kind: schema.StringKind}},
|
||||
ValueFields: []schema.Field{{Name: "x", Kind: schema.Int32Kind}, {Name: "y", Kind: schema.StringKind}},
|
||||
},
|
||||
newType: schema.ObjectType{
|
||||
newType: schema.StateObjectType{
|
||||
KeyFields: []schema.Field{{Name: "name", Kind: schema.StringKind}, {Name: "id", Kind: schema.Int32Kind}},
|
||||
ValueFields: []schema.Field{{Name: "y", Kind: schema.StringKind}, {Name: "x", Kind: schema.Int32Kind}},
|
||||
},
|
||||
diff: ObjectTypeDiff{
|
||||
diff: StateObjectTypeDiff{
|
||||
KeyFieldsDiff: FieldsDiff{
|
||||
OldOrder: []string{"id", "name"},
|
||||
NewOrder: []string{"name", "id"},
|
||||
@ -124,7 +124,7 @@ func Test_objectTypeDiff(t *testing.T) {
|
||||
NewOrder: []string{"y", "x"},
|
||||
},
|
||||
},
|
||||
trueF: func(d ObjectTypeDiff) bool { return !d.KeyFieldsDiff.Empty() && !d.ValueFieldsDiff.Empty() },
|
||||
trueF: func(d StateObjectTypeDiff) bool { return !d.KeyFieldsDiff.Empty() && !d.ValueFieldsDiff.Empty() },
|
||||
hasCompatibleChanges: false,
|
||||
},
|
||||
}
|
||||
@ -2,14 +2,14 @@ package schema
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ValidateObjectKey validates that the value conforms to the set of fields as a Key in an ObjectUpdate.
|
||||
// See ObjectUpdate.Key for documentation on the requirements of such keys.
|
||||
// ValidateObjectKey validates that the value conforms to the set of fields as a Key in an StateObjectUpdate.
|
||||
// See StateObjectUpdate.Key for documentation on the requirements of such keys.
|
||||
func ValidateObjectKey(keyFields []Field, value interface{}, typeSet TypeSet) error {
|
||||
return validateFieldsValue(keyFields, value, typeSet)
|
||||
}
|
||||
|
||||
// ValidateObjectValue validates that the value conforms to the set of fields as a Value in an ObjectUpdate.
|
||||
// See ObjectUpdate.Value for documentation on the requirements of such values.
|
||||
// ValidateObjectValue validates that the value conforms to the set of fields as a Value in an StateObjectUpdate.
|
||||
// See StateObjectUpdate.Value for documentation on the requirements of such values.
|
||||
func ValidateObjectValue(valueFields []Field, value interface{}, typeSet TypeSet) error {
|
||||
valueUpdates, ok := value.(ValueUpdates)
|
||||
if !ok {
|
||||
|
||||
@ -57,13 +57,13 @@ func (s ModuleSchema) Validate() error {
|
||||
}
|
||||
|
||||
// ValidateObjectUpdate validates that the update conforms to the module schema.
|
||||
func (s ModuleSchema) ValidateObjectUpdate(update ObjectUpdate) error {
|
||||
func (s ModuleSchema) ValidateObjectUpdate(update StateObjectUpdate) error {
|
||||
typ, ok := s.types[update.TypeName]
|
||||
if !ok {
|
||||
return fmt.Errorf("object type %q not found in module schema", update.TypeName)
|
||||
}
|
||||
|
||||
objTyp, ok := typ.(ObjectType)
|
||||
objTyp, ok := typ.(StateObjectType)
|
||||
if !ok {
|
||||
return fmt.Errorf("type %q is not an object type", update.TypeName)
|
||||
}
|
||||
@ -91,14 +91,14 @@ func (s ModuleSchema) LookupEnumType(name string) (t EnumType, found bool) {
|
||||
}
|
||||
|
||||
// LookupObjectType is a convenience method that looks up an ObjectType by name.
|
||||
func (s ModuleSchema) LookupObjectType(name string) (t ObjectType, found bool) {
|
||||
func (s ModuleSchema) LookupStateObjectType(name string) (t StateObjectType, found bool) {
|
||||
typ, found := s.LookupType(name)
|
||||
if !found {
|
||||
return ObjectType{}, false
|
||||
return StateObjectType{}, false
|
||||
}
|
||||
t, ok := typ.(ObjectType)
|
||||
t, ok := typ.(StateObjectType)
|
||||
if !ok {
|
||||
return ObjectType{}, false
|
||||
return StateObjectType{}, false
|
||||
}
|
||||
return t, true
|
||||
}
|
||||
@ -119,9 +119,9 @@ func (s ModuleSchema) AllTypes(f func(Type) bool) {
|
||||
}
|
||||
|
||||
// ObjectTypes iterators over all the object types in the schema in alphabetical order.
|
||||
func (s ModuleSchema) ObjectTypes(f func(ObjectType) bool) {
|
||||
func (s ModuleSchema) StateObjectTypes(f func(StateObjectType) bool) {
|
||||
s.AllTypes(func(t Type) bool {
|
||||
objTyp, ok := t.(ObjectType)
|
||||
objTyp, ok := t.(StateObjectType)
|
||||
if ok {
|
||||
return f(objTyp)
|
||||
}
|
||||
@ -141,8 +141,8 @@ func (s ModuleSchema) EnumTypes(f func(EnumType) bool) {
|
||||
}
|
||||
|
||||
type moduleSchemaJson struct {
|
||||
ObjectTypes []ObjectType `json:"object_types"`
|
||||
EnumTypes []EnumType `json:"enum_types"`
|
||||
ObjectTypes []StateObjectType `json:"object_types"`
|
||||
EnumTypes []EnumType `json:"enum_types"`
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaler interface for ModuleSchema.
|
||||
@ -151,7 +151,7 @@ type moduleSchemaJson struct {
|
||||
func (s ModuleSchema) MarshalJSON() ([]byte, error) {
|
||||
asJson := moduleSchemaJson{}
|
||||
|
||||
s.ObjectTypes(func(objType ObjectType) bool {
|
||||
s.StateObjectTypes(func(objType StateObjectType) bool {
|
||||
asJson.ObjectTypes = append(asJson.ObjectTypes, objType)
|
||||
return true
|
||||
})
|
||||
|
||||
@ -15,7 +15,7 @@ func TestModuleSchema_Validate(t *testing.T) {
|
||||
{
|
||||
name: "valid module schema",
|
||||
types: []Type{
|
||||
ObjectType{
|
||||
StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -30,7 +30,7 @@ func TestModuleSchema_Validate(t *testing.T) {
|
||||
{
|
||||
name: "invalid object type",
|
||||
types: []Type{
|
||||
ObjectType{
|
||||
StateObjectType{
|
||||
Name: "",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -45,7 +45,7 @@ func TestModuleSchema_Validate(t *testing.T) {
|
||||
{
|
||||
name: "duplicate type name",
|
||||
types: []Type{
|
||||
ObjectType{
|
||||
StateObjectType{
|
||||
Name: "type1",
|
||||
ValueFields: []Field{
|
||||
{
|
||||
@ -85,13 +85,13 @@ func TestModuleSchema_ValidateObjectUpdate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
moduleSchema ModuleSchema
|
||||
objectUpdate ObjectUpdate
|
||||
objectUpdate StateObjectUpdate
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "valid object update",
|
||||
moduleSchema: requireModuleSchema(t,
|
||||
ObjectType{
|
||||
StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -101,7 +101,7 @@ func TestModuleSchema_ValidateObjectUpdate(t *testing.T) {
|
||||
},
|
||||
},
|
||||
),
|
||||
objectUpdate: ObjectUpdate{
|
||||
objectUpdate: StateObjectUpdate{
|
||||
TypeName: "object1",
|
||||
Key: "abc",
|
||||
},
|
||||
@ -110,7 +110,7 @@ func TestModuleSchema_ValidateObjectUpdate(t *testing.T) {
|
||||
{
|
||||
name: "object type not found",
|
||||
moduleSchema: requireModuleSchema(t,
|
||||
ObjectType{
|
||||
StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -120,7 +120,7 @@ func TestModuleSchema_ValidateObjectUpdate(t *testing.T) {
|
||||
},
|
||||
},
|
||||
),
|
||||
objectUpdate: ObjectUpdate{
|
||||
objectUpdate: StateObjectUpdate{
|
||||
TypeName: "object2",
|
||||
Key: "abc",
|
||||
},
|
||||
@ -128,7 +128,7 @@ func TestModuleSchema_ValidateObjectUpdate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "type name refers to an enum",
|
||||
moduleSchema: requireModuleSchema(t, ObjectType{
|
||||
moduleSchema: requireModuleSchema(t, StateObjectType{
|
||||
Name: "obj1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -143,7 +143,7 @@ func TestModuleSchema_ValidateObjectUpdate(t *testing.T) {
|
||||
Values: []EnumValueDefinition{{Name: "a", Value: 1}, {Name: "b", Value: 2}},
|
||||
},
|
||||
),
|
||||
objectUpdate: ObjectUpdate{
|
||||
objectUpdate: StateObjectUpdate{
|
||||
TypeName: "enum1",
|
||||
Key: "a",
|
||||
},
|
||||
@ -177,7 +177,7 @@ func requireModuleSchema(t *testing.T, types ...Type) ModuleSchema {
|
||||
}
|
||||
|
||||
func TestModuleSchema_LookupType(t *testing.T) {
|
||||
moduleSchema := requireModuleSchema(t, ObjectType{
|
||||
moduleSchema := requireModuleSchema(t, StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -187,7 +187,7 @@ func TestModuleSchema_LookupType(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
objectType, ok := moduleSchema.LookupObjectType("object1")
|
||||
objectType, ok := moduleSchema.LookupStateObjectType("object1")
|
||||
if !ok {
|
||||
t.Fatalf("expected to find object type \"object1\"")
|
||||
}
|
||||
@ -200,7 +200,7 @@ func TestModuleSchema_LookupType(t *testing.T) {
|
||||
func exampleSchema(t *testing.T) ModuleSchema {
|
||||
t.Helper()
|
||||
return requireModuleSchema(t,
|
||||
ObjectType{
|
||||
StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -210,7 +210,7 @@ func exampleSchema(t *testing.T) ModuleSchema {
|
||||
},
|
||||
},
|
||||
},
|
||||
ObjectType{
|
||||
StateObjectType{
|
||||
Name: "object2",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -262,7 +262,7 @@ func TestModuleSchema_ObjectTypes(t *testing.T) {
|
||||
moduleSchema := exampleSchema(t)
|
||||
|
||||
var typeNames []string
|
||||
moduleSchema.ObjectTypes(func(typ ObjectType) bool {
|
||||
moduleSchema.StateObjectTypes(func(typ StateObjectType) bool {
|
||||
typeNames = append(typeNames, typ.Name)
|
||||
return true
|
||||
})
|
||||
@ -274,7 +274,7 @@ func TestModuleSchema_ObjectTypes(t *testing.T) {
|
||||
|
||||
typeNames = nil
|
||||
// scan just the first type and return false
|
||||
moduleSchema.ObjectTypes(func(typ ObjectType) bool {
|
||||
moduleSchema.StateObjectTypes(func(typ StateObjectType) bool {
|
||||
typeNames = append(typeNames, typ.Name)
|
||||
return false
|
||||
})
|
||||
|
||||
@ -2,8 +2,8 @@ package schema
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ObjectType describes an object type a module schema.
|
||||
type ObjectType struct {
|
||||
// StateObjectType describes an object type a module schema.
|
||||
type StateObjectType struct {
|
||||
// Name is the name of the object type. It must be unique within the module schema amongst all object and enum
|
||||
// types and conform to the NameFormat regular expression.
|
||||
Name string `json:"name"`
|
||||
@ -30,14 +30,14 @@ type ObjectType struct {
|
||||
}
|
||||
|
||||
// TypeName implements the Type interface.
|
||||
func (o ObjectType) TypeName() string {
|
||||
func (o StateObjectType) TypeName() string {
|
||||
return o.Name
|
||||
}
|
||||
|
||||
func (ObjectType) isType() {}
|
||||
func (StateObjectType) isType() {}
|
||||
|
||||
// Validate validates the object type.
|
||||
func (o ObjectType) Validate(typeSet TypeSet) error {
|
||||
func (o StateObjectType) Validate(typeSet TypeSet) error {
|
||||
if !ValidateName(o.Name) {
|
||||
return fmt.Errorf("invalid object type name %q", o.Name)
|
||||
}
|
||||
@ -82,7 +82,7 @@ func (o ObjectType) Validate(typeSet TypeSet) error {
|
||||
}
|
||||
|
||||
// ValidateObjectUpdate validates that the update conforms to the object type.
|
||||
func (o ObjectType) ValidateObjectUpdate(update ObjectUpdate, typeSet TypeSet) error {
|
||||
func (o StateObjectType) ValidateObjectUpdate(update StateObjectUpdate, typeSet TypeSet) error {
|
||||
if o.Name != update.TypeName {
|
||||
return fmt.Errorf("object type name %q does not match update type name %q", o.Name, update.TypeName)
|
||||
}
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var object1Type = ObjectType{
|
||||
var object1Type = StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -15,7 +15,7 @@ var object1Type = ObjectType{
|
||||
},
|
||||
}
|
||||
|
||||
var object2Type = ObjectType{
|
||||
var object2Type = StateObjectType{
|
||||
KeyFields: []Field{
|
||||
{
|
||||
Name: "field1",
|
||||
@ -28,7 +28,7 @@ var object2Type = ObjectType{
|
||||
},
|
||||
}
|
||||
|
||||
var object3Type = ObjectType{
|
||||
var object3Type = StateObjectType{
|
||||
Name: "object3",
|
||||
ValueFields: []Field{
|
||||
{
|
||||
@ -42,7 +42,7 @@ var object3Type = ObjectType{
|
||||
},
|
||||
}
|
||||
|
||||
var object4Type = ObjectType{
|
||||
var object4Type = StateObjectType{
|
||||
Name: "object4",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -61,7 +61,7 @@ var object4Type = ObjectType{
|
||||
func TestObjectType_Validate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
objectType ObjectType
|
||||
objectType StateObjectType
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
@ -71,7 +71,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "empty object type name",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -84,7 +84,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "invalid key field",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -97,7 +97,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "invalid value field",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "object1",
|
||||
ValueFields: []Field{
|
||||
{
|
||||
@ -109,12 +109,12 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "no fields",
|
||||
objectType: ObjectType{Name: "object0"},
|
||||
objectType: StateObjectType{Name: "object0"},
|
||||
errContains: "has no key or value fields",
|
||||
},
|
||||
{
|
||||
name: "duplicate field",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -133,7 +133,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "duplicate field 22",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "object1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -150,7 +150,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "nullable key field",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "objectNullKey",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -164,7 +164,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "float32 key field",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "o1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -177,7 +177,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "float64 key field",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "o1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -190,7 +190,7 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "json key field",
|
||||
objectType: ObjectType{
|
||||
objectType: StateObjectType{
|
||||
Name: "o1",
|
||||
KeyFields: []Field{
|
||||
{
|
||||
@ -222,14 +222,14 @@ func TestObjectType_Validate(t *testing.T) {
|
||||
func TestObjectType_ValidateObjectUpdate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
objectType ObjectType
|
||||
object ObjectUpdate
|
||||
objectType StateObjectType
|
||||
object StateObjectUpdate
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "wrong name",
|
||||
objectType: object1Type,
|
||||
object: ObjectUpdate{
|
||||
object: StateObjectUpdate{
|
||||
TypeName: "object2",
|
||||
Key: "hello",
|
||||
},
|
||||
@ -238,7 +238,7 @@ func TestObjectType_ValidateObjectUpdate(t *testing.T) {
|
||||
{
|
||||
name: "invalid value",
|
||||
objectType: object1Type,
|
||||
object: ObjectUpdate{
|
||||
object: StateObjectUpdate{
|
||||
TypeName: "object1",
|
||||
Key: 123,
|
||||
},
|
||||
@ -247,7 +247,7 @@ func TestObjectType_ValidateObjectUpdate(t *testing.T) {
|
||||
{
|
||||
name: "valid update",
|
||||
objectType: object4Type,
|
||||
object: ObjectUpdate{
|
||||
object: StateObjectUpdate{
|
||||
TypeName: "object4",
|
||||
Key: int32(123),
|
||||
Value: "hello",
|
||||
@ -256,7 +256,7 @@ func TestObjectType_ValidateObjectUpdate(t *testing.T) {
|
||||
{
|
||||
name: "valid deletion",
|
||||
objectType: object4Type,
|
||||
object: ObjectUpdate{
|
||||
object: StateObjectUpdate{
|
||||
TypeName: "object4",
|
||||
Key: int32(123),
|
||||
Value: "ignored!",
|
||||
@ -2,8 +2,8 @@ package schema
|
||||
|
||||
import "sort"
|
||||
|
||||
// ObjectUpdate represents an update operation on an object in a module's state.
|
||||
type ObjectUpdate struct {
|
||||
// StateObjectUpdate represents an update operation on an object in a module's state.
|
||||
type StateObjectUpdate struct {
|
||||
// TypeName is the name of the object type in the module's schema.
|
||||
TypeName string
|
||||
|
||||
@ -19,7 +19,7 @@ type ObjectUpdate struct {
|
||||
Key interface{}
|
||||
|
||||
// Value returns the non-primary key fields of the object and can either conform to the same constraints
|
||||
// as ObjectUpdate.Key or it may be and instance of ValueUpdates. ValueUpdates can be used as a performance
|
||||
// as StateObjectUpdate.Key or it may be and instance of ValueUpdates. ValueUpdates can be used as a performance
|
||||
// optimization to avoid copying the values of the object into the update and/or to omit unchanged fields.
|
||||
// If this is a delete operation, then this value is ignored and can be nil.
|
||||
Value interface{}
|
||||
@ -119,7 +119,7 @@ func (a *Simulator) BlockDataGenN(minUpdatesPerBlock, maxUpdatesPerBlock int) *r
|
||||
})
|
||||
}
|
||||
|
||||
func (a *Simulator) formatUpdateKey(moduleName string, update schema.ObjectUpdate) string {
|
||||
func (a *Simulator) formatUpdateKey(moduleName string, update schema.StateObjectUpdate) string {
|
||||
mod, err := a.state.GetModule(moduleName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
var ExampleAppSchema = map[string]schema.ModuleSchema{
|
||||
"all_kinds": mkAllKindsModule(),
|
||||
"test_cases": schema.MustCompileModuleSchema(
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "Singleton",
|
||||
KeyFields: []schema.Field{},
|
||||
ValueFields: []schema.Field{
|
||||
@ -25,7 +25,7 @@ var ExampleAppSchema = map[string]schema.ModuleSchema{
|
||||
},
|
||||
},
|
||||
},
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "Simple",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
@ -44,7 +44,7 @@ var ExampleAppSchema = map[string]schema.ModuleSchema{
|
||||
},
|
||||
},
|
||||
},
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "TwoKeys",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
@ -57,7 +57,7 @@ var ExampleAppSchema = map[string]schema.ModuleSchema{
|
||||
},
|
||||
},
|
||||
},
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "ThreeKeys",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
@ -80,7 +80,7 @@ var ExampleAppSchema = map[string]schema.ModuleSchema{
|
||||
},
|
||||
},
|
||||
},
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "ManyValues",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
@ -107,7 +107,7 @@ var ExampleAppSchema = map[string]schema.ModuleSchema{
|
||||
},
|
||||
},
|
||||
},
|
||||
schema.ObjectType{
|
||||
schema.StateObjectType{
|
||||
Name: "RetainDeletions",
|
||||
KeyFields: []schema.Field{
|
||||
{
|
||||
@ -141,7 +141,7 @@ func mkAllKindsModule() schema.ModuleSchema {
|
||||
return schema.MustCompileModuleSchema(types...)
|
||||
}
|
||||
|
||||
func mkTestObjectType(kind schema.Kind) schema.ObjectType {
|
||||
func mkTestObjectType(kind schema.Kind) schema.StateObjectType {
|
||||
field := schema.Field{
|
||||
Kind: kind,
|
||||
}
|
||||
@ -161,7 +161,7 @@ func mkTestObjectType(kind schema.Kind) schema.ObjectType {
|
||||
val2Field.Name = "valNullable"
|
||||
val2Field.Nullable = true
|
||||
|
||||
return schema.ObjectType{
|
||||
return schema.StateObjectType{
|
||||
Name: fmt.Sprintf("test_%v", kind),
|
||||
KeyFields: []schema.Field{keyField},
|
||||
ValueFields: []schema.Field{val1Field, val2Field},
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
// ObjectKeyString formats the object key as a string deterministically for storage in a map.
|
||||
// The key must be valid for the object type and the object type must be valid.
|
||||
// No validation is performed here.
|
||||
func ObjectKeyString(objectType schema.ObjectType, key any) string {
|
||||
func ObjectKeyString(objectType schema.StateObjectType, key any) string {
|
||||
keyFields := objectType.KeyFields
|
||||
n := len(keyFields)
|
||||
switch n {
|
||||
|
||||
@ -8,12 +8,12 @@ import (
|
||||
|
||||
func TestObjectKeyString(t *testing.T) {
|
||||
tt := []struct {
|
||||
objectType schema.ObjectType
|
||||
objectType schema.StateObjectType
|
||||
key any
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
objectType: schema.ObjectType{
|
||||
objectType: schema.StateObjectType{
|
||||
Name: "Singleton",
|
||||
ValueFields: []schema.Field{
|
||||
{Name: "Value", Kind: schema.StringKind},
|
||||
@ -23,7 +23,7 @@ func TestObjectKeyString(t *testing.T) {
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
objectType: schema.ObjectType{
|
||||
objectType: schema.StateObjectType{
|
||||
Name: "Simple",
|
||||
KeyFields: []schema.Field{{Name: "Key", Kind: schema.StringKind}},
|
||||
},
|
||||
@ -31,7 +31,7 @@ func TestObjectKeyString(t *testing.T) {
|
||||
expected: "Key=key",
|
||||
},
|
||||
{
|
||||
objectType: schema.ObjectType{
|
||||
objectType: schema.StateObjectType{
|
||||
Name: "BytesAddressDecInt",
|
||||
KeyFields: []schema.Field{
|
||||
{Name: "Bz", Kind: schema.BytesKind},
|
||||
|
||||
@ -16,7 +16,7 @@ func ModuleSchemaGen() *rapid.Generator[schema.ModuleSchema] {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
objectTypes := distinctTypes(ObjectTypeGen(tempSchema)).Draw(t, "objectTypes")
|
||||
objectTypes := distinctTypes(StateObjectTypeGen(tempSchema)).Draw(t, "objectTypes")
|
||||
allTypes := append(enumTypes, objectTypes...)
|
||||
|
||||
modSchema, err := schema.CompileModuleSchema(allTypes...)
|
||||
|
||||
@ -7,8 +7,8 @@ import (
|
||||
"cosmossdk.io/schema"
|
||||
)
|
||||
|
||||
// ObjectTypeGen generates random ObjectType's based on the validity criteria of object types.
|
||||
func ObjectTypeGen(typeSet schema.TypeSet) *rapid.Generator[schema.ObjectType] {
|
||||
// StateObjectTypeGen generates random StateObjectType's based on the validity criteria of object types.
|
||||
func StateObjectTypeGen(typeSet schema.TypeSet) *rapid.Generator[schema.StateObjectType] {
|
||||
keyFieldsGen := rapid.SliceOfNDistinct(KeyFieldGen(typeSet), 1, 6, func(f schema.Field) string {
|
||||
return f.Name
|
||||
})
|
||||
@ -17,8 +17,8 @@ func ObjectTypeGen(typeSet schema.TypeSet) *rapid.Generator[schema.ObjectType] {
|
||||
return f.Name
|
||||
})
|
||||
|
||||
return rapid.Custom(func(t *rapid.T) schema.ObjectType {
|
||||
typ := schema.ObjectType{
|
||||
return rapid.Custom(func(t *rapid.T) schema.StateObjectType {
|
||||
typ := schema.StateObjectType{
|
||||
Name: NameGen.Filter(func(s string) bool {
|
||||
// filter out names that already exist in the schema
|
||||
_, found := typeSet.LookupType(s)
|
||||
@ -31,7 +31,7 @@ func ObjectTypeGen(typeSet schema.TypeSet) *rapid.Generator[schema.ObjectType] {
|
||||
typ.RetainDeletions = boolGen.Draw(t, "retainDeletions")
|
||||
|
||||
return typ
|
||||
}).Filter(func(typ schema.ObjectType) bool {
|
||||
}).Filter(func(typ schema.StateObjectType) bool {
|
||||
// filter out duplicate field names
|
||||
fieldNames := map[string]bool{}
|
||||
if hasDuplicateFieldNames(fieldNames, typ.KeyFields) {
|
||||
@ -55,14 +55,14 @@ func hasDuplicateFieldNames(typeNames map[string]bool, fields []schema.Field) bo
|
||||
return false
|
||||
}
|
||||
|
||||
// ObjectInsertGen generates object updates that are valid for insertion.
|
||||
func ObjectInsertGen(objectType schema.ObjectType, typeSet schema.TypeSet) *rapid.Generator[schema.ObjectUpdate] {
|
||||
return ObjectUpdateGen(objectType, nil, typeSet)
|
||||
// StateObjectInsertGen generates object updates that are valid for insertion.
|
||||
func StateObjectInsertGen(objectType schema.StateObjectType, typeSet schema.TypeSet) *rapid.Generator[schema.StateObjectUpdate] {
|
||||
return StateObjectUpdateGen(objectType, nil, typeSet)
|
||||
}
|
||||
|
||||
// ObjectUpdateGen generates object updates that are valid for updates using the provided state map as a source
|
||||
// StateObjectUpdateGen generates object updates that are valid for updates using the provided state map as a source
|
||||
// of valid existing keys.
|
||||
func ObjectUpdateGen(objectType schema.ObjectType, state *btree.Map[string, schema.ObjectUpdate], sch schema.TypeSet) *rapid.Generator[schema.ObjectUpdate] {
|
||||
func StateObjectUpdateGen(objectType schema.StateObjectType, state *btree.Map[string, schema.StateObjectUpdate], sch schema.TypeSet) *rapid.Generator[schema.StateObjectUpdate] {
|
||||
keyGen := ObjectKeyGen(objectType.KeyFields, sch).Filter(func(key interface{}) bool {
|
||||
// filter out keys that exist in the state
|
||||
if state != nil {
|
||||
@ -73,8 +73,8 @@ func ObjectUpdateGen(objectType schema.ObjectType, state *btree.Map[string, sche
|
||||
})
|
||||
insertValueGen := ObjectValueGen(objectType.ValueFields, false, sch)
|
||||
updateValueGen := ObjectValueGen(objectType.ValueFields, true, sch)
|
||||
return rapid.Custom(func(t *rapid.T) schema.ObjectUpdate {
|
||||
update := schema.ObjectUpdate{
|
||||
return rapid.Custom(func(t *rapid.T) schema.StateObjectUpdate {
|
||||
update := schema.StateObjectUpdate{
|
||||
TypeName: objectType.Name,
|
||||
}
|
||||
|
||||
|
||||
@ -9,16 +9,16 @@ import (
|
||||
|
||||
func TestObject(t *testing.T) {
|
||||
rapid.Check(t, func(t *rapid.T) {
|
||||
objectType := ObjectTypeGen(testEnumSchema).Draw(t, "object")
|
||||
objectType := StateObjectTypeGen(testEnumSchema).Draw(t, "object")
|
||||
require.NoError(t, objectType.Validate(testEnumSchema))
|
||||
})
|
||||
}
|
||||
|
||||
func TestObjectUpdate(t *testing.T) {
|
||||
rapid.Check(t, func(t *rapid.T) {
|
||||
objectType := ObjectTypeGen(testEnumSchema).Draw(t, "object")
|
||||
objectType := StateObjectTypeGen(testEnumSchema).Draw(t, "object")
|
||||
require.NoError(t, objectType.Validate(testEnumSchema))
|
||||
update := ObjectInsertGen(objectType, testEnumSchema).Draw(t, "update")
|
||||
update := StateObjectInsertGen(objectType, testEnumSchema).Draw(t, "update")
|
||||
require.NoError(t, objectType.ValidateObjectUpdate(update, testEnumSchema))
|
||||
})
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ func NewApp(appSchema map[string]schema.ModuleSchema, options Options) *App {
|
||||
moduleState, ok := app.moduleStates.Get(moduleName)
|
||||
require.True(t, ok)
|
||||
numUpdates := numUpdatesGen.Draw(t, "numUpdates")
|
||||
updates := make([]schema.ObjectUpdate, numUpdates)
|
||||
updates := make([]schema.StateObjectUpdate, numUpdates)
|
||||
for i := 0; i < numUpdates; i++ {
|
||||
update := moduleState.UpdateGen().Draw(t, fmt.Sprintf("update[%d]", i))
|
||||
updates[i] = update
|
||||
|
||||
@ -16,7 +16,7 @@ type Module struct {
|
||||
name string
|
||||
moduleSchema schema.ModuleSchema
|
||||
objectCollections *btree.Map[string, *ObjectCollection]
|
||||
updateGen *rapid.Generator[schema.ObjectUpdate]
|
||||
updateGen *rapid.Generator[schema.StateObjectUpdate]
|
||||
}
|
||||
|
||||
// NewModule creates a new Module for the given module schema.
|
||||
@ -24,7 +24,7 @@ func NewModule(name string, moduleSchema schema.ModuleSchema, options Options) *
|
||||
objectCollections := &btree.Map[string, *ObjectCollection]{}
|
||||
var objectTypeNames []string
|
||||
|
||||
moduleSchema.ObjectTypes(func(objectType schema.ObjectType) bool {
|
||||
moduleSchema.StateObjectTypes(func(objectType schema.StateObjectType) bool {
|
||||
objectCollection := NewObjectCollection(objectType, options, moduleSchema)
|
||||
objectCollections.Set(objectType.Name, objectCollection)
|
||||
objectTypeNames = append(objectTypeNames, objectType.Name)
|
||||
@ -33,7 +33,7 @@ func NewModule(name string, moduleSchema schema.ModuleSchema, options Options) *
|
||||
|
||||
objectTypeSelector := rapid.SampledFrom(objectTypeNames)
|
||||
|
||||
updateGen := rapid.Custom(func(t *rapid.T) schema.ObjectUpdate {
|
||||
updateGen := rapid.Custom(func(t *rapid.T) schema.StateObjectUpdate {
|
||||
objectType := objectTypeSelector.Draw(t, "objectType")
|
||||
objectColl, ok := objectCollections.Get(objectType)
|
||||
require.True(t, ok)
|
||||
@ -49,7 +49,7 @@ func NewModule(name string, moduleSchema schema.ModuleSchema, options Options) *
|
||||
}
|
||||
|
||||
// ApplyUpdate applies the given object update to the module.
|
||||
func (o *Module) ApplyUpdate(update schema.ObjectUpdate) error {
|
||||
func (o *Module) ApplyUpdate(update schema.StateObjectUpdate) error {
|
||||
objState, ok := o.objectCollections.Get(update.TypeName)
|
||||
if !ok {
|
||||
return fmt.Errorf("object type %s not found in module", update.TypeName)
|
||||
@ -60,7 +60,7 @@ func (o *Module) ApplyUpdate(update schema.ObjectUpdate) error {
|
||||
|
||||
// UpdateGen returns a generator for object updates. The generator is stateful and returns
|
||||
// a certain number of updates and deletes of existing objects in the module.
|
||||
func (o *Module) UpdateGen() *rapid.Generator[schema.ObjectUpdate] {
|
||||
func (o *Module) UpdateGen() *rapid.Generator[schema.StateObjectUpdate] {
|
||||
return o.updateGen
|
||||
}
|
||||
|
||||
|
||||
@ -10,20 +10,20 @@ import (
|
||||
schematesting "cosmossdk.io/schema/testing"
|
||||
)
|
||||
|
||||
// ObjectCollection is a collection of objects of a specific type for testing purposes.
|
||||
// ObjectCollection is a collection of state objects of a specific type for testing purposes.
|
||||
type ObjectCollection struct {
|
||||
options Options
|
||||
objectType schema.ObjectType
|
||||
objectType schema.StateObjectType
|
||||
typeSet schema.TypeSet
|
||||
objects *btree.Map[string, schema.ObjectUpdate]
|
||||
updateGen *rapid.Generator[schema.ObjectUpdate]
|
||||
objects *btree.Map[string, schema.StateObjectUpdate]
|
||||
updateGen *rapid.Generator[schema.StateObjectUpdate]
|
||||
valueFieldIndices map[string]int
|
||||
}
|
||||
|
||||
// NewObjectCollection creates a new ObjectCollection for the given object type.
|
||||
func NewObjectCollection(objectType schema.ObjectType, options Options, typeSet schema.TypeSet) *ObjectCollection {
|
||||
objects := &btree.Map[string, schema.ObjectUpdate]{}
|
||||
updateGen := schematesting.ObjectUpdateGen(objectType, objects, typeSet)
|
||||
func NewObjectCollection(objectType schema.StateObjectType, options Options, typeSet schema.TypeSet) *ObjectCollection {
|
||||
objects := &btree.Map[string, schema.StateObjectUpdate]{}
|
||||
updateGen := schematesting.StateObjectUpdateGen(objectType, objects, typeSet)
|
||||
valueFieldIndices := make(map[string]int, len(objectType.ValueFields))
|
||||
for i, field := range objectType.ValueFields {
|
||||
valueFieldIndices[field.Name] = i
|
||||
@ -40,7 +40,7 @@ func NewObjectCollection(objectType schema.ObjectType, options Options, typeSet
|
||||
}
|
||||
|
||||
// ApplyUpdate applies the given object update to the collection.
|
||||
func (o *ObjectCollection) ApplyUpdate(update schema.ObjectUpdate) error {
|
||||
func (o *ObjectCollection) ApplyUpdate(update schema.StateObjectUpdate) error {
|
||||
if update.TypeName != o.objectType.Name {
|
||||
return fmt.Errorf("update type name %q does not match object type name %q", update.TypeName, o.objectType.Name)
|
||||
}
|
||||
@ -106,27 +106,27 @@ func (o *ObjectCollection) ApplyUpdate(update schema.ObjectUpdate) error {
|
||||
|
||||
// UpdateGen returns a generator for random object updates against the collection. This generator
|
||||
// is stateful and returns a certain number of updates and deletes to existing objects.
|
||||
func (o *ObjectCollection) UpdateGen() *rapid.Generator[schema.ObjectUpdate] {
|
||||
func (o *ObjectCollection) UpdateGen() *rapid.Generator[schema.StateObjectUpdate] {
|
||||
return o.updateGen
|
||||
}
|
||||
|
||||
// AllState iterates over the state of the collection by calling the given function with each item in
|
||||
// state represented as an object update.
|
||||
func (o *ObjectCollection) AllState(f func(schema.ObjectUpdate, error) bool) {
|
||||
o.objects.Scan(func(_ string, v schema.ObjectUpdate) bool {
|
||||
func (o *ObjectCollection) AllState(f func(schema.StateObjectUpdate, error) bool) {
|
||||
o.objects.Scan(func(_ string, v schema.StateObjectUpdate) bool {
|
||||
return f(v, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// GetObject returns the object with the given key from the collection represented as an ObjectUpdate
|
||||
// itself. Deletions that are retained are returned as ObjectUpdate's with delete set to true.
|
||||
func (o *ObjectCollection) GetObject(key interface{}) (update schema.ObjectUpdate, found bool, err error) {
|
||||
// GetObject returns the object with the given key from the collection represented as an StateObjectUpdate
|
||||
// itself. Deletions that are retained are returned as StateObjectUpdate's with delete set to true.
|
||||
func (o *ObjectCollection) GetObject(key interface{}) (update schema.StateObjectUpdate, found bool, err error) {
|
||||
update, ok := o.objects.Get(schematesting.ObjectKeyString(o.objectType, key))
|
||||
return update, ok, nil
|
||||
}
|
||||
|
||||
// ObjectType returns the object type of the collection.
|
||||
func (o *ObjectCollection) ObjectType() schema.ObjectType {
|
||||
func (o *ObjectCollection) ObjectType() schema.StateObjectType {
|
||||
return o.objectType
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package schema
|
||||
|
||||
// Type is an interface that all types in the schema implement.
|
||||
// Currently, these are ObjectType and EnumType.
|
||||
// Currently, these are StateObjectType and EnumType.
|
||||
type Type interface {
|
||||
// TypeName returns the type's name.
|
||||
TypeName() string
|
||||
@ -31,8 +31,8 @@ type TypeSet interface {
|
||||
// LookupEnumType is a convenience method that looks up an EnumType by name.
|
||||
LookupEnumType(name string) (t EnumType, found bool)
|
||||
|
||||
// LookupObjectType is a convenience method that looks up an ObjectType by name.
|
||||
LookupObjectType(name string) (t ObjectType, found bool)
|
||||
// LookupStateObjectType is a convenience method that looks up an StateObjectType by name.
|
||||
LookupStateObjectType(name string) (t StateObjectType, found bool)
|
||||
|
||||
// AllTypes calls the given function for each type in the type set.
|
||||
// This function is compatible with go 1.23 iterators and can be used like this:
|
||||
@ -45,9 +45,9 @@ type TypeSet interface {
|
||||
// This function is compatible with go 1.23 iterators.
|
||||
EnumTypes(f func(EnumType) bool)
|
||||
|
||||
// ObjectTypes calls the given function for each ObjectType in the type set.
|
||||
// StateObjectTypes calls the given function for each StateObjectType in the type set.
|
||||
// This function is compatible with go 1.23 iterators.
|
||||
ObjectTypes(f func(ObjectType) bool)
|
||||
StateObjectTypes(f func(objectType StateObjectType) bool)
|
||||
|
||||
// isTypeSet is a private method that ensures that only types in this package can be marked as type sets.
|
||||
isTypeSet()
|
||||
@ -71,14 +71,14 @@ func (s emptyTypeSet) LookupEnumType(string) (t EnumType, found bool) {
|
||||
return EnumType{}, false
|
||||
}
|
||||
|
||||
func (s emptyTypeSet) LookupObjectType(string) (t ObjectType, found bool) {
|
||||
return ObjectType{}, false
|
||||
func (s emptyTypeSet) LookupStateObjectType(string) (t StateObjectType, found bool) {
|
||||
return StateObjectType{}, false
|
||||
}
|
||||
|
||||
func (emptyTypeSet) AllTypes(func(Type) bool) {}
|
||||
|
||||
func (s emptyTypeSet) EnumTypes(func(EnumType) bool) {}
|
||||
|
||||
func (s emptyTypeSet) ObjectTypes(func(ObjectType) bool) {}
|
||||
func (s emptyTypeSet) StateObjectTypes(func(objectType StateObjectType) bool) {}
|
||||
|
||||
func (emptyTypeSet) isTypeSet() {}
|
||||
|
||||
@ -3,24 +3,24 @@ package view
|
||||
import "cosmossdk.io/schema"
|
||||
|
||||
// ObjectCollection is the interface for viewing the state of a collection of objects in a module
|
||||
// represented by ObjectUpdate's for an ObjectType. ObjectUpdates must not include
|
||||
// represented by StateObjectUpdate's for an ObjectType. ObjectUpdates must not include
|
||||
// ValueUpdates in the Value field. When ValueUpdates are applied they must be
|
||||
// converted to individual value or array format depending on the number of fields in
|
||||
// the value. For collections which retain deletions, ObjectUpdate's with the Delete
|
||||
// the value. For collections which retain deletions, StateObjectUpdate's with the Delete
|
||||
// field set to true should be returned with the latest Value still intact.
|
||||
type ObjectCollection interface {
|
||||
// ObjectType returns the object type for the collection.
|
||||
ObjectType() schema.ObjectType
|
||||
ObjectType() schema.StateObjectType
|
||||
|
||||
// GetObject returns the object update for the given key if it exists. And error should only be returned
|
||||
// if there was an error getting the object update. If the object does not exist but there was no error,
|
||||
// then found should be false and the error should be nil.
|
||||
GetObject(key interface{}) (update schema.ObjectUpdate, found bool, err error)
|
||||
GetObject(key interface{}) (update schema.StateObjectUpdate, found bool, err error)
|
||||
|
||||
// AllState iterates over the state of the collection by calling the given function with each item in
|
||||
// state represented as an object update. If there is an error getting an object update, the error will be
|
||||
// non-nil and the object update should be empty.
|
||||
AllState(f func(schema.ObjectUpdate, error) bool)
|
||||
AllState(f func(schema.StateObjectUpdate, error) bool)
|
||||
|
||||
// Len returns the number of objects in the collection.
|
||||
Len() (int, error)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user