Enforce new linting rules: errname, errorlint, lll, stylecheck (#32)
This commit is contained in:
parent
ec08a5a918
commit
482dd095a6
16 changed files with 891 additions and 336 deletions
|
@ -1,35 +1,19 @@
|
|||
package spec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/sunboyy/repogen/internal/code"
|
||||
)
|
||||
|
||||
// ParsingError is an error from parsing interface methods
|
||||
type ParsingError string
|
||||
|
||||
func (err ParsingError) Error() string {
|
||||
switch err {
|
||||
case QueryRequiredError:
|
||||
return "query is required"
|
||||
case InvalidParamError:
|
||||
return "parameters do not match the query"
|
||||
case InvalidUpdateFieldsError:
|
||||
return "update fields are invalid"
|
||||
case ContextParamRequiredError:
|
||||
return "context parameter is required"
|
||||
}
|
||||
return string(err)
|
||||
}
|
||||
|
||||
// parsing error constants
|
||||
const (
|
||||
QueryRequiredError ParsingError = "ERROR_QUERY_REQUIRED"
|
||||
InvalidParamError ParsingError = "ERROR_INVALID_PARAM"
|
||||
InvalidUpdateFieldsError ParsingError = "ERROR_INVALID_UPDATE_FIELDS"
|
||||
ContextParamRequiredError ParsingError = "ERROR_CONTEXT_PARAM_REQUIRED"
|
||||
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")
|
||||
)
|
||||
|
||||
// NewUnsupportedReturnError creates unsupportedReturnError
|
||||
|
@ -49,7 +33,8 @@ func (err unsupportedReturnError) Error() string {
|
|||
return fmt.Sprintf("return type '%s' at index %d is not supported", err.GivenType.Code(), err.Index)
|
||||
}
|
||||
|
||||
// NewOperationReturnCountUnmatchedError creates operationReturnCountUnmatchedError
|
||||
// NewOperationReturnCountUnmatchedError creates
|
||||
// operationReturnCountUnmatchedError.
|
||||
func NewOperationReturnCountUnmatchedError(returnCount int) error {
|
||||
return operationReturnCountUnmatchedError{
|
||||
ReturnCount: returnCount,
|
||||
|
|
|
@ -14,7 +14,8 @@ func (r FieldReference) ReferencedField() code.StructField {
|
|||
return r[len(r)-1]
|
||||
}
|
||||
|
||||
// ReferencingCode returns a string containing name of the referenced fields concatenating with period (.).
|
||||
// ReferencingCode returns a string containing name of the referenced fields
|
||||
// concatenating with period (.).
|
||||
func (r FieldReference) ReferencingCode() string {
|
||||
var fieldNames []string
|
||||
for _, field := range r {
|
||||
|
|
|
@ -5,8 +5,11 @@ import (
|
|||
"github.com/sunboyy/repogen/internal/code"
|
||||
)
|
||||
|
||||
// ParseInterfaceMethod returns repository method spec from declared interface method
|
||||
func ParseInterfaceMethod(structs map[string]code.Struct, structModel code.Struct, method code.Method) (MethodSpec, error) {
|
||||
// ParseInterfaceMethod returns repository method spec from declared interface
|
||||
// method.
|
||||
func ParseInterfaceMethod(structs map[string]code.Struct, structModel code.Struct,
|
||||
method code.Method) (MethodSpec, error) {
|
||||
|
||||
parser := interfaceMethodParser{
|
||||
fieldResolver: fieldResolver{
|
||||
Structs: structs,
|
||||
|
@ -67,12 +70,12 @@ func (p interfaceMethodParser) parseInsertOperation(tokens []string) (Operation,
|
|||
|
||||
pointerType := code.PointerType{ContainedType: p.StructModel.ReferencedType()}
|
||||
if mode == QueryModeOne && p.Method.Params[1].Type != pointerType {
|
||||
return nil, InvalidParamError
|
||||
return nil, ErrInvalidParam
|
||||
}
|
||||
|
||||
arrayType := code.ArrayType{ContainedType: pointerType}
|
||||
if mode == QueryModeMany && p.Method.Params[1].Type != arrayType {
|
||||
return nil, InvalidParamError
|
||||
return nil, ErrInvalidParam
|
||||
}
|
||||
|
||||
return InsertOperation{
|
||||
|
@ -354,14 +357,14 @@ func (p interfaceMethodParser) extractIntOrBoolReturns(returns []code.Type) (Que
|
|||
func (p interfaceMethodParser) validateContextParam() error {
|
||||
contextType := code.ExternalType{PackageAlias: "context", Name: "Context"}
|
||||
if len(p.Method.Params) == 0 || p.Method.Params[0].Type != contextType {
|
||||
return ContextParamRequiredError
|
||||
return ErrContextParamRequired
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p interfaceMethodParser) validateQueryFromParams(params []code.Param, querySpec QuerySpec) error {
|
||||
if querySpec.NumberOfArguments() != len(params) {
|
||||
return InvalidParamError
|
||||
return ErrInvalidParam
|
||||
}
|
||||
|
||||
var currentParamIndex int
|
||||
|
@ -373,7 +376,9 @@ func (p interfaceMethodParser) validateQueryFromParams(params []code.Param, quer
|
|||
}
|
||||
|
||||
for i := 0; i < predicate.Comparator.NumberOfArguments(); i++ {
|
||||
requiredType := predicate.Comparator.ArgumentTypeFromFieldType(predicate.FieldReference.ReferencedField().Type)
|
||||
requiredType := predicate.Comparator.ArgumentTypeFromFieldType(
|
||||
predicate.FieldReference.ReferencedField().Type,
|
||||
)
|
||||
|
||||
if params[currentParamIndex].Type != requiredType {
|
||||
return NewArgumentTypeNotMatchedError(predicate.FieldReference.ReferencingCode(), requiredType,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package spec_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
|
@ -126,8 +127,19 @@ func TestParseInterfaceMethod_Insert(t *testing.T) {
|
|||
Method: code.Method{
|
||||
Name: "InsertMany",
|
||||
Params: []code.Param{
|
||||
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||
{Type: code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}}},
|
||||
{
|
||||
Type: code.ExternalType{
|
||||
PackageAlias: "context",
|
||||
Name: "Context",
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: code.ArrayType{
|
||||
ContainedType: code.PointerType{
|
||||
ContainedType: code.SimpleType("UserModel"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Returns: []code.Type{
|
||||
code.ArrayType{ContainedType: code.InterfaceType{}},
|
||||
|
@ -198,7 +210,11 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
ExpectedOperation: spec.FindOperation{
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{phoneNumberField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{phoneNumberField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -238,7 +254,11 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
ExpectedOperation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{nameField, firstNameField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{nameField, firstNameField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -258,7 +278,11 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
ExpectedOperation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{referrerField, idField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{referrerField, idField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -297,8 +321,16 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
Query: spec.QuerySpec{
|
||||
Operator: spec.OperatorAnd,
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{cityField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{FieldReference: spec.FieldReference{genderField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{cityField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -322,8 +354,16 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
Query: spec.QuerySpec{
|
||||
Operator: spec.OperatorOr,
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{cityField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{FieldReference: spec.FieldReference{genderField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{cityField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -384,7 +424,11 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
ExpectedOperation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorLessThanEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -404,7 +448,11 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
ExpectedOperation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorGreaterThan, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorGreaterThan,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -424,7 +472,11 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
ExpectedOperation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorGreaterThanEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -523,7 +575,11 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
|||
ExpectedOperation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{enabledField}, Comparator: spec.ComparatorFalse, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{enabledField},
|
||||
Comparator: spec.ComparatorFalse,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -705,11 +761,19 @@ func TestParseInterfaceMethod_Update(t *testing.T) {
|
|||
},
|
||||
ExpectedOperation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{genderField}, ParamIndex: 1, Operator: spec.UpdateOperatorSet},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
ParamIndex: 1,
|
||||
Operator: spec.UpdateOperatorSet,
|
||||
},
|
||||
},
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{idField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{idField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -729,11 +793,19 @@ func TestParseInterfaceMethod_Update(t *testing.T) {
|
|||
},
|
||||
ExpectedOperation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{genderField}, ParamIndex: 1, Operator: spec.UpdateOperatorSet},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
ParamIndex: 1,
|
||||
Operator: spec.UpdateOperatorSet,
|
||||
},
|
||||
},
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{idField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{idField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -753,11 +825,19 @@ func TestParseInterfaceMethod_Update(t *testing.T) {
|
|||
},
|
||||
ExpectedOperation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{nameField, firstNameField}, ParamIndex: 1, Operator: spec.UpdateOperatorSet},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{nameField, firstNameField},
|
||||
ParamIndex: 1,
|
||||
Operator: spec.UpdateOperatorSet,
|
||||
},
|
||||
},
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{idField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{idField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -778,8 +858,16 @@ func TestParseInterfaceMethod_Update(t *testing.T) {
|
|||
},
|
||||
ExpectedOperation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{genderField}, ParamIndex: 1, Operator: spec.UpdateOperatorSet},
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{cityField}, ParamIndex: 2, Operator: spec.UpdateOperatorSet},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
ParamIndex: 1,
|
||||
Operator: spec.UpdateOperatorSet,
|
||||
},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{cityField},
|
||||
ParamIndex: 2,
|
||||
Operator: spec.UpdateOperatorSet,
|
||||
},
|
||||
},
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
|
@ -803,11 +891,19 @@ func TestParseInterfaceMethod_Update(t *testing.T) {
|
|||
},
|
||||
ExpectedOperation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{consentHistoryField}, ParamIndex: 1, Operator: spec.UpdateOperatorPush},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{consentHistoryField},
|
||||
ParamIndex: 1,
|
||||
Operator: spec.UpdateOperatorPush,
|
||||
},
|
||||
},
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{idField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{idField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -827,11 +923,19 @@ func TestParseInterfaceMethod_Update(t *testing.T) {
|
|||
},
|
||||
ExpectedOperation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{ageField}, ParamIndex: 1, Operator: spec.UpdateOperatorInc},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
ParamIndex: 1,
|
||||
Operator: spec.UpdateOperatorInc,
|
||||
},
|
||||
},
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{idField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{idField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -852,8 +956,16 @@ func TestParseInterfaceMethod_Update(t *testing.T) {
|
|||
},
|
||||
ExpectedOperation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{enabledField}, ParamIndex: 1, Operator: spec.UpdateOperatorSet},
|
||||
spec.UpdateField{FieldReference: spec.FieldReference{consentHistoryField}, ParamIndex: 2, Operator: spec.UpdateOperatorPush},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{enabledField},
|
||||
ParamIndex: 1,
|
||||
Operator: spec.UpdateOperatorSet,
|
||||
},
|
||||
spec.UpdateField{
|
||||
FieldReference: spec.FieldReference{consentHistoryField},
|
||||
ParamIndex: 2,
|
||||
Operator: spec.UpdateOperatorPush,
|
||||
},
|
||||
},
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
|
@ -921,7 +1033,11 @@ func TestParseInterfaceMethod_Delete(t *testing.T) {
|
|||
ExpectedOperation: spec.DeleteOperation{
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{phoneNumberField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{phoneNumberField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -980,8 +1096,16 @@ func TestParseInterfaceMethod_Delete(t *testing.T) {
|
|||
Query: spec.QuerySpec{
|
||||
Operator: spec.OperatorAnd,
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{cityField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{FieldReference: spec.FieldReference{genderField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{cityField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -1005,8 +1129,16 @@ func TestParseInterfaceMethod_Delete(t *testing.T) {
|
|||
Query: spec.QuerySpec{
|
||||
Operator: spec.OperatorOr,
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{cityField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{FieldReference: spec.FieldReference{genderField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{cityField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -1067,7 +1199,11 @@ func TestParseInterfaceMethod_Delete(t *testing.T) {
|
|||
ExpectedOperation: spec.DeleteOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorLessThanEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -1087,7 +1223,11 @@ func TestParseInterfaceMethod_Delete(t *testing.T) {
|
|||
ExpectedOperation: spec.DeleteOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorGreaterThan, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorGreaterThan,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -1107,7 +1247,11 @@ func TestParseInterfaceMethod_Delete(t *testing.T) {
|
|||
ExpectedOperation: spec.DeleteOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorGreaterThanEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
|
@ -1208,7 +1352,11 @@ func TestParseInterfaceMethod_Count(t *testing.T) {
|
|||
ExpectedOperation: spec.CountOperation{
|
||||
Query: spec.QuerySpec{
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{genderField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -1247,7 +1395,7 @@ func TestParseInterfaceMethod_Invalid(t *testing.T) {
|
|||
})
|
||||
|
||||
expectedError := spec.NewUnknownOperationError("Search")
|
||||
if err != expectedError {
|
||||
if !errors.Is(err, expectedError) {
|
||||
t.Errorf("\nExpected = %+v\nReceived = %+v", expectedError, err)
|
||||
}
|
||||
}
|
||||
|
@ -1275,7 +1423,10 @@ func TestParseInterfaceMethod_Insert_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.NewUnsupportedReturnError(code.PointerType{ContainedType: code.SimpleType("UserModel")}, 0),
|
||||
ExpectedError: spec.NewUnsupportedReturnError(
|
||||
code.PointerType{ContainedType: code.SimpleType("UserModel")},
|
||||
0,
|
||||
),
|
||||
},
|
||||
{
|
||||
Name: "unempty interface return from insert method",
|
||||
|
@ -1315,22 +1466,32 @@ func TestParseInterfaceMethod_Insert_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.ContextParamRequiredError,
|
||||
ExpectedError: spec.ErrContextParamRequired,
|
||||
},
|
||||
{
|
||||
Name: "mismatched model parameter for one mode",
|
||||
Method: code.Method{
|
||||
Name: "Insert",
|
||||
Params: []code.Param{
|
||||
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||
{Name: "userModel", Type: code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}}},
|
||||
{
|
||||
Name: "ctx",
|
||||
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
|
||||
},
|
||||
{
|
||||
Name: "userModel",
|
||||
Type: code.ArrayType{
|
||||
ContainedType: code.PointerType{
|
||||
ContainedType: code.SimpleType("UserModel"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Returns: []code.Type{
|
||||
code.InterfaceType{},
|
||||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidParamError,
|
||||
ExpectedError: spec.ErrInvalidParam,
|
||||
},
|
||||
{
|
||||
Name: "mismatched model parameter for many mode",
|
||||
|
@ -1345,7 +1506,7 @@ func TestParseInterfaceMethod_Insert_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidParamError,
|
||||
ExpectedError: spec.ErrInvalidParam,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -1405,7 +1566,7 @@ func TestParseInterfaceMethod_Find_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.QueryRequiredError,
|
||||
ExpectedError: spec.ErrQueryRequired,
|
||||
},
|
||||
{
|
||||
Name: "misplaced operator token (leftmost)",
|
||||
|
@ -1463,7 +1624,7 @@ func TestParseInterfaceMethod_Find_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.ContextParamRequiredError,
|
||||
ExpectedError: spec.ErrContextParamRequired,
|
||||
},
|
||||
{
|
||||
Name: "mismatched number of parameters",
|
||||
|
@ -1479,7 +1640,7 @@ func TestParseInterfaceMethod_Find_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidParamError,
|
||||
ExpectedError: spec.ErrInvalidParam,
|
||||
},
|
||||
{
|
||||
Name: "struct field not found",
|
||||
|
@ -1711,7 +1872,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidUpdateFieldsError,
|
||||
ExpectedError: spec.ErrInvalidUpdateFields,
|
||||
},
|
||||
{
|
||||
Name: "misplaced And token in update fields",
|
||||
|
@ -1725,7 +1886,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidUpdateFieldsError,
|
||||
ExpectedError: spec.ErrInvalidUpdateFields,
|
||||
},
|
||||
{
|
||||
Name: "push operator in non-array field",
|
||||
|
@ -1782,7 +1943,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.QueryRequiredError,
|
||||
ExpectedError: spec.ErrQueryRequired,
|
||||
},
|
||||
{
|
||||
Name: "ambiguous query",
|
||||
|
@ -1813,8 +1974,13 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.NewArgumentTypeNotMatchedError(consentHistoryField.Name, code.SimpleType("ConsentHistoryItem"),
|
||||
code.ArrayType{ContainedType: code.SimpleType("ConsentHistoryItem")}),
|
||||
ExpectedError: spec.NewArgumentTypeNotMatchedError(
|
||||
consentHistoryField.Name,
|
||||
code.SimpleType("ConsentHistoryItem"),
|
||||
code.ArrayType{
|
||||
ContainedType: code.SimpleType("ConsentHistoryItem"),
|
||||
},
|
||||
),
|
||||
},
|
||||
{
|
||||
Name: "insufficient function parameters",
|
||||
|
@ -1829,7 +1995,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidUpdateFieldsError,
|
||||
ExpectedError: spec.ErrInvalidUpdateFields,
|
||||
},
|
||||
{
|
||||
Name: "update model with invalid parameter",
|
||||
|
@ -1844,7 +2010,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidUpdateFieldsError,
|
||||
ExpectedError: spec.ErrInvalidUpdateFields,
|
||||
},
|
||||
{
|
||||
Name: "no context parameter",
|
||||
|
@ -1859,7 +2025,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.ContextParamRequiredError,
|
||||
ExpectedError: spec.ErrContextParamRequired,
|
||||
},
|
||||
{
|
||||
Name: "struct field not found in update fields",
|
||||
|
@ -1967,7 +2133,7 @@ func TestParseInterfaceMethod_Delete_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.QueryRequiredError,
|
||||
ExpectedError: spec.ErrQueryRequired,
|
||||
},
|
||||
{
|
||||
Name: "misplaced operator token (leftmost)",
|
||||
|
@ -2025,7 +2191,7 @@ func TestParseInterfaceMethod_Delete_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.ContextParamRequiredError,
|
||||
ExpectedError: spec.ErrContextParamRequired,
|
||||
},
|
||||
{
|
||||
Name: "mismatched number of parameters",
|
||||
|
@ -2041,7 +2207,7 @@ func TestParseInterfaceMethod_Delete_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidParamError,
|
||||
ExpectedError: spec.ErrInvalidParam,
|
||||
},
|
||||
{
|
||||
Name: "struct field not found",
|
||||
|
@ -2147,7 +2313,7 @@ func TestParseInterfaceMethod_Count_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.QueryRequiredError,
|
||||
ExpectedError: spec.ErrQueryRequired,
|
||||
},
|
||||
{
|
||||
Name: "invalid query",
|
||||
|
@ -2172,7 +2338,7 @@ func TestParseInterfaceMethod_Count_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.ContextParamRequiredError,
|
||||
ExpectedError: spec.ErrContextParamRequired,
|
||||
},
|
||||
{
|
||||
Name: "mismatched number of parameter",
|
||||
|
@ -2188,7 +2354,7 @@ func TestParseInterfaceMethod_Count_Invalid(t *testing.T) {
|
|||
code.TypeError,
|
||||
},
|
||||
},
|
||||
ExpectedError: spec.InvalidParamError,
|
||||
ExpectedError: spec.ErrInvalidParam,
|
||||
},
|
||||
{
|
||||
Name: "mismatched method parameter type",
|
||||
|
|
|
@ -46,7 +46,8 @@ const (
|
|||
ComparatorFalse Comparator = "EQUAL_FALSE"
|
||||
)
|
||||
|
||||
// ArgumentTypeFromFieldType returns a type of required argument from the given struct field type
|
||||
// ArgumentTypeFromFieldType returns a type of required argument from the given
|
||||
// struct field type.
|
||||
func (c Comparator) ArgumentTypeFromFieldType(t code.Type) code.Type {
|
||||
switch c {
|
||||
case ComparatorIn, ComparatorNotIn:
|
||||
|
@ -56,7 +57,8 @@ func (c Comparator) ArgumentTypeFromFieldType(t code.Type) code.Type {
|
|||
}
|
||||
}
|
||||
|
||||
// NumberOfArguments returns the number of arguments required to perform the comparison
|
||||
// NumberOfArguments returns the number of arguments required to perform the
|
||||
// comparison.
|
||||
func (c Comparator) NumberOfArguments() int {
|
||||
switch c {
|
||||
case ComparatorBetween:
|
||||
|
@ -82,7 +84,7 @@ type queryParser struct {
|
|||
|
||||
func (p queryParser) parseQuery(rawTokens []string, paramIndex int) (QuerySpec, error) {
|
||||
if len(rawTokens) == 0 {
|
||||
return QuerySpec{}, QueryRequiredError
|
||||
return QuerySpec{}, ErrQueryRequired
|
||||
}
|
||||
|
||||
tokens := rawTokens
|
||||
|
|
|
@ -47,14 +47,16 @@ func (u UpdateFields) NumberOfArguments() int {
|
|||
return len(u)
|
||||
}
|
||||
|
||||
// UpdateField stores mapping between field name in the model and the parameter index
|
||||
// UpdateField stores mapping between field name in the model and the parameter
|
||||
// index.
|
||||
type UpdateField struct {
|
||||
FieldReference FieldReference
|
||||
ParamIndex int
|
||||
Operator UpdateOperator
|
||||
}
|
||||
|
||||
// UpdateOperator is a custom type that declares update operator to be used in an update operation
|
||||
// UpdateOperator is a custom type that declares update operator to be used in
|
||||
// an update operation
|
||||
type UpdateOperator string
|
||||
|
||||
// UpdateOperator constants
|
||||
|
@ -117,14 +119,14 @@ func (p interfaceMethodParser) parseUpdate(tokens []string) (Update, error) {
|
|||
if len(tokens) == 0 {
|
||||
requiredType := code.PointerType{ContainedType: p.StructModel.ReferencedType()}
|
||||
if len(p.Method.Params) <= 1 || p.Method.Params[1].Type != requiredType {
|
||||
return nil, InvalidUpdateFieldsError
|
||||
return nil, ErrInvalidUpdateFields
|
||||
}
|
||||
return UpdateModel{}, nil
|
||||
}
|
||||
|
||||
updateFieldTokens, ok := splitByAnd(tokens)
|
||||
if !ok {
|
||||
return nil, InvalidUpdateFieldsError
|
||||
return nil, ErrInvalidUpdateFields
|
||||
}
|
||||
|
||||
var updateFields UpdateFields
|
||||
|
@ -142,7 +144,7 @@ func (p interfaceMethodParser) parseUpdate(tokens []string) (Update, error) {
|
|||
|
||||
for _, field := range updateFields {
|
||||
if len(p.Method.Params) < field.ParamIndex+field.Operator.NumberOfArguments() {
|
||||
return nil, InvalidUpdateFieldsError
|
||||
return nil, ErrInvalidUpdateFields
|
||||
}
|
||||
|
||||
requiredType := field.Operator.ArgumentType(field.FieldReference.ReferencedField().Type)
|
||||
|
@ -158,7 +160,9 @@ func (p interfaceMethodParser) parseUpdate(tokens []string) (Update, error) {
|
|||
return updateFields, nil
|
||||
}
|
||||
|
||||
func (p interfaceMethodParser) parseUpdateField(t []string, paramIndex int) (UpdateField, error) {
|
||||
func (p interfaceMethodParser) parseUpdateField(t []string,
|
||||
paramIndex int) (UpdateField, error) {
|
||||
|
||||
if len(t) > 1 && t[len(t)-1] == "Push" {
|
||||
return p.createUpdateField(t[:len(t)-1], UpdateOperatorPush, paramIndex)
|
||||
}
|
||||
|
@ -168,7 +172,9 @@ func (p interfaceMethodParser) parseUpdateField(t []string, paramIndex int) (Upd
|
|||
return p.createUpdateField(t, UpdateOperatorSet, paramIndex)
|
||||
}
|
||||
|
||||
func (p interfaceMethodParser) createUpdateField(t []string, operator UpdateOperator, paramIndex int) (UpdateField, error) {
|
||||
func (p interfaceMethodParser) createUpdateField(t []string,
|
||||
operator UpdateOperator, paramIndex int) (UpdateField, error) {
|
||||
|
||||
fieldReference, ok := p.fieldResolver.ResolveStructField(p.StructModel, t)
|
||||
if !ok {
|
||||
return UpdateField{}, NewStructFieldNotFoundError(t)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue