2021-01-16 06:36:44 +00:00
|
|
|
package spec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-06 11:05:47 +00:00
|
|
|
querySpec, err := parseQuery(tokens, 1)
|
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-07 05:51:51 +00:00
|
|
|
}, nil
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
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-07 05:51:51 +00:00
|
|
|
updateFieldTokens, queryTokens := p.splitUpdateFieldAndQueryTokens(tokens)
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
paramIndex := 1
|
|
|
|
var fields []UpdateField
|
|
|
|
var aggregatedToken string
|
2021-02-07 05:51:51 +00:00
|
|
|
for _, token := range updateFieldTokens {
|
|
|
|
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 {
|
|
|
|
fields = append(fields, UpdateField{Name: aggregatedToken, ParamIndex: paramIndex})
|
|
|
|
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
|
|
|
}
|
|
|
|
fields = append(fields, UpdateField{Name: aggregatedToken, ParamIndex: paramIndex})
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
querySpec, err := parseQuery(queryTokens, 1+len(fields))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range fields {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if structField.Type != p.Method.Params[field.ParamIndex].Type {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, InvalidParamError
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.validateQueryFromParams(p.Method.Params[len(fields)+1:], querySpec); err != nil {
|
2021-02-07 05:51:51 +00:00
|
|
|
return nil, err
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
return UpdateOperation{
|
2021-02-06 11:05:47 +00:00
|
|
|
Fields: fields,
|
|
|
|
Mode: mode,
|
|
|
|
Query: querySpec,
|
2021-02-07 05:51:51 +00:00
|
|
|
}, nil
|
2021-01-27 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
func (p interfaceMethodParser) splitUpdateFieldAndQueryTokens(tokens []string) ([]string, []string) {
|
|
|
|
var updateFieldTokens []string
|
|
|
|
var queryTokens []string
|
|
|
|
|
|
|
|
for i, token := range tokens {
|
|
|
|
if token == "By" || token == "All" {
|
|
|
|
queryTokens = tokens[i:]
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
updateFieldTokens = append(updateFieldTokens, token)
|
|
|
|
}
|
2021-01-26 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:51:51 +00:00
|
|
|
return updateFieldTokens, queryTokens
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
}
|