2021-01-21 11:56:30 +00:00
|
|
|
package spec
|
|
|
|
|
2021-02-14 04:48:09 +00:00
|
|
|
import (
|
2022-11-09 04:21:11 +00:00
|
|
|
"errors"
|
2021-02-14 04:48:09 +00:00
|
|
|
"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
|
|
|
// parsing error constants
|
2022-11-09 04:21:11 +00:00
|
|
|
var (
|
|
|
|
ErrQueryRequired = errors.New("spec: query is required")
|
|
|
|
ErrInvalidParam = errors.New("spec: parameters do not match the query")
|
|
|
|
ErrInvalidUpdateFields = errors.New("spec: update fields are invalid")
|
|
|
|
ErrContextParamRequired = errors.New("spec: context parameter is required")
|
2021-01-21 11:56:30 +00:00
|
|
|
)
|
2021-02-14 04:48:09 +00:00
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
// NewUnsupportedReturnError creates unsupportedReturnError
|
|
|
|
func NewUnsupportedReturnError(givenType code.Type, index int) error {
|
|
|
|
return unsupportedReturnError{
|
|
|
|
GivenType: givenType,
|
|
|
|
Index: index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type unsupportedReturnError struct {
|
|
|
|
GivenType code.Type
|
|
|
|
Index int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err unsupportedReturnError) Error() string {
|
|
|
|
return fmt.Sprintf("return type '%s' at index %d is not supported", err.GivenType.Code(), err.Index)
|
|
|
|
}
|
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
// NewOperationReturnCountUnmatchedError creates
|
|
|
|
// operationReturnCountUnmatchedError.
|
2021-06-02 11:39:15 +00:00
|
|
|
func NewOperationReturnCountUnmatchedError(returnCount int) error {
|
|
|
|
return operationReturnCountUnmatchedError{
|
|
|
|
ReturnCount: returnCount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type operationReturnCountUnmatchedError struct {
|
|
|
|
ReturnCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err operationReturnCountUnmatchedError) Error() string {
|
|
|
|
return fmt.Sprintf("operation requires return count of %d", err.ReturnCount)
|
|
|
|
}
|
|
|
|
|
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-05-03 07:12:58 +00:00
|
|
|
// NewArgumentTypeNotMatchedError creates argumentTypeNotMatchedError
|
|
|
|
func NewArgumentTypeNotMatchedError(fieldName string, requiredType code.Type, givenType code.Type) error {
|
|
|
|
return argumentTypeNotMatchedError{
|
|
|
|
FieldName: fieldName,
|
|
|
|
RequiredType: requiredType,
|
|
|
|
GivenType: givenType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type argumentTypeNotMatchedError struct {
|
|
|
|
FieldName string
|
|
|
|
RequiredType code.Type
|
|
|
|
GivenType code.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err argumentTypeNotMatchedError) Error() string {
|
|
|
|
return fmt.Sprintf("field '%s' requires an argument of type '%s' (got '%s')",
|
|
|
|
err.FieldName, err.RequiredType.Code(), err.GivenType.Code())
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
2021-05-03 07:12:58 +00:00
|
|
|
|
|
|
|
// NewIncompatibleUpdateOperatorError creates incompatibleUpdateOperatorError
|
|
|
|
func NewIncompatibleUpdateOperatorError(updateOperator UpdateOperator, fieldReference FieldReference) error {
|
|
|
|
return incompatibleUpdateOperatorError{
|
|
|
|
UpdateOperator: updateOperator,
|
|
|
|
ReferencingCode: fieldReference.ReferencingCode(),
|
|
|
|
ReferencedType: fieldReference.ReferencedField().Type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type incompatibleUpdateOperatorError struct {
|
|
|
|
UpdateOperator UpdateOperator
|
|
|
|
ReferencingCode string
|
|
|
|
ReferencedType code.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err incompatibleUpdateOperatorError) Error() string {
|
|
|
|
return fmt.Sprintf("cannot use update operator %s with struct field '%s' of type '%s'",
|
|
|
|
err.UpdateOperator, err.ReferencingCode, err.ReferencedType.Code())
|
|
|
|
}
|