Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CI to not warn in unrelevant occasions #51

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exclude_patterns:
- "examples/"
- "**/*_test.go"
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ coverage:
status:
project:
default:
target: 75%
target: 90%
patch:
default:
target: 80%
Expand Down
30 changes: 15 additions & 15 deletions internal/spec/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ func (p interfaceMethodParser) parseFindOperation(tokens []string) (Operation, e
return nil, err
}

if err := p.validateContextParam(); err != nil {
return nil, err
}

if err := p.validateQueryFromParams(p.Signature.Params(), 1, querySpec); err != nil {
if err := p.validateQueryOnlyParams(querySpec); err != nil {
return nil, err
}

Expand Down Expand Up @@ -310,11 +306,7 @@ func (p interfaceMethodParser) parseDeleteOperation(tokens []string) (Operation,
return nil, err
}

if err := p.validateContextParam(); err != nil {
return nil, err
}

if err := p.validateQueryFromParams(p.Signature.Params(), 1, querySpec); err != nil {
if err := p.validateQueryOnlyParams(querySpec); err != nil {
return nil, err
}

Expand All @@ -334,11 +326,7 @@ func (p interfaceMethodParser) parseCountOperation(tokens []string) (Operation,
return nil, err
}

if err := p.validateContextParam(); err != nil {
return nil, err
}

if err := p.validateQueryFromParams(p.Signature.Params(), 1, querySpec); err != nil {
if err := p.validateQueryOnlyParams(querySpec); err != nil {
return nil, err
}

Expand Down Expand Up @@ -385,6 +373,18 @@ func (p interfaceMethodParser) extractIntOrBoolReturns(returns *types.Tuple) (Qu
return "", NewUnsupportedReturnError(returns.At(0).Type(), 0)
}

func (p interfaceMethodParser) validateQueryOnlyParams(querySpec QuerySpec) error {
if err := p.validateContextParam(); err != nil {
return err
}

if err := p.validateQueryFromParams(p.Signature.Params(), 1, querySpec); err != nil {
return err
}

return nil
}

func (p interfaceMethodParser) validateContextParam() error {
if p.Signature.Params().Len() == 0 || p.Signature.Params().At(0).Type().String() != "context.Context" {
return ErrContextParamRequired
Expand Down
78 changes: 49 additions & 29 deletions internal/spec/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@
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)

Check warning on line 100 in internal/spec/query.go

View check run for this annotation

Codecov / codecov/patch

internal/spec/query.go#L100

Added line #L100 was not covered by tests
}

func (p queryParser) parseQueryBy(rawTokens []string, paramIndex int) (QuerySpec, error) {
tokens := rawTokens[1:]
if len(tokens) == 0 {
return QuerySpec{}, NewInvalidQueryError(rawTokens)
}
Expand Down Expand Up @@ -155,45 +160,60 @@
func (p queryParser) parsePredicate(t []string, paramIndex int) (Predicate,
error) {

if len(t) > 1 && t[len(t)-1] == "Not" {
switch {
case endsWith(t, "Not"):
return p.createPredicate(t[:len(t)-1], ComparatorNot, paramIndex)
}
if len(t) > 2 && t[len(t)-2] == "Less" && t[len(t)-1] == "Than" {

case endsWith(t, "Less", "Than"):
return p.createPredicate(t[:len(t)-2], ComparatorLessThan, paramIndex)
}
if len(t) > 3 && t[len(t)-3] == "Less" && t[len(t)-2] == "Than" && t[len(t)-1] == "Equal" {

case endsWith(t, "Less", "Than", "Equal"):
return p.createPredicate(t[:len(t)-3], ComparatorLessThanEqual, paramIndex)
}
if len(t) > 2 && t[len(t)-2] == "Greater" && t[len(t)-1] == "Than" {

case endsWith(t, "Greater", "Than"):
return p.createPredicate(t[:len(t)-2], ComparatorGreaterThan, paramIndex)
}
if len(t) > 3 && t[len(t)-3] == "Greater" && t[len(t)-2] == "Than" && t[len(t)-1] == "Equal" {

case endsWith(t, "Greater", "Than", "Equal"):
return p.createPredicate(t[:len(t)-3], ComparatorGreaterThanEqual, paramIndex)
}
if len(t) > 2 && t[len(t)-2] == "Not" && t[len(t)-1] == "In" {

case endsWith(t, "Not", "In"):
return p.createPredicate(t[:len(t)-2], ComparatorNotIn, paramIndex)
}
if len(t) > 2 && t[len(t)-2] == "Not" && t[len(t)-1] == "Exists" {

case endsWith(t, "Not", "Exists"):
return p.createPredicate(t[:len(t)-2], ComparatorNotExists, paramIndex)
}
if len(t) > 1 && t[len(t)-1] == "In" {

case endsWith(t, "In"):
return p.createPredicate(t[:len(t)-1], ComparatorIn, paramIndex)
}
if len(t) > 1 && t[len(t)-1] == "Between" {

case endsWith(t, "Between"):
return p.createPredicate(t[:len(t)-1], ComparatorBetween, paramIndex)
}
if len(t) > 1 && t[len(t)-1] == "True" {

case endsWith(t, "True"):
return p.createPredicate(t[:len(t)-1], ComparatorTrue, paramIndex)
}
if len(t) > 1 && t[len(t)-1] == "False" {

case endsWith(t, "False"):
return p.createPredicate(t[:len(t)-1], ComparatorFalse, paramIndex)
}
if len(t) > 1 && t[len(t)-1] == "Exists" {

case endsWith(t, "Exists"):
return p.createPredicate(t[:len(t)-1], ComparatorExists, paramIndex)
}

return p.createPredicate(t, ComparatorEqual, paramIndex)
}

func endsWith(t []string, suffix ...string) bool {
if len(t) < len(suffix) {
return false
}

for i, suffixToken := range suffix {
if t[len(t)-len(suffix)+i] != suffixToken {
return false
}
}
return true
}

func (p queryParser) createPredicate(t []string, comparator Comparator,
paramIndex int) (Predicate, error) {

Expand Down
47 changes: 32 additions & 15 deletions internal/spec/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Loading