2021-03-30 12:16:46 +00:00
|
|
|
package spec
|
|
|
|
|
2023-05-24 11:01:50 +00:00
|
|
|
import "git.kmsign.com/royalcat/repogen/internal/code"
|
2021-03-30 12:16:46 +00:00
|
|
|
|
|
|
|
// UpdateOperation is a method specification for update operations
|
|
|
|
type UpdateOperation struct {
|
|
|
|
Update Update
|
|
|
|
Mode QueryMode
|
|
|
|
Query QuerySpec
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns "Update" operation name
|
|
|
|
func (o UpdateOperation) Name() string {
|
|
|
|
return "Update"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is an interface of update operation type
|
|
|
|
type Update interface {
|
|
|
|
Name() string
|
|
|
|
NumberOfArguments() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateModel is a type of update operation that update the whole model
|
|
|
|
type UpdateModel struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns UpdateModel name 'Model'
|
|
|
|
func (u UpdateModel) Name() string {
|
|
|
|
return "Model"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOfArguments returns 1
|
|
|
|
func (u UpdateModel) NumberOfArguments() int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateFields is a type of update operation that update specific fields
|
|
|
|
type UpdateFields []UpdateField
|
|
|
|
|
|
|
|
// Name returns UpdateFields name 'Fields'
|
|
|
|
func (u UpdateFields) Name() string {
|
|
|
|
return "Fields"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOfArguments returns number of update fields
|
|
|
|
func (u UpdateFields) NumberOfArguments() int {
|
|
|
|
return len(u)
|
|
|
|
}
|
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
// UpdateField stores mapping between field name in the model and the parameter
|
|
|
|
// index.
|
2021-03-30 12:16:46 +00:00
|
|
|
type UpdateField struct {
|
|
|
|
FieldReference FieldReference
|
|
|
|
ParamIndex int
|
2021-03-31 11:44:31 +00:00
|
|
|
Operator UpdateOperator
|
|
|
|
}
|
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
// UpdateOperator is a custom type that declares update operator to be used in
|
|
|
|
// an update operation
|
2021-03-31 11:44:31 +00:00
|
|
|
type UpdateOperator string
|
|
|
|
|
|
|
|
// UpdateOperator constants
|
|
|
|
const (
|
|
|
|
UpdateOperatorSet UpdateOperator = "SET"
|
|
|
|
UpdateOperatorPush UpdateOperator = "PUSH"
|
2021-05-03 07:12:58 +00:00
|
|
|
UpdateOperatorInc UpdateOperator = "INC"
|
2021-03-31 11:44:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NumberOfArguments returns number of arguments required to perform an update operation
|
|
|
|
func (o UpdateOperator) NumberOfArguments() int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArgumentType returns type that is required for function parameter
|
2021-05-03 07:12:58 +00:00
|
|
|
func (o UpdateOperator) ArgumentType(fieldType code.Type) code.Type {
|
2021-03-31 11:44:31 +00:00
|
|
|
switch o {
|
|
|
|
case UpdateOperatorPush:
|
2021-05-03 07:12:58 +00:00
|
|
|
arrayType := fieldType.(code.ArrayType)
|
|
|
|
return arrayType.ContainedType
|
2021-03-31 11:44:31 +00:00
|
|
|
default:
|
2021-05-03 07:12:58 +00:00
|
|
|
return fieldType
|
2021-03-31 11:44:31 +00:00
|
|
|
}
|
2021-03-30 12:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p interfaceMethodParser) parseUpdateOperation(tokens []string) (Operation, error) {
|
|
|
|
mode, err := p.extractIntOrBoolReturns(p.Method.Returns)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := p.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 {
|
2022-11-09 04:21:11 +00:00
|
|
|
return nil, ErrInvalidUpdateFields
|
2021-03-30 12:16:46 +00:00
|
|
|
}
|
|
|
|
return UpdateModel{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFieldTokens, ok := splitByAnd(tokens)
|
|
|
|
if !ok {
|
2022-11-09 04:21:11 +00:00
|
|
|
return nil, ErrInvalidUpdateFields
|
2021-03-30 12:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var updateFields UpdateFields
|
|
|
|
|
|
|
|
paramIndex := 1
|
|
|
|
for _, updateFieldToken := range updateFieldTokens {
|
2021-03-31 11:44:31 +00:00
|
|
|
updateField, err := p.parseUpdateField(updateFieldToken, paramIndex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-03-30 12:16:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 11:44:31 +00:00
|
|
|
updateFields = append(updateFields, updateField)
|
|
|
|
paramIndex += updateField.Operator.NumberOfArguments()
|
2021-03-30 12:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range updateFields {
|
2021-03-31 11:44:31 +00:00
|
|
|
if len(p.Method.Params) < field.ParamIndex+field.Operator.NumberOfArguments() {
|
2022-11-09 04:21:11 +00:00
|
|
|
return nil, ErrInvalidUpdateFields
|
2021-03-30 12:16:46 +00:00
|
|
|
}
|
2021-03-31 11:44:31 +00:00
|
|
|
|
2021-05-03 07:12:58 +00:00
|
|
|
requiredType := field.Operator.ArgumentType(field.FieldReference.ReferencedField().Type)
|
2021-03-31 11:44:31 +00:00
|
|
|
|
|
|
|
for i := 0; i < field.Operator.NumberOfArguments(); i++ {
|
|
|
|
if requiredType != p.Method.Params[field.ParamIndex+i].Type {
|
2021-05-03 07:12:58 +00:00
|
|
|
return nil, NewArgumentTypeNotMatchedError(field.FieldReference.ReferencingCode(), requiredType,
|
|
|
|
p.Method.Params[field.ParamIndex+i].Type)
|
2021-03-31 11:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-30 12:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return updateFields, nil
|
|
|
|
}
|
2021-03-31 11:44:31 +00:00
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
func (p interfaceMethodParser) parseUpdateField(t []string,
|
|
|
|
paramIndex int) (UpdateField, error) {
|
|
|
|
|
2021-03-31 11:44:31 +00:00
|
|
|
if len(t) > 1 && t[len(t)-1] == "Push" {
|
|
|
|
return p.createUpdateField(t[:len(t)-1], UpdateOperatorPush, paramIndex)
|
|
|
|
}
|
2021-05-03 07:12:58 +00:00
|
|
|
if len(t) > 1 && t[len(t)-1] == "Inc" {
|
|
|
|
return p.createUpdateField(t[:len(t)-1], UpdateOperatorInc, paramIndex)
|
|
|
|
}
|
2021-03-31 11:44:31 +00:00
|
|
|
return p.createUpdateField(t, UpdateOperatorSet, paramIndex)
|
|
|
|
}
|
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
func (p interfaceMethodParser) createUpdateField(t []string,
|
|
|
|
operator UpdateOperator, paramIndex int) (UpdateField, error) {
|
|
|
|
|
2021-03-31 11:44:31 +00:00
|
|
|
fieldReference, ok := p.fieldResolver.ResolveStructField(p.StructModel, t)
|
|
|
|
if !ok {
|
|
|
|
return UpdateField{}, NewStructFieldNotFoundError(t)
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:12:58 +00:00
|
|
|
if !p.validateUpdateOperator(fieldReference.ReferencedField().Type, operator) {
|
|
|
|
return UpdateField{}, NewIncompatibleUpdateOperatorError(operator, fieldReference)
|
|
|
|
}
|
|
|
|
|
2021-03-31 11:44:31 +00:00
|
|
|
return UpdateField{
|
|
|
|
FieldReference: fieldReference,
|
|
|
|
ParamIndex: paramIndex,
|
|
|
|
Operator: operator,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-05-03 07:12:58 +00:00
|
|
|
|
|
|
|
func (p interfaceMethodParser) validateUpdateOperator(referencedType code.Type, operator UpdateOperator) bool {
|
|
|
|
switch operator {
|
|
|
|
case UpdateOperatorPush:
|
|
|
|
_, ok := referencedType.(code.ArrayType)
|
|
|
|
return ok
|
|
|
|
case UpdateOperatorInc:
|
|
|
|
return referencedType.IsNumber()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|