Skip to content

Commit

Permalink
more renames
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Sep 12, 2024
1 parent 4c379ad commit ba32357
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 57 deletions.
28 changes: 14 additions & 14 deletions schema/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.StateObjectType
// 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.StateObjectType
// 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
Expand Down Expand Up @@ -44,20 +44,20 @@ func CompareModuleSchemas(oldSchema, newSchema schema.ModuleSchema) ModuleSchema
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.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
})
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
20 changes: 10 additions & 10 deletions schema/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestCompareModuleSchemas(t *testing.T) {
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
}),
diff: ModuleSchemaDiff{
AddedObjectTypes: []schema.StateObjectType{
AddedStateObjectTypes: []schema.StateObjectType{
{
Name: "object1",
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
Expand All @@ -55,7 +55,7 @@ func TestCompareModuleSchemas(t *testing.T) {
}),
newSchema: requireModuleSchema(t),
diff: ModuleSchemaDiff{
RemovedObjectTypes: []schema.StateObjectType{
RemovedStateObjectTypes: []schema.StateObjectType{
{
Name: "object1",
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}},
Expand All @@ -75,7 +75,7 @@ func TestCompareModuleSchemas(t *testing.T) {
KeyFields: []schema.Field{{Name: "key1", Kind: schema.StringKind}, {Name: "key2", Kind: schema.StringKind}},
}),
diff: ModuleSchemaDiff{
ChangedObjectTypes: []ObjectTypeDiff{
ChangedStateObjectTypes: []StateObjectTypeDiff{
{
Name: "object1",
KeyFieldsDiff: FieldsDiff{
Expand All @@ -100,7 +100,7 @@ func TestCompareModuleSchemas(t *testing.T) {
ValueFields: []schema.Field{{Name: "value1", Kind: schema.StringKind, Nullable: true}},
}),
diff: ModuleSchemaDiff{
ChangedObjectTypes: []ObjectTypeDiff{
ChangedStateObjectTypes: []StateObjectTypeDiff{
{
Name: "object1",
ValueFieldsDiff: FieldsDiff{
Expand All @@ -123,7 +123,7 @@ func TestCompareModuleSchemas(t *testing.T) {
ValueFields: []schema.Field{{Name: "value1", Kind: schema.StringKind}},
}),
diff: ModuleSchemaDiff{
ChangedObjectTypes: []ObjectTypeDiff{
ChangedStateObjectTypes: []StateObjectTypeDiff{
{
Name: "object1",
ValueFieldsDiff: FieldsDiff{
Expand All @@ -145,7 +145,7 @@ func TestCompareModuleSchemas(t *testing.T) {
KeyFields: []schema.Field{{Name: "key2", Kind: schema.StringKind}, {Name: "key1", Kind: schema.StringKind}},
}),
diff: ModuleSchemaDiff{
ChangedObjectTypes: []ObjectTypeDiff{
ChangedStateObjectTypes: []StateObjectTypeDiff{
{
Name: "object1",
KeyFieldsDiff: FieldsDiff{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestCompareModuleSchemas(t *testing.T) {
KeyFields: []schema.Field{{Name: "key1", Kind: schema.Int32Kind}},
}),
diff: ModuleSchemaDiff{
ChangedObjectTypes: []ObjectTypeDiff{
ChangedStateObjectTypes: []StateObjectTypeDiff{
{
Name: "object1",
ValueFieldsDiff: FieldsDiff{
Expand Down Expand Up @@ -291,13 +291,13 @@ func TestCompareModuleSchemas(t *testing.T) {
schema.EnumType{Name: "foo", Values: []schema.EnumValueDefinition{{Name: "a", Value: 1}}},
),
diff: ModuleSchemaDiff{
RemovedObjectTypes: []schema.StateObjectType{
RemovedStateObjectTypes: []schema.StateObjectType{
{
Name: "foo",
KeyFields: []schema.Field{{Name: "key1", Kind: schema.EnumKind, ReferencedType: "bar"}},
},
},
AddedObjectTypes: []schema.StateObjectType{
AddedStateObjectTypes: []schema.StateObjectType{
{
Name: "bar",
KeyFields: []schema.Field{{Name: "key1", Kind: schema.EnumKind, ReferencedType: "foo"}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -39,8 +39,8 @@ type FieldsDiff struct {
NewOrder []string
}

func compareObjectType(oldObj, newObj schema.StateObjectType) ObjectTypeDiff {
diff := ObjectTypeDiff{
func compareObjectType(oldObj, newObj schema.StateObjectType) StateObjectTypeDiff {
diff := StateObjectTypeDiff{
Name: oldObj.TypeName(),
}

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func Test_objectTypeDiff(t *testing.T) {
name string
oldType schema.StateObjectType
newType schema.StateObjectType
diff ObjectTypeDiff
trueF func(ObjectTypeDiff) bool
diff StateObjectTypeDiff
trueF func(StateObjectTypeDiff) bool
hasCompatibleChanges bool
}{
{
Expand All @@ -24,8 +24,8 @@ func Test_objectTypeDiff(t *testing.T) {
newType: schema.StateObjectType{
KeyFields: []schema.Field{{Name: "id", Kind: schema.Int32Kind}},
},
diff: ObjectTypeDiff{},
trueF: ObjectTypeDiff.Empty,
diff: StateObjectTypeDiff{},
trueF: StateObjectTypeDiff.Empty,
hasCompatibleChanges: true,
},
{
Expand All @@ -36,7 +36,7 @@ func Test_objectTypeDiff(t *testing.T) {
newType: schema.StateObjectType{
KeyFields: []schema.Field{{Name: "id", Kind: schema.StringKind}},
},
diff: ObjectTypeDiff{
diff: StateObjectTypeDiff{
KeyFieldsDiff: FieldsDiff{
Changed: []FieldDiff{
{
Expand All @@ -47,7 +47,7 @@ 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,
},
{
Expand All @@ -58,7 +58,7 @@ func Test_objectTypeDiff(t *testing.T) {
newType: schema.StateObjectType{
ValueFields: []schema.Field{{Name: "name", Kind: schema.Int32Kind}},
},
diff: ObjectTypeDiff{
diff: StateObjectTypeDiff{
ValueFieldsDiff: FieldsDiff{
Changed: []FieldDiff{
{
Expand All @@ -69,7 +69,7 @@ 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,
},
{
Expand All @@ -80,12 +80,12 @@ func Test_objectTypeDiff(t *testing.T) {
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,
},
{
Expand All @@ -96,12 +96,12 @@ func Test_objectTypeDiff(t *testing.T) {
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,
},
{
Expand All @@ -114,7 +114,7 @@ func Test_objectTypeDiff(t *testing.T) {
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"},
Expand All @@ -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,
},
}
Expand Down
2 changes: 1 addition & 1 deletion schema/testing/module_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
14 changes: 7 additions & 7 deletions schema/testing/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"cosmossdk.io/schema"
)

// ObjectTypeGen generates random StateObjectType's based on the validity criteria of object types.
func ObjectTypeGen(typeSet schema.TypeSet) *rapid.Generator[schema.StateObjectType] {
// 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
})
Expand Down Expand Up @@ -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.StateObjectType, typeSet schema.TypeSet) *rapid.Generator[schema.StateObjectUpdate] {
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.StateObjectType, state *btree.Map[string, schema.StateObjectUpdate], sch schema.TypeSet) *rapid.Generator[schema.StateObjectUpdate] {
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 {
Expand Down
6 changes: 3 additions & 3 deletions schema/testing/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
4 changes: 2 additions & 2 deletions schema/testing/statesim/object_coll.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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.StateObjectType
Expand All @@ -23,7 +23,7 @@ type ObjectCollection struct {
// NewObjectCollection creates a new ObjectCollection for the given object type.
func NewObjectCollection(objectType schema.StateObjectType, options Options, typeSet schema.TypeSet) *ObjectCollection {
objects := &btree.Map[string, schema.StateObjectUpdate]{}
updateGen := schematesting.ObjectUpdateGen(objectType, objects, typeSet)
updateGen := schematesting.StateObjectUpdateGen(objectType, objects, typeSet)
valueFieldIndices := make(map[string]int, len(objectType.ValueFields))
for i, field := range objectType.ValueFields {
valueFieldIndices[field.Name] = i
Expand Down

0 comments on commit ba32357

Please sign in to comment.