Skip to content

Commit

Permalink
refactor(schema)!: rename Schema -> TypeSet and other small renamings (
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored Aug 30, 2024
1 parent b33ed07 commit ce8f9d4
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 89 deletions.
2 changes: 1 addition & 1 deletion indexer/postgres/internal/testdata/example_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
AllKindsObject.ValueFields = append(AllKindsObject.ValueFields, field)
}

ExampleSchema = schema.MustNewModuleSchema(
ExampleSchema = schema.MustCompileModuleSchema(
AllKindsObject,
SingletonObject,
VoteObject,
Expand Down
4 changes: 2 additions & 2 deletions schema/decoding/decoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (e exampleBankModule) subBalance(acct, denom string, amount uint64) error {

func init() {
var err error
exampleBankSchema, err = schema.NewModuleSchema(schema.ObjectType{
exampleBankSchema, err = schema.CompileModuleSchema(schema.ObjectType{
Name: "balances",
KeyFields: []schema.Field{
{
Expand Down Expand Up @@ -435,7 +435,7 @@ type oneValueModule struct {

func init() {
var err error
oneValueModSchema, err = schema.NewModuleSchema(schema.ObjectType{
oneValueModSchema, err = schema.CompileModuleSchema(schema.ObjectType{
Name: "item",
ValueFields: []schema.Field{
{Name: "value", Kind: schema.StringKind},
Expand Down
6 changes: 3 additions & 3 deletions schema/decoding/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type modA struct{}

func (m modA) ModuleCodec() (schema.ModuleCodec, error) {
modSchema, err := schema.NewModuleSchema(schema.ObjectType{Name: "A", KeyFields: []schema.Field{{Name: "field1", Kind: schema.StringKind}}})
modSchema, err := schema.CompileModuleSchema(schema.ObjectType{Name: "A", KeyFields: []schema.Field{{Name: "field1", Kind: schema.StringKind}}})
if err != nil {
return schema.ModuleCodec{}, err
}
Expand All @@ -22,7 +22,7 @@ func (m modA) ModuleCodec() (schema.ModuleCodec, error) {
type modB struct{}

func (m modB) ModuleCodec() (schema.ModuleCodec, error) {
modSchema, err := schema.NewModuleSchema(schema.ObjectType{Name: "B", KeyFields: []schema.Field{{Name: "field2", Kind: schema.StringKind}}})
modSchema, err := schema.CompileModuleSchema(schema.ObjectType{Name: "B", KeyFields: []schema.Field{{Name: "field2", Kind: schema.StringKind}}})
if err != nil {
return schema.ModuleCodec{}, err
}
Expand All @@ -44,7 +44,7 @@ var testResolver = ModuleSetDecoderResolver(moduleSet)
func TestModuleSetDecoderResolver_IterateAll(t *testing.T) {
objectTypes := map[string]bool{}
err := testResolver.IterateAll(func(moduleName string, cdc schema.ModuleCodec) error {
cdc.Schema.Types(func(t schema.Type) bool {
cdc.Schema.AllTypes(func(t schema.Type) bool {
objTyp, ok := t.(schema.ObjectType)
if ok {
objectTypes[objTyp.Name] = true
Expand Down
2 changes: 1 addition & 1 deletion schema/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func TestCompareModuleSchemas(t *testing.T) {
}

func requireModuleSchema(t *testing.T, types ...schema.Type) schema.ModuleSchema {
s, err := schema.NewModuleSchema(types...)
s, err := schema.CompileModuleSchema(types...)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion schema/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (e EnumType) TypeName() string {
func (EnumType) isType() {}

// Validate validates the enum definition.
func (e EnumType) Validate(Schema) error {
func (e EnumType) Validate(TypeSet) error {
if !ValidateName(e.Name) {
return fmt.Errorf("invalid enum definition name %q", e.Name)
}
Expand Down
2 changes: 1 addition & 1 deletion schema/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestEnumDefinition_Validate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.enum.Validate(EmptySchema{})
err := tt.enum.Validate(EmptyTypeSet())
if tt.errContains == "" {
if err != nil {
t.Errorf("expected valid enum definition to pass validation, got: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Field struct {
}

// Validate validates the field.
func (c Field) Validate(schema Schema) error {
func (c Field) Validate(typeSet TypeSet) error {
// valid name
if !ValidateName(c.Name) {
return fmt.Errorf("invalid field name %q", c.Name)
Expand All @@ -38,7 +38,7 @@ func (c Field) Validate(schema Schema) error {
return fmt.Errorf("enum field %q must have a referenced type", c.Name)
}

ty, ok := schema.LookupType(c.ReferencedType)
ty, ok := typeSet.LookupType(c.ReferencedType)
if !ok {
return fmt.Errorf("enum field %q references unknown type %q", c.Name, c.ReferencedType)
}
Expand All @@ -58,7 +58,7 @@ func (c Field) Validate(schema Schema) error {
// ValidateValue validates that the value conforms to the field's kind and nullability.
// Unlike Kind.ValidateValue, it also checks that the value conforms to the EnumType
// if the field is an EnumKind.
func (c Field) ValidateValue(value interface{}, schema Schema) error {
func (c Field) ValidateValue(value interface{}, typeSet TypeSet) error {
if value == nil {
if !c.Nullable {
return fmt.Errorf("field %q cannot be null", c.Name)
Expand All @@ -72,7 +72,7 @@ func (c Field) ValidateValue(value interface{}, schema Schema) error {

switch c.Kind {
case EnumKind:
ty, ok := schema.LookupType(c.ReferencedType)
ty, ok := typeSet.LookupType(c.ReferencedType)
if !ok {
return fmt.Errorf("enum field %q references unknown type %q", c.Name, c.ReferencedType)
}
Expand Down
2 changes: 1 addition & 1 deletion schema/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestFieldJSON(t *testing.T) {
}
}

var testEnumSchema = MustNewModuleSchema(EnumType{
var testEnumSchema = MustCompileModuleSchema(EnumType{
Name: "enum",
Values: []EnumValueDefinition{{Name: "a", Value: 1}, {Name: "b", Value: 2}},
})
16 changes: 8 additions & 8 deletions schema/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ 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.
func ValidateObjectKey(keyFields []Field, value interface{}, schema Schema) error {
return validateFieldsValue(keyFields, value, schema)
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.
func ValidateObjectValue(valueFields []Field, value interface{}, schema Schema) error {
func ValidateObjectValue(valueFields []Field, value interface{}, typeSet TypeSet) error {
valueUpdates, ok := value.(ValueUpdates)
if !ok {
return validateFieldsValue(valueFields, value, schema)
return validateFieldsValue(valueFields, value, typeSet)
}

values := map[string]interface{}{}
Expand All @@ -31,7 +31,7 @@ func ValidateObjectValue(valueFields []Field, value interface{}, schema Schema)
continue
}

if err := field.ValidateValue(v, schema); err != nil {
if err := field.ValidateValue(v, typeSet); err != nil {
return err
}

Expand All @@ -45,13 +45,13 @@ func ValidateObjectValue(valueFields []Field, value interface{}, schema Schema)
return nil
}

func validateFieldsValue(fields []Field, value interface{}, schema Schema) error {
func validateFieldsValue(fields []Field, value interface{}, typeSet TypeSet) error {
if len(fields) == 0 {
return nil
}

if len(fields) == 1 {
return fields[0].ValidateValue(value, schema)
return fields[0].ValidateValue(value, typeSet)
}

values, ok := value.([]interface{})
Expand All @@ -63,7 +63,7 @@ func validateFieldsValue(fields []Field, value interface{}, schema Schema) error
return fmt.Errorf("expected %d key fields, got %d values", len(fields), len(value.([]interface{})))
}
for i, field := range fields {
if err := field.ValidateValue(values[i], schema); err != nil {
if err := field.ValidateValue(values[i], typeSet); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions schema/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestValidateForKeyFields(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateObjectKey(tt.keyFields, tt.key, EmptySchema{})
err := ValidateObjectKey(tt.keyFields, tt.key, EmptyTypeSet())
if tt.errContains == "" {
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestValidateForValueFields(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateObjectValue(tt.valueFields, tt.value, EmptySchema{})
err := ValidateObjectValue(tt.valueFields, tt.value, EmptyTypeSet())
if tt.errContains == "" {
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down
22 changes: 12 additions & 10 deletions schema/module_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type ModuleSchema struct {
types map[string]Type
}

// NewModuleSchema constructs a new ModuleSchema and validates it. Any module schema returned without an error
// is guaranteed to be valid.
func NewModuleSchema(types ...Type) (ModuleSchema, error) {
// CompileModuleSchema compiles the types into a ModuleSchema and validates it.
// Any module schema returned without an error is guaranteed to be valid.
func CompileModuleSchema(types ...Type) (ModuleSchema, error) {
typeMap := map[string]Type{}

for _, typ := range types {
Expand All @@ -34,10 +34,10 @@ func NewModuleSchema(types ...Type) (ModuleSchema, error) {
return res, nil
}

// MustNewModuleSchema constructs a new ModuleSchema and panics if it is invalid.
// MustCompileModuleSchema constructs a new ModuleSchema and panics if it is invalid.
// This should only be used in test code or static initialization where it is safe to panic!
func MustNewModuleSchema(types ...Type) ModuleSchema {
sch, err := NewModuleSchema(types...)
func MustCompileModuleSchema(types ...Type) ModuleSchema {
sch, err := CompileModuleSchema(types...)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (s ModuleSchema) LookupType(name string) (Type, bool) {

// Types calls the provided function for each type in the module schema and stops if the function returns false.
// The types are iterated over in sorted order by name. This function is compatible with go 1.23 iterators.
func (s ModuleSchema) Types(f func(Type) bool) {
func (s ModuleSchema) AllTypes(f func(Type) bool) {
keys := make([]string, 0, len(s.types))
for k := range s.types {
keys = append(keys, k)
Expand All @@ -94,7 +94,7 @@ func (s ModuleSchema) Types(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) {
s.Types(func(t Type) bool {
s.AllTypes(func(t Type) bool {
objTyp, ok := t.(ObjectType)
if ok {
return f(objTyp)
Expand All @@ -105,7 +105,7 @@ func (s ModuleSchema) ObjectTypes(f func(ObjectType) bool) {

// EnumTypes iterators over all the enum types in the schema in alphabetical order.
func (s ModuleSchema) EnumTypes(f func(EnumType) bool) {
s.Types(func(t Type) bool {
s.AllTypes(func(t Type) bool {
enumType, ok := t.(EnumType)
if ok {
return f(enumType)
Expand Down Expand Up @@ -169,4 +169,6 @@ func (s *ModuleSchema) UnmarshalJSON(data []byte) error {
return nil
}

var _ Schema = ModuleSchema{}
func (ModuleSchema) isTypeSet() {}

var _ TypeSet = ModuleSchema{}
10 changes: 5 additions & 5 deletions schema/module_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestModuleSchema_Validate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// because validate is called when calling NewModuleSchema, we just call NewModuleSchema
_, err := NewModuleSchema(tt.types...)
// because validate is called when calling CompileModuleSchema, we just call CompileModuleSchema
_, err := CompileModuleSchema(tt.types...)
if tt.errContains == "" {
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestModuleSchema_ValidateObjectUpdate(t *testing.T) {

func requireModuleSchema(t *testing.T, types ...Type) ModuleSchema {
t.Helper()
moduleSchema, err := NewModuleSchema(types...)
moduleSchema, err := CompileModuleSchema(types...)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestModuleSchema_Types(t *testing.T) {
moduleSchema := exampleSchema(t)

var typeNames []string
moduleSchema.Types(func(typ Type) bool {
moduleSchema.AllTypes(func(typ Type) bool {
typeNames = append(typeNames, typ.TypeName())
return true
})
Expand All @@ -252,7 +252,7 @@ func TestModuleSchema_Types(t *testing.T) {

typeNames = nil
// scan just the first type and return false
moduleSchema.Types(func(typ Type) bool {
moduleSchema.AllTypes(func(typ Type) bool {
typeNames = append(typeNames, typ.TypeName())
return false
})
Expand Down
12 changes: 6 additions & 6 deletions schema/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func (o ObjectType) TypeName() string {
func (ObjectType) isType() {}

// Validate validates the object type.
func (o ObjectType) Validate(schema Schema) error {
func (o ObjectType) Validate(typeSet TypeSet) error {
if !ValidateName(o.Name) {
return fmt.Errorf("invalid object type name %q", o.Name)
}

fieldNames := map[string]bool{}

for _, field := range o.KeyFields {
if err := field.Validate(schema); err != nil {
if err := field.Validate(typeSet); err != nil {
return fmt.Errorf("invalid key field %q: %v", field.Name, err) //nolint:errorlint // false positive due to using go1.12
}

Expand All @@ -64,7 +64,7 @@ func (o ObjectType) Validate(schema Schema) error {
}

for _, field := range o.ValueFields {
if err := field.Validate(schema); err != nil {
if err := field.Validate(typeSet); err != nil {
return fmt.Errorf("invalid value field %q: %v", field.Name, err) //nolint:errorlint // false positive due to using go1.12
}

Expand All @@ -82,18 +82,18 @@ func (o ObjectType) Validate(schema Schema) error {
}

// ValidateObjectUpdate validates that the update conforms to the object type.
func (o ObjectType) ValidateObjectUpdate(update ObjectUpdate, schema Schema) error {
func (o ObjectType) ValidateObjectUpdate(update ObjectUpdate, 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)
}

if err := ValidateObjectKey(o.KeyFields, update.Key, schema); err != nil {
if err := ValidateObjectKey(o.KeyFields, update.Key, typeSet); err != nil {
return fmt.Errorf("invalid key for object type %q: %v", update.TypeName, err) //nolint:errorlint // false positive due to using go1.12
}

if update.Delete {
return nil
}

return ValidateObjectValue(o.ValueFields, update.Value, schema)
return ValidateObjectValue(o.ValueFields, update.Value, typeSet)
}
4 changes: 2 additions & 2 deletions schema/object_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestObjectType_Validate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.objectType.Validate(EmptySchema{})
err := tt.objectType.Validate(EmptyTypeSet())
if tt.errContains == "" {
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestObjectType_ValidateObjectUpdate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.objectType.ValidateObjectUpdate(tt.object, EmptySchema{})
err := tt.objectType.ValidateObjectUpdate(tt.object, EmptyTypeSet())
if tt.errContains == "" {
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion schema/testing/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import (
func TestEnumType(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
enumType := EnumType().Draw(t, "enum")
require.NoError(t, enumType.Validate(schema.EmptySchema{}))
require.NoError(t, enumType.Validate(schema.EmptyTypeSet()))
})
}
4 changes: 2 additions & 2 deletions schema/testing/example_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// that can be used in reproducible unit testing and property based testing.
var ExampleAppSchema = map[string]schema.ModuleSchema{
"all_kinds": mkAllKindsModule(),
"test_cases": schema.MustNewModuleSchema(
"test_cases": schema.MustCompileModuleSchema(
schema.ObjectType{
Name: "Singleton",
KeyFields: []schema.Field{},
Expand Down Expand Up @@ -138,7 +138,7 @@ func mkAllKindsModule() schema.ModuleSchema {
types = append(types, typ)
}

return schema.MustNewModuleSchema(types...)
return schema.MustCompileModuleSchema(types...)
}

func mkTestObjectType(kind schema.Kind) schema.ObjectType {
Expand Down
Loading

0 comments on commit ce8f9d4

Please sign in to comment.