2021-01-21 11:56:30 +00:00
|
|
|
package spec
|
|
|
|
|
2021-02-14 04:48:09 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-02-23 12:10:25 +00:00
|
|
|
|
|
|
|
"github.com/sunboyy/repogen/internal/code"
|
2021-02-14 04:48:09 +00:00
|
|
|
)
|
|
|
|
|
2021-01-21 11:56:30 +00:00
|
|
|
// ParsingError is an error from parsing interface methods
|
|
|
|
type ParsingError string
|
|
|
|
|
|
|
|
func (err ParsingError) Error() string {
|
|
|
|
switch err {
|
2021-02-01 14:39:20 +00:00
|
|
|
case UnsupportedReturnError:
|
|
|
|
return "this type of return is not supported"
|
2021-02-14 04:48:09 +00:00
|
|
|
case QueryRequiredError:
|
|
|
|
return "query is required"
|
2021-01-21 11:56:30 +00:00
|
|
|
case InvalidParamError:
|
|
|
|
return "parameters do not match the query"
|
2021-01-27 12:15:25 +00:00
|
|
|
case InvalidUpdateFieldsError:
|
2021-03-31 11:44:31 +00:00
|
|
|
return "update fields are invalid"
|
2021-01-21 11:56:30 +00:00
|
|
|
case ContextParamRequiredError:
|
|
|
|
return "context parameter is required"
|
2021-03-31 11:44:31 +00:00
|
|
|
case PushNonArrayError:
|
|
|
|
return "cannot use push operation in a non-array type"
|
2021-01-21 11:56:30 +00:00
|
|
|
}
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parsing error constants
|
|
|
|
const (
|
2021-02-01 14:39:20 +00:00
|
|
|
UnsupportedReturnError ParsingError = "ERROR_UNSUPPORTED_RETURN"
|
2021-02-14 04:48:09 +00:00
|
|
|
QueryRequiredError ParsingError = "ERROR_QUERY_REQUIRED"
|
2021-01-21 11:56:30 +00:00
|
|
|
InvalidParamError ParsingError = "ERROR_INVALID_PARAM"
|
2021-01-27 12:15:25 +00:00
|
|
|
InvalidUpdateFieldsError ParsingError = "ERROR_INVALID_UPDATE_FIELDS"
|
2021-01-21 11:56:30 +00:00
|
|
|
ContextParamRequiredError ParsingError = "ERROR_CONTEXT_PARAM_REQUIRED"
|
2021-03-31 11:44:31 +00:00
|
|
|
PushNonArrayError ParsingError = "ERROR_PUSH_NON_ARRAY"
|
2021-01-21 11:56:30 +00:00
|
|
|
)
|
2021-02-14 04:48:09 +00:00
|
|
|
|
|
|
|
// NewInvalidQueryError creates invalidQueryError
|
|
|
|
func NewInvalidQueryError(queryTokens []string) error {
|
|
|
|
return invalidQueryError{QueryString: strings.Join(queryTokens, "")}
|
|
|
|
}
|
|
|
|
|
|
|
|
type invalidQueryError struct {
|
|
|
|
QueryString string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err invalidQueryError) Error() string {
|
|
|
|
return fmt.Sprintf("invalid query '%s'", err.QueryString)
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
// NewInvalidSortError creates invalidSortError
|
|
|
|
func NewInvalidSortError(sortTokens []string) error {
|
|
|
|
return invalidSortError{SortString: strings.Join(sortTokens, "")}
|
|
|
|
}
|
|
|
|
|
|
|
|
type invalidSortError struct {
|
|
|
|
SortString string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err invalidSortError) Error() string {
|
|
|
|
return fmt.Sprintf("invalid sort '%s'", err.SortString)
|
|
|
|
}
|
|
|
|
|
2021-02-14 04:48:09 +00:00
|
|
|
// NewUnknownOperationError creates unknownOperationError
|
|
|
|
func NewUnknownOperationError(operationName string) error {
|
|
|
|
return unknownOperationError{OperationName: operationName}
|
|
|
|
}
|
|
|
|
|
|
|
|
type unknownOperationError struct {
|
|
|
|
OperationName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err unknownOperationError) Error() string {
|
|
|
|
return fmt.Sprintf("unknown operation '%s'", err.OperationName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStructFieldNotFoundError creates structFieldNotFoundError
|
2021-03-03 14:01:32 +00:00
|
|
|
func NewStructFieldNotFoundError(tokens []string) error {
|
|
|
|
return structFieldNotFoundError{FieldName: strings.Join(tokens, "")}
|
2021-02-14 04:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type structFieldNotFoundError struct {
|
|
|
|
FieldName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err structFieldNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("struct field '%s' not found", err.FieldName)
|
|
|
|
}
|
2021-02-23 12:10:25 +00:00
|
|
|
|
|
|
|
// NewIncompatibleComparatorError creates incompatibleComparatorError
|
|
|
|
func NewIncompatibleComparatorError(comparator Comparator, field code.StructField) error {
|
|
|
|
return incompatibleComparatorError{
|
|
|
|
Comparator: comparator,
|
|
|
|
Field: field,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type incompatibleComparatorError struct {
|
|
|
|
Comparator Comparator
|
|
|
|
Field code.StructField
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err incompatibleComparatorError) Error() string {
|
|
|
|
return fmt.Sprintf("cannot use comparator %s with struct field '%s' of type '%s'",
|
|
|
|
err.Comparator, err.Field.Name, err.Field.Type.Code())
|
|
|
|
}
|