2021-01-16 06:36:44 +00:00
|
|
|
package spec
|
|
|
|
|
|
|
|
import (
|
2023-05-21 15:34:30 +00:00
|
|
|
"strconv"
|
|
|
|
|
2023-05-24 11:04:29 +00:00
|
|
|
"git.kmsign.ru/royalcat/repogen/internal/code"
|
2021-01-16 06:36:44 +00:00
|
|
|
"github.com/fatih/camelcase"
|
|
|
|
)
|
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
// ParseInterfaceMethod returns repository method spec from declared interface
|
|
|
|
// method.
|
|
|
|
func ParseInterfaceMethod(structs map[string]code.Struct, structModel code.Struct,
|
|
|
|
method code.Method) (MethodSpec, error) {
|
|
|
|
|
2021-01-21 11:56:30 +00:00
|
|
|
parser := interfaceMethodParser{
|
2021-03-03 14:01:32 +00:00
|
|
|
fieldResolver: fieldResolver{
|
|
|
|
Structs: structs,
|
|
|
|
},
|
2021-01-16 06:36:44 +00:00
|
|
|
StructModel: structModel,
|
2021-01-21 11:56:30 +00:00
|
|
|
Method: method,
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return parser.Parse()
|
|
|
|
}
|
|
|
|
|
2021-01-21 11:56:30 +00:00
|
|
|
type interfaceMethodParser struct {
|
2021-03-03 14:01:32 +00:00
|
|
|
fieldResolver fieldResolver
|
|
|
|
StructModel code.Struct
|
|
|
|
Method code.Method
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 11:56:30 +00:00
|
|
|
func (p interfaceMethodParser) Parse() (MethodSpec, error) {
|
2021-02-07 05:51:51 +00:00
|
|
|
operation, err := p.parseMethod()
|
|
|
|
if err != nil {
|
|
|
|
return MethodSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return MethodSpec{
|
|
|
|
Name: p.Method.Name,
|
|
|
|
Params: p.Method.Params,
|
|
|
|
Returns: p.Method.Returns,
|
|
|
|
Operation: operation,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p interfaceMethodParser) parseMethod() (Operation, error) {
|
2021-01-21 11:56:30 +00:00
|
|
|
methodNameTokens := camelcase.Split(p.Method.Name)
|
2021-01-16 06:36:44 +00:00
|
|
|
switch methodNameTokens[0] {
|
2021-02-01 14:39:20 +00:00
|
|
|
case "Insert":
|
2021-02-07 05:51:51 +00:00
|
|
|
return p.parseInsertOperation(methodNameTokens[1:])
|
2021-01-16 06:36:44 +00:00
|
|
|
case "Find":
|
2021-02-07 05:51:51 +00:00
|
|
|
return p.parseFindOperation(methodNameTokens[1:])
|
2021-01-27 12:15:25 +00:00
|
|
|
case "Update":
|
2021-02-07 05:51:51 +00:00
|
|
|
return p.parseUpdateOperation(methodNameTokens[1:])
|
2021-01-26 13:23:52 +00:00
|
|
|
case "Delete":
|
2021-02-07 05:51:51 +00:00
|
|
|
return p.parseDeleteOperation(methodNameTokens[1:])
|
2021-02-06 11:05:47 +00:00
|
|
|
case "Count":
|
2021-02-07 05:51:51 +00:00
|
|
|
return p.parseCountOperation(methodNameTokens[1:])
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
2021-02-14 04:48:09 +00:00
|
|
|
return nil, NewUnknownOperationError(methodNameTokens[0])
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
func (p interfaceMethodParser) parseInsertOperation(tokens []string) (Operation, error) {
|
2021-02-01 14:39:20 +00:00
|
|
|
mode, err := p.extractInsertReturns(p.Method.Returns)
|
|
|
|
if err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.validateContextParam(); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pointerType := code.PointerType{ContainedType: p.StructModel.ReferencedType()}
|
|
|
|
if mode == QueryModeOne && p.Method.Params[1].Type != pointerType {
|
2022-11-09 04:21:11 +00:00
|
|
|
return nil, ErrInvalidParam
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arrayType := code.ArrayType{ContainedType: pointerType}
|
|
|
|
if mode == QueryModeMany && p.Method.Params[1].Type != arrayType {
|
2022-11-09 04:21:11 +00:00
|
|
|
return nil, ErrInvalidParam
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
return InsertOperation{
|
2021-02-06 11:05:47 +00:00
|
|
|
Mode: mode,
|
2021-02-07 05:51:51 +00:00
|
|
|
}, nil
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p interfaceMethodParser) extractInsertReturns(returns []code.Type) (QueryMode, error) {
|
|
|
|
if len(returns) != 2 {
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewOperationReturnCountUnmatchedError(2)
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
if returns[1] != code.TypeError {
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewUnsupportedReturnError(returns[1], 1)
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
switch t := returns[0].(type) {
|
|
|
|
case code.InterfaceType:
|
|
|
|
if len(t.Methods) == 0 {
|
|
|
|
return QueryModeOne, nil
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
case code.ArrayType:
|
|
|
|
interfaceType, ok := t.ContainedType.(code.InterfaceType)
|
|
|
|
if ok && len(interfaceType.Methods) == 0 {
|
|
|
|
return QueryModeMany, nil
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewUnsupportedReturnError(returns[0], 0)
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
func (p interfaceMethodParser) parseFindOperation(tokens []string) (Operation, error) {
|
2021-02-06 11:05:47 +00:00
|
|
|
mode, err := p.extractModelOrSliceReturns(p.Method.Returns)
|
2021-01-16 06:36:44 +00:00
|
|
|
if err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 15:34:30 +00:00
|
|
|
limit, tokens, err := p.parseFindTop(tokens)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if mode == QueryModeOne && limit != 0 {
|
|
|
|
return nil, ErrLimitOnFindOne
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
queryTokens, sortTokens := p.splitQueryAndSortTokens(tokens)
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
querySpec, err := p.parseQuery(queryTokens, 1)
|
2021-02-26 03:27:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sorts, err := p.parseSort(sortTokens)
|
2021-01-16 06:36:44 +00:00
|
|
|
if err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := p.validateContextParam(); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.validateQueryFromParams(p.Method.Params[1:], querySpec); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
return FindOperation{
|
2021-02-06 11:05:47 +00:00
|
|
|
Mode: mode,
|
|
|
|
Query: querySpec,
|
2021-02-26 03:27:00 +00:00
|
|
|
Sorts: sorts,
|
2023-05-21 15:34:30 +00:00
|
|
|
Limit: limit,
|
2021-02-07 05:51:51 +00:00
|
|
|
}, nil
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 15:34:30 +00:00
|
|
|
func (p interfaceMethodParser) parseFindTop(tokens []string) (int, []string,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
if len(tokens) >= 1 && tokens[0] == "Top" {
|
|
|
|
if len(tokens) < 2 {
|
|
|
|
return 0, nil, ErrLimitAmountRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
limit, err := strconv.Atoi(tokens[1])
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, ErrLimitAmountRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit <= 0 {
|
|
|
|
return 0, nil, ErrLimitNonPositive
|
|
|
|
}
|
|
|
|
return limit, tokens[2:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, tokens, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
func (p interfaceMethodParser) parseSort(rawTokens []string) ([]Sort, error) {
|
|
|
|
if len(rawTokens) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
sortTokens, ok := splitByAnd(rawTokens[2:])
|
|
|
|
if !ok {
|
|
|
|
return nil, NewInvalidSortError(rawTokens)
|
|
|
|
}
|
2021-02-26 03:27:00 +00:00
|
|
|
|
|
|
|
var sorts []Sort
|
|
|
|
for _, token := range sortTokens {
|
2021-03-03 14:01:32 +00:00
|
|
|
sort, err := p.parseSortToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-02-26 03:27:00 +00:00
|
|
|
}
|
2021-03-03 14:01:32 +00:00
|
|
|
sorts = append(sorts, sort)
|
2021-02-26 03:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sorts, nil
|
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
func (p interfaceMethodParser) parseSortToken(t []string) (Sort, error) {
|
2021-02-26 03:27:00 +00:00
|
|
|
if len(t) > 1 && t[len(t)-1] == "Asc" {
|
2021-03-03 14:01:32 +00:00
|
|
|
return p.createSort(t[:len(t)-1], OrderingAscending)
|
2021-02-26 03:27:00 +00:00
|
|
|
}
|
|
|
|
if len(t) > 1 && t[len(t)-1] == "Desc" {
|
2021-03-03 14:01:32 +00:00
|
|
|
return p.createSort(t[:len(t)-1], OrderingDescending)
|
|
|
|
}
|
|
|
|
return p.createSort(t, OrderingAscending)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p interfaceMethodParser) createSort(t []string, ordering Ordering) (Sort, error) {
|
|
|
|
fields, ok := p.fieldResolver.ResolveStructField(p.StructModel, t)
|
|
|
|
if !ok {
|
|
|
|
return Sort{}, NewStructFieldNotFoundError(t)
|
2021-02-26 03:27:00 +00:00
|
|
|
}
|
2021-03-03 14:01:32 +00:00
|
|
|
|
|
|
|
return Sort{
|
|
|
|
FieldReference: fields,
|
|
|
|
Ordering: ordering,
|
|
|
|
}, nil
|
2021-02-26 03:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p interfaceMethodParser) splitQueryAndSortTokens(tokens []string) ([]string, []string) {
|
|
|
|
var queryTokens []string
|
|
|
|
var sortTokens []string
|
|
|
|
|
|
|
|
for i, token := range tokens {
|
|
|
|
if len(tokens) > i && token == "Order" && tokens[i+1] == "By" {
|
|
|
|
sortTokens = tokens[i:]
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
queryTokens = append(queryTokens, token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return queryTokens, sortTokens
|
|
|
|
}
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
func (p interfaceMethodParser) extractModelOrSliceReturns(returns []code.Type) (QueryMode, error) {
|
2021-01-16 06:36:44 +00:00
|
|
|
if len(returns) != 2 {
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewOperationReturnCountUnmatchedError(2)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
if returns[1] != code.TypeError {
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewUnsupportedReturnError(returns[1], 1)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
switch t := returns[0].(type) {
|
|
|
|
case code.PointerType:
|
|
|
|
simpleType := t.ContainedType
|
2021-01-16 06:36:44 +00:00
|
|
|
if simpleType == code.SimpleType(p.StructModel.Name) {
|
|
|
|
return QueryModeOne, nil
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
case code.ArrayType:
|
|
|
|
pointerType, ok := t.ContainedType.(code.PointerType)
|
2021-01-16 06:36:44 +00:00
|
|
|
if ok {
|
|
|
|
simpleType := pointerType.ContainedType
|
|
|
|
if simpleType == code.SimpleType(p.StructModel.Name) {
|
|
|
|
return QueryModeMany, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewUnsupportedReturnError(returns[0], 0)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
func splitByAnd(tokens []string) ([][]string, bool) {
|
|
|
|
var updateFieldTokens [][]string
|
|
|
|
var aggregatedToken []string
|
|
|
|
|
|
|
|
for _, token := range tokens {
|
|
|
|
if token != "And" {
|
|
|
|
aggregatedToken = append(aggregatedToken, token)
|
|
|
|
} else if len(aggregatedToken) == 0 {
|
|
|
|
return nil, false
|
|
|
|
} else {
|
|
|
|
updateFieldTokens = append(updateFieldTokens, aggregatedToken)
|
|
|
|
aggregatedToken = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(aggregatedToken) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
updateFieldTokens = append(updateFieldTokens, aggregatedToken)
|
|
|
|
|
|
|
|
return updateFieldTokens, true
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:02:57 +00:00
|
|
|
func (p interfaceMethodParser) splitUpdateAndQueryTokens(tokens []string) ([]string, []string) {
|
|
|
|
var updateTokens []string
|
2021-02-07 05:51:51 +00:00
|
|
|
var queryTokens []string
|
|
|
|
|
|
|
|
for i, token := range tokens {
|
|
|
|
if token == "By" || token == "All" {
|
|
|
|
queryTokens = tokens[i:]
|
|
|
|
break
|
|
|
|
} else {
|
2021-02-24 12:02:57 +00:00
|
|
|
updateTokens = append(updateTokens, token)
|
2021-02-07 05:51:51 +00:00
|
|
|
}
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:02:57 +00:00
|
|
|
return updateTokens, queryTokens
|
2021-02-07 05:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p interfaceMethodParser) parseDeleteOperation(tokens []string) (Operation, error) {
|
2021-02-06 11:05:47 +00:00
|
|
|
mode, err := p.extractIntOrBoolReturns(p.Method.Returns)
|
2021-01-26 13:23:52 +00:00
|
|
|
if err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
querySpec, err := p.parseQuery(tokens, 1)
|
2021-01-26 13:23:52 +00:00
|
|
|
if err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := p.validateContextParam(); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.validateQueryFromParams(p.Method.Params[1:], querySpec); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
return DeleteOperation{
|
2021-02-06 11:05:47 +00:00
|
|
|
Mode: mode,
|
|
|
|
Query: querySpec,
|
2021-02-07 05:51:51 +00:00
|
|
|
}, nil
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
func (p interfaceMethodParser) parseCountOperation(tokens []string) (Operation, error) {
|
2021-02-06 11:05:47 +00:00
|
|
|
if err := p.validateCountReturns(p.Method.Returns); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
querySpec, err := p.parseQuery(tokens, 1)
|
2021-02-06 11:05:47 +00:00
|
|
|
if err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
if err := p.validateContextParam(); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.validateQueryFromParams(p.Method.Params[1:], querySpec); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
return CountOperation{
|
2021-02-06 11:05:47 +00:00
|
|
|
Query: querySpec,
|
2021-02-07 05:51:51 +00:00
|
|
|
}, nil
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
func (p interfaceMethodParser) validateCountReturns(returns []code.Type) error {
|
|
|
|
if len(returns) != 2 {
|
2021-06-02 11:39:15 +00:00
|
|
|
return NewOperationReturnCountUnmatchedError(2)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
if returns[0] != code.TypeInt {
|
2021-06-02 11:39:15 +00:00
|
|
|
return NewUnsupportedReturnError(returns[0], 0)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
if returns[1] != code.TypeError {
|
2021-06-02 11:39:15 +00:00
|
|
|
return NewUnsupportedReturnError(returns[1], 1)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
2021-02-06 11:05:47 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p interfaceMethodParser) extractIntOrBoolReturns(returns []code.Type) (QueryMode, error) {
|
|
|
|
if len(returns) != 2 {
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewOperationReturnCountUnmatchedError(2)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
if returns[1] != code.TypeError {
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewUnsupportedReturnError(returns[1], 1)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
2021-01-19 12:26:26 +00:00
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
simpleType, ok := returns[0].(code.SimpleType)
|
|
|
|
if ok {
|
2022-10-18 12:37:50 +00:00
|
|
|
if simpleType == code.TypeBool {
|
2021-02-06 11:05:47 +00:00
|
|
|
return QueryModeOne, nil
|
|
|
|
}
|
2022-10-18 12:37:50 +00:00
|
|
|
if simpleType == code.TypeInt {
|
2021-02-06 11:05:47 +00:00
|
|
|
return QueryModeMany, nil
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:39:15 +00:00
|
|
|
return "", NewUnsupportedReturnError(returns[0], 0)
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
2021-01-21 11:56:30 +00:00
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
func (p interfaceMethodParser) validateContextParam() error {
|
2021-01-21 11:56:30 +00:00
|
|
|
contextType := code.ExternalType{PackageAlias: "context", Name: "Context"}
|
|
|
|
if len(p.Method.Params) == 0 || p.Method.Params[0].Type != contextType {
|
2022-11-09 04:21:11 +00:00
|
|
|
return ErrContextParamRequired
|
2021-01-21 11:56:30 +00:00
|
|
|
}
|
2021-01-27 12:15:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-21 11:56:30 +00:00
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
func (p interfaceMethodParser) validateQueryFromParams(params []code.Param, querySpec QuerySpec) error {
|
|
|
|
if querySpec.NumberOfArguments() != len(params) {
|
2022-11-09 04:21:11 +00:00
|
|
|
return ErrInvalidParam
|
2021-01-21 11:56:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
var currentParamIndex int
|
2021-01-21 11:56:30 +00:00
|
|
|
for _, predicate := range querySpec.Predicates {
|
2021-02-23 12:10:25 +00:00
|
|
|
if (predicate.Comparator == ComparatorTrue || predicate.Comparator == ComparatorFalse) &&
|
2022-10-18 12:37:50 +00:00
|
|
|
predicate.FieldReference.ReferencedField().Type != code.TypeBool {
|
2021-03-03 14:01:32 +00:00
|
|
|
return NewIncompatibleComparatorError(predicate.Comparator,
|
|
|
|
predicate.FieldReference.ReferencedField())
|
2021-02-23 12:10:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 08:31:21 +00:00
|
|
|
for i := 0; i < predicate.Comparator.NumberOfArguments(); i++ {
|
2022-11-09 04:21:11 +00:00
|
|
|
requiredType := predicate.Comparator.ArgumentTypeFromFieldType(
|
|
|
|
predicate.FieldReference.ReferencedField().Type,
|
|
|
|
)
|
2021-05-03 07:12:58 +00:00
|
|
|
|
|
|
|
if params[currentParamIndex].Type != requiredType {
|
|
|
|
return NewArgumentTypeNotMatchedError(predicate.FieldReference.ReferencingCode(), requiredType,
|
|
|
|
params[currentParamIndex].Type)
|
2021-01-24 08:31:21 +00:00
|
|
|
}
|
|
|
|
currentParamIndex++
|
2021-01-21 11:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-03 14:01:32 +00:00
|
|
|
|
|
|
|
func (p interfaceMethodParser) parseQuery(queryTokens []string, paramIndex int) (QuerySpec, error) {
|
|
|
|
queryParser := queryParser{
|
|
|
|
fieldResolver: p.fieldResolver,
|
|
|
|
StructModel: p.StructModel,
|
|
|
|
}
|
|
|
|
return queryParser.parseQuery(queryTokens, paramIndex)
|
|
|
|
}
|