diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..01540c4 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,3 @@ +exclude_patterns: + - "examples/" + - "**/*_test.go" diff --git a/codecov.yml b/codecov.yml index cb4361b..62fd89d 100644 --- a/codecov.yml +++ b/codecov.yml @@ -2,7 +2,7 @@ coverage: status: project: default: - target: 75% + target: 90% patch: default: target: 80% diff --git a/internal/spec/query.go b/internal/spec/query.go index 929c4ce..862e04e 100644 --- a/internal/spec/query.go +++ b/internal/spec/query.go @@ -88,15 +88,20 @@ func (p queryParser) parseQuery(rawTokens []string, paramIndex int) (QuerySpec, return QuerySpec{}, ErrQueryRequired } - tokens := rawTokens - if len(tokens) == 1 && tokens[0] == "All" { - return QuerySpec{}, nil + switch rawTokens[0] { + case "All": + if len(rawTokens) == 1 { + return QuerySpec{}, nil + } + case "By": + return p.parseQueryBy(rawTokens, paramIndex) } - if tokens[0] == "By" { - tokens = tokens[1:] - } + return QuerySpec{}, NewInvalidQueryError(rawTokens) +} +func (p queryParser) parseQueryBy(rawTokens []string, paramIndex int) (QuerySpec, error) { + tokens := rawTokens[1:] if len(tokens) == 0 { return QuerySpec{}, NewInvalidQueryError(rawTokens) } diff --git a/internal/spec/update.go b/internal/spec/update.go index e360666..4d3b5bc 100644 --- a/internal/spec/update.go +++ b/internal/spec/update.go @@ -124,6 +124,19 @@ func (p interfaceMethodParser) parseUpdate(tokens []string) (Update, error) { return UpdateModel{}, nil } + updateFields, err := p.parseUpdateFieldsFromTokens(tokens) + if err != nil { + return nil, err + } + + if err := p.validateUpdateFieldsWithParams(updateFields); err != nil { + return nil, err + } + + return updateFields, nil +} + +func (p interfaceMethodParser) parseUpdateFieldsFromTokens(tokens []string) (UpdateFields, error) { updateFieldTokens, ok := splitByAnd(tokens) if !ok { return nil, ErrInvalidUpdateFields @@ -142,21 +155,6 @@ func (p interfaceMethodParser) parseUpdate(tokens []string) (Update, error) { paramIndex += updateField.Operator.NumberOfArguments() } - for _, field := range updateFields { - if p.Signature.Params().Len() < field.ParamIndex+field.Operator.NumberOfArguments() { - return nil, ErrInvalidUpdateFields - } - - expectedType := field.Operator.ArgumentType(field.FieldReference.ReferencedField().Var.Type()) - - for i := 0; i < field.Operator.NumberOfArguments(); i++ { - if !types.Identical(p.Signature.Params().At(field.ParamIndex+i).Type(), expectedType) { - return nil, NewArgumentTypeNotMatchedError(field.FieldReference.ReferencingCode(), expectedType, - p.Signature.Params().At(field.ParamIndex+i).Type()) - } - } - } - return updateFields, nil } @@ -214,3 +212,22 @@ func (p interfaceMethodParser) validateUpdateOperator(referencedType types.Type, } return true } + +func (p interfaceMethodParser) validateUpdateFieldsWithParams(updateFields UpdateFields) error { + for _, field := range updateFields { + if p.Signature.Params().Len() < field.ParamIndex+field.Operator.NumberOfArguments() { + return ErrInvalidUpdateFields + } + + expectedType := field.Operator.ArgumentType(field.FieldReference.ReferencedField().Var.Type()) + + for i := 0; i < field.Operator.NumberOfArguments(); i++ { + if !types.Identical(p.Signature.Params().At(field.ParamIndex+i).Type(), expectedType) { + return NewArgumentTypeNotMatchedError(field.FieldReference.ReferencingCode(), expectedType, + p.Signature.Params().At(field.ParamIndex+i).Type()) + } + } + } + + return nil +}