Skip to content

Commit

Permalink
Export the fields of errors related to contract update validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Apr 6, 2021
1 parent 8721f72 commit 51bb585
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 70 deletions.
46 changes: 23 additions & 23 deletions runtime/contract_update_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func (validator *ContractUpdateValidator) checkDeclarationUpdatability(
// - 'structs' and 'enums'
if oldDeclaration.DeclarationKind() != newDeclaration.DeclarationKind() {
validator.report(&InvalidDeclarationKindChangeError{
name: oldDeclaration.DeclarationIdentifier().Identifier,
oldKind: oldDeclaration.DeclarationKind(),
newKind: newDeclaration.DeclarationKind(),
Name: oldDeclaration.DeclarationIdentifier().Identifier,
OldKind: oldDeclaration.DeclarationKind(),
NewKind: newDeclaration.DeclarationKind(),
Range: ast.NewRangeFromPositioned(newDeclaration.DeclarationIdentifier()),
})

Expand Down Expand Up @@ -147,8 +147,8 @@ func (validator *ContractUpdateValidator) checkFields(oldDeclaration ast.Declara
oldField := oldFields[newField.Identifier.Identifier]
if oldField == nil {
validator.report(&ExtraneousFieldError{
declName: newDeclaration.DeclarationIdentifier().Identifier,
fieldName: newField.Identifier.Identifier,
DeclName: newDeclaration.DeclarationIdentifier().Identifier,
FieldName: newField.Identifier.Identifier,
Range: ast.NewRangeFromPositioned(newField.Identifier),
})

Expand All @@ -163,9 +163,9 @@ func (validator *ContractUpdateValidator) checkField(oldField *ast.FieldDeclarat
err := oldField.TypeAnnotation.Type.CheckEqual(newField.TypeAnnotation.Type, validator)
if err != nil {
validator.report(&FieldMismatchError{
declName: validator.currentDecl.DeclarationIdentifier().Identifier,
fieldName: newField.Identifier.Identifier,
err: err,
DeclName: validator.currentDecl.DeclarationIdentifier().Identifier,
FieldName: newField.Identifier.Identifier,
Err: err,
Range: ast.NewRangeFromPositioned(newField.TypeAnnotation),
})
}
Expand Down Expand Up @@ -233,9 +233,9 @@ func (validator *ContractUpdateValidator) checkEnumCases(oldDeclaration ast.Decl

if newEnumCaseCount < oldEnumCaseCount {
validator.report(&MissingEnumCasesError{
declName: newDeclaration.DeclarationIdentifier().Identifier,
expected: oldEnumCaseCount,
found: newEnumCaseCount,
DeclName: newDeclaration.DeclarationIdentifier().Identifier,
Expected: oldEnumCaseCount,
Found: newEnumCaseCount,
Range: ast.NewRangeFromPositioned(newDeclaration.DeclarationIdentifier()),
})

Expand All @@ -244,7 +244,7 @@ func (validator *ContractUpdateValidator) checkEnumCases(oldDeclaration ast.Decl
return
}

// Validate the the new enum cases.
// Check whether the enum cases matches the old enum cases.
for index, newEnumCase := range newEnumCases {
// If there are no more old enum-cases, then these are newly added enum-cases,
// which should be fine.
Expand All @@ -255,8 +255,8 @@ func (validator *ContractUpdateValidator) checkEnumCases(oldDeclaration ast.Decl
oldEnumCase := oldEnumCases[index]
if oldEnumCase.Identifier.Identifier != newEnumCase.Identifier.Identifier {
validator.report(&EnumCaseMismatchError{
expectedName: oldEnumCase.Identifier.Identifier,
foundName: newEnumCase.Identifier.Identifier,
ExpectedName: oldEnumCase.Identifier.Identifier,
FoundName: newEnumCase.Identifier.Identifier,
Range: ast.NewRangeFromPositioned(newEnumCase),
})
}
Expand Down Expand Up @@ -470,8 +470,8 @@ func (validator *ContractUpdateValidator) checkConformances(

if len(oldConformances) != len(newConformances) {
validator.report(&ConformanceCountMismatchError{
expected: len(oldConformances),
found: len(newConformances),
Expected: len(oldConformances),
Found: len(newConformances),
Range: ast.NewRangeFromPositioned(newDecl.Identifier),
})

Expand All @@ -485,8 +485,8 @@ func (validator *ContractUpdateValidator) checkConformances(
err := oldConformance.CheckEqual(newConformance, validator)
if err != nil {
validator.report(&ConformanceMismatchError{
declName: newDecl.Identifier.Identifier,
err: err,
DeclName: newDecl.Identifier.Identifier,
Err: err,
Range: ast.NewRangeFromPositioned(newConformance),
})
}
Expand All @@ -502,16 +502,16 @@ func (validator *ContractUpdateValidator) report(err error) {

func (validator *ContractUpdateValidator) getContractUpdateError() error {
return &ContractUpdateError{
contractName: validator.contractName,
errors: validator.errors,
location: validator.location,
ContractName: validator.contractName,
Errors: validator.errors,
Location: validator.location,
}
}

func getTypeMismatchError(expectedType ast.Type, foundType ast.Type) *TypeMismatchError {
return &TypeMismatchError{
expectedType: expectedType,
foundType: foundType,
ExpectedType: expectedType,
FoundType: foundType,
Range: ast.NewRangeFromPositioned(foundType),
}
}
Expand Down
12 changes: 6 additions & 6 deletions runtime/contract_update_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,11 +1471,11 @@ func assertFieldTypeMismatchError(
fieldMismatchError.Error(),
)

assert.IsType(t, &TypeMismatchError{}, fieldMismatchError.err)
assert.IsType(t, &TypeMismatchError{}, fieldMismatchError.Err)
assert.Equal(
t,
fmt.Sprintf("incompatible type annotations. expected `%s`, found `%s`", expectedType, foundType),
fieldMismatchError.err.Error(),
fieldMismatchError.Err.Error(),
)
}

Expand All @@ -1496,11 +1496,11 @@ func assertConformanceMismatchError(
conformanceMismatchError.Error(),
)

assert.IsType(t, &TypeMismatchError{}, conformanceMismatchError.err)
assert.IsType(t, &TypeMismatchError{}, conformanceMismatchError.Err)
assert.Equal(
t,
fmt.Sprintf("incompatible type annotations. expected `%s`, found `%s`", expectedType, foundType),
conformanceMismatchError.err.Error(),
conformanceMismatchError.Err.Error(),
)
}

Expand All @@ -1520,7 +1520,7 @@ func assertEnumCaseMismatchError(t *testing.T, err error, expectedEnumCase strin
)
}

func assertMissingEnumCasesError(t *testing.T, err error, declName string, expectedCaes int, foundCases int) {
func assertMissingEnumCasesError(t *testing.T, err error, declName string, expectedCases int, foundCases int) {
require.Error(t, err)
require.IsType(t, &MissingEnumCasesError{}, err)
extraFieldError := err.(*MissingEnumCasesError)
Expand All @@ -1529,7 +1529,7 @@ func assertMissingEnumCasesError(t *testing.T, err error, declName string, expec
fmt.Sprintf(
"missing cases in enum `%s`: expected %d or more, found %d",
declName,
expectedCaes,
expectedCases,
foundCases,
),
extraFieldError.Error(),
Expand Down
82 changes: 41 additions & 41 deletions runtime/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,70 +254,70 @@ func (*InvalidContractDeploymentOriginError) Error() string {
// ContractUpdateError is reported upon any invalid update to a contract or contract interface.
// It contains all the errors reported during the update validation.
type ContractUpdateError struct {
contractName string
errors []error
location common.Location
ContractName string
Errors []error
Location common.Location
}

func (e *ContractUpdateError) Error() string {
return fmt.Sprintf("cannot update contract `%s`", e.contractName)
return fmt.Sprintf("cannot update contract `%s`", e.ContractName)
}

func (e *ContractUpdateError) ChildErrors() []error {
return e.errors
return e.Errors
}

func (e *ContractUpdateError) ImportLocation() common.Location {
return e.location
return e.Location
}

// FieldMismatchError is reported during a contract update, when a type of a field
// does not match the existing type of the same field.
type FieldMismatchError struct {
declName string
fieldName string
err error
DeclName string
FieldName string
Err error
ast.Range
}

func (e *FieldMismatchError) Error() string {
return fmt.Sprintf("mismatching field `%s` in `%s`",
e.fieldName,
e.declName,
e.FieldName,
e.DeclName,
)
}

func (e *FieldMismatchError) SecondaryError() string {
return e.err.Error()
return e.Err.Error()
}

// TypeMismatchError is reported during a contract update, when a type of the new program
// does not match the existing type.
type TypeMismatchError struct {
expectedType ast.Type
foundType ast.Type
ExpectedType ast.Type
FoundType ast.Type
ast.Range
}

func (e *TypeMismatchError) Error() string {
return fmt.Sprintf("incompatible type annotations. expected `%s`, found `%s`",
e.expectedType,
e.foundType,
e.ExpectedType,
e.FoundType,
)
}

// ExtraneousFieldError is reported during a contract update, when an updated composite
// declaration has more fields than the existing declaration.
type ExtraneousFieldError struct {
declName string
fieldName string
DeclName string
FieldName string
ast.Range
}

func (e *ExtraneousFieldError) Error() string {
return fmt.Sprintf("found new field `%s` in `%s`",
e.fieldName,
e.declName,
e.FieldName,
e.DeclName,
)
}

Expand All @@ -334,73 +334,73 @@ func (e *ContractNotFoundError) Error() string {
// InvalidDeclarationKindChangeError is reported during a contract update, when an attempt is made
// to convert an existing contract to a contract interface, or vise versa.
type InvalidDeclarationKindChangeError struct {
name string
oldKind common.DeclarationKind
newKind common.DeclarationKind
Name string
OldKind common.DeclarationKind
NewKind common.DeclarationKind
ast.Range
}

func (e *InvalidDeclarationKindChangeError) Error() string {
return fmt.Sprintf("trying to convert %s `%s` to a %s", e.oldKind.Name(), e.name, e.newKind.Name())
return fmt.Sprintf("trying to convert %s `%s` to a %s", e.OldKind.Name(), e.Name, e.NewKind.Name())
}

// ConformanceMismatchError is reported during a contract update, when the enum conformance of the new program
// does not match the existing one.
type ConformanceMismatchError struct {
declName string
err error
DeclName string
Err error
ast.Range
}

func (e *ConformanceMismatchError) Error() string {
return fmt.Sprintf("conformances does not match in `%s`", e.declName)
return fmt.Sprintf("conformances does not match in `%s`", e.DeclName)
}

func (e *ConformanceMismatchError) SecondaryError() string {
return e.err.Error()
return e.Err.Error()
}

// ConformanceCountMismatchError is reported during a contract update, when the conformance count
// does not match the existing conformance count.
type ConformanceCountMismatchError struct {
expected int
found int
Expected int
Found int
ast.Range
}

func (e *ConformanceCountMismatchError) Error() string {
return fmt.Sprintf("conformances count does not match: expected %d, found %d", e.expected, e.found)
return fmt.Sprintf("conformances count does not match: expected %d, found %d", e.Expected, e.Found)
}

// EnumCaseMismatchError is reported during an enum update, when an updated enum case
// does not match the existing enum case.
type EnumCaseMismatchError struct {
expectedName string
foundName string
ExpectedName string
FoundName string
ast.Range
}

func (e *EnumCaseMismatchError) Error() string {
return fmt.Sprintf("mismatching enum case: expected `%s`, found `%s`",
e.expectedName,
e.foundName,
e.ExpectedName,
e.FoundName,
)
}

// MissingEnumCasesError is reported during an enum update, if any enum cases are removed
// from an existing enum.
type MissingEnumCasesError struct {
declName string
expected int
found int
DeclName string
Expected int
Found int
ast.Range
}

func (e *MissingEnumCasesError) Error() string {
return fmt.Sprintf(
"missing cases in enum `%s`: expected %d or more, found %d",
e.declName,
e.expected,
e.found,
e.DeclName,
e.Expected,
e.Found,
)
}

0 comments on commit 51bb585

Please sign in to comment.