2021-01-16 06:36:44 +00:00
|
|
|
package spec
|
|
|
|
|
|
|
|
import (
|
2021-02-26 03:27:00 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-01-16 06:36:44 +00:00
|
|
|
"github.com/fatih/camelcase"
|
|
|
|
"github.com/sunboyy/repogen/internal/code"
|
|
|
|
)
|
|
|
|
|
2021-01-21 11:56:30 +00:00
|
|
|
// ParseInterfaceMethod returns repository method spec from declared interface method
|
|
|
|
func ParseInterfaceMethod(structModel code.Struct, method code.Method) (MethodSpec, error) {
|
|
|
|
parser := interfaceMethodParser{
|
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-01-16 06:36:44 +00:00
|
|
|
StructModel code.Struct
|
2021-01-21 11:56:30 +00:00
|
|
|
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 {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, InvalidParamError
|
2021-02-01 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arrayType := code.ArrayType{ContainedType: pointerType}
|
|
|
|
if mode == QueryModeMany && p.Method.Params[1].Type != arrayType {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, InvalidParamError
|
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 {
|
|
|
|
return "", UnsupportedReturnError
|
|
|
|
}
|
|
|
|
|
|
|
|
if returns[1] != code.SimpleType("error") {
|
|
|
|
return "", UnsupportedReturnError
|
|
|
|
}
|
|
|
|
|
|
|
|
interfaceType, ok := returns[0].(code.InterfaceType)
|
|
|
|
if ok {
|
|
|
|
if len(interfaceType.Methods) != 0 {
|
|
|
|
return "", UnsupportedReturnError
|
|
|
|
}
|
|
|
|
return QueryModeOne, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
arrayType, ok := returns[0].(code.ArrayType)
|
|
|
|
if ok {
|
|
|
|
interfaceType, ok := arrayType.ContainedType.(code.InterfaceType)
|
|
|
|
if !ok || len(interfaceType.Methods) != 0 {
|
|
|
|
return "", UnsupportedReturnError
|
|
|
|
}
|
|
|
|
return QueryModeMany, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", UnsupportedReturnError
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
queryTokens, sortTokens := p.splitQueryAndSortTokens(tokens)
|
|
|
|
|
|
|
|
querySpec, err := parseQuery(queryTokens, 1)
|
|
|
|
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,
|
2021-02-07 05:51:51 +00:00
|
|
|
}, nil
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
func (p interfaceMethodParser) parseSort(rawTokens []string) ([]Sort, error) {
|
|
|
|
if len(rawTokens) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sortTokens := rawTokens[2:]
|
|
|
|
|
|
|
|
var sorts []Sort
|
|
|
|
var aggregatedToken sortToken
|
|
|
|
for _, token := range sortTokens {
|
|
|
|
if token != "And" {
|
|
|
|
aggregatedToken = append(aggregatedToken, token)
|
|
|
|
} else if len(aggregatedToken) == 0 {
|
|
|
|
return nil, NewInvalidSortError(rawTokens)
|
|
|
|
} else {
|
|
|
|
sorts = append(sorts, aggregatedToken.ToSort())
|
|
|
|
aggregatedToken = sortToken{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(aggregatedToken) == 0 {
|
|
|
|
return nil, NewInvalidSortError(rawTokens)
|
|
|
|
}
|
|
|
|
sorts = append(sorts, aggregatedToken.ToSort())
|
|
|
|
|
|
|
|
return sorts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sortToken []string
|
|
|
|
|
|
|
|
func (t sortToken) ToSort() Sort {
|
|
|
|
if len(t) > 1 && t[len(t)-1] == "Asc" {
|
|
|
|
return Sort{FieldName: strings.Join(t[:len(t)-1], ""), Ordering: OrderingAscending}
|
|
|
|
}
|
|
|
|
if len(t) > 1 && t[len(t)-1] == "Desc" {
|
|
|
|
return Sort{FieldName: strings.Join(t[:len(t)-1], ""), Ordering: OrderingDescending}
|
|
|
|
}
|
|
|
|
return Sort{FieldName: strings.Join(t, ""), Ordering: OrderingAscending}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-21 11:56:30 +00:00
|
|
|
return "", UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if returns[1] != code.SimpleType("error") {
|
2021-01-21 11:56:30 +00:00
|
|
|
return "", UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pointerType, ok := returns[0].(code.PointerType)
|
|
|
|
if ok {
|
|
|
|
simpleType := pointerType.ContainedType
|
|
|
|
if simpleType == code.SimpleType(p.StructModel.Name) {
|
|
|
|
return QueryModeOne, nil
|
|
|
|
}
|
2021-01-21 11:56:30 +00:00
|
|
|
return "", UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arrayType, ok := returns[0].(code.ArrayType)
|
|
|
|
if ok {
|
|
|
|
pointerType, ok := arrayType.ContainedType.(code.PointerType)
|
|
|
|
if ok {
|
|
|
|
simpleType := pointerType.ContainedType
|
|
|
|
if simpleType == code.SimpleType(p.StructModel.Name) {
|
|
|
|
return QueryModeMany, nil
|
|
|
|
}
|
2021-01-21 11:56:30 +00:00
|
|
|
return "", UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 11:56:30 +00:00
|
|
|
return "", UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
func (p interfaceMethodParser) parseUpdateOperation(tokens []string) (Operation, error) {
|
2021-02-06 11:05:47 +00:00
|
|
|
mode, err := p.extractIntOrBoolReturns(p.Method.Returns)
|
2021-01-27 12:15:25 +00:00
|
|
|
if err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:02:57 +00:00
|
|
|
if err := p.validateContextParam(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTokens, queryTokens := p.splitUpdateAndQueryTokens(tokens)
|
|
|
|
|
|
|
|
update, err := p.parseUpdate(updateTokens)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
querySpec, err := parseQuery(queryTokens, 1+update.NumberOfArguments())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.validateQueryFromParams(p.Method.Params[update.NumberOfArguments()+1:], querySpec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return UpdateOperation{
|
|
|
|
Update: update,
|
|
|
|
Mode: mode,
|
|
|
|
Query: querySpec,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 UpdateModel{}, nil
|
|
|
|
}
|
2021-02-07 05:51:51 +00:00
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
paramIndex := 1
|
2021-02-24 12:02:57 +00:00
|
|
|
var update UpdateFields
|
2021-01-27 12:15:25 +00:00
|
|
|
var aggregatedToken string
|
2021-02-24 12:02:57 +00:00
|
|
|
for _, token := range tokens {
|
2021-02-07 05:51:51 +00:00
|
|
|
if token != "And" {
|
2021-01-27 12:15:25 +00:00
|
|
|
aggregatedToken += token
|
|
|
|
} else if len(aggregatedToken) == 0 {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, InvalidUpdateFieldsError
|
2021-01-27 12:15:25 +00:00
|
|
|
} else {
|
2021-02-24 12:02:57 +00:00
|
|
|
update = append(update, UpdateField{Name: aggregatedToken, ParamIndex: paramIndex})
|
2021-01-27 12:15:25 +00:00
|
|
|
paramIndex++
|
|
|
|
aggregatedToken = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(aggregatedToken) == 0 {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, InvalidUpdateFieldsError
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
2021-02-24 12:02:57 +00:00
|
|
|
update = append(update, UpdateField{Name: aggregatedToken, ParamIndex: paramIndex})
|
2021-01-27 12:15:25 +00:00
|
|
|
|
2021-02-24 12:02:57 +00:00
|
|
|
for _, field := range update {
|
2021-01-27 12:15:25 +00:00
|
|
|
structField, ok := p.StructModel.Fields.ByName(field.Name)
|
|
|
|
if !ok {
|
2021-02-14 04:48:09 +00:00
|
|
|
return nil, NewStructFieldNotFoundError(field.Name)
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:02:57 +00:00
|
|
|
if len(p.Method.Params) <= field.ParamIndex || structField.Type != p.Method.Params[field.ParamIndex].Type {
|
|
|
|
return nil, InvalidUpdateFieldsError
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 12:02:57 +00:00
|
|
|
return update, nil
|
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-02-06 11:05:47 +00:00
|
|
|
querySpec, err := 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-02-06 11:05:47 +00:00
|
|
|
querySpec, err := parseQuery(tokens, 1)
|
|
|
|
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 {
|
|
|
|
return UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
if returns[0] != code.SimpleType("int") {
|
|
|
|
return UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
if returns[1] != code.SimpleType("error") {
|
|
|
|
return UnsupportedReturnError
|
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 {
|
|
|
|
return "", UnsupportedReturnError
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
if returns[1] != code.SimpleType("error") {
|
|
|
|
return "", UnsupportedReturnError
|
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 {
|
|
|
|
if simpleType == code.SimpleType("bool") {
|
|
|
|
return QueryModeOne, nil
|
|
|
|
}
|
|
|
|
if simpleType == code.SimpleType("int") {
|
|
|
|
return QueryModeMany, nil
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
return "", UnsupportedReturnError
|
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 {
|
|
|
|
return ContextParamRequiredError
|
|
|
|
}
|
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) {
|
2021-01-21 11:56:30 +00:00
|
|
|
return InvalidParamError
|
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
var currentParamIndex int
|
2021-01-21 11:56:30 +00:00
|
|
|
for _, predicate := range querySpec.Predicates {
|
|
|
|
structField, ok := p.StructModel.Fields.ByName(predicate.Field)
|
|
|
|
if !ok {
|
2021-02-14 04:48:09 +00:00
|
|
|
return NewStructFieldNotFoundError(predicate.Field)
|
2021-01-21 11:56:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 12:10:25 +00:00
|
|
|
if (predicate.Comparator == ComparatorTrue || predicate.Comparator == ComparatorFalse) &&
|
|
|
|
structField.Type != code.SimpleType("bool") {
|
|
|
|
return NewIncompatibleComparatorError(predicate.Comparator, structField)
|
|
|
|
}
|
|
|
|
|
2021-01-24 08:31:21 +00:00
|
|
|
for i := 0; i < predicate.Comparator.NumberOfArguments(); i++ {
|
2021-01-27 12:15:25 +00:00
|
|
|
if params[currentParamIndex].Type != predicate.Comparator.ArgumentTypeFromFieldType(
|
2021-01-24 08:31:21 +00:00
|
|
|
structField.Type) {
|
|
|
|
return InvalidParamError
|
|
|
|
}
|
|
|
|
currentParamIndex++
|
2021-01-21 11:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|