Skip to content

Commit

Permalink
fix: array values are not converted to their field type
Browse files Browse the repository at this point in the history
  • Loading branch information
tauslim committed Mar 16, 2024
1 parent fd55eb9 commit 7e7a2f5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
23 changes: 11 additions & 12 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ var (
ErrBlocBracket = errors.New("bloc is a square bracket")
ErrParenthesisMalformed = errors.New("parenthesis bloc is malformed")
ErrUnregonizedBloc = errors.New("unrecognized bloc")
ErrSecondArgPairSqrBr = errors.New("second argument needs to be a single pair of square bracket array")
ErrInvalidPlacementSqrBrBloc = errors.New("invalid formation of square brackets bloc")
)

Expand Down Expand Up @@ -472,7 +471,7 @@ func getBlocNode(tb []TokenString) (*RqlNode, error) {

if isValue(tb) {
return nil, ErrBlocValue
} else if isOpSqrBrStyleBloc(tb) {
} else if isSqrBrStyleBloc(tb) {
var err error
n.Op = "group"
n.Args, err = parseArrArgs(tb)
Expand Down Expand Up @@ -529,19 +528,15 @@ func isFuncStyleBloc(tb []TokenString) bool {
}

func isSqrBrStyleBloc(tb []TokenString) bool {
return tb[0].t == Ident && (tb[1].t == Comma) && (tb[2].t == OpeningSquareBracket)
}

func isOpSqrBrStyleBloc(tb []TokenString) bool {
return tb[0].t == OpeningSquareBracket
return len(tb) >= 3 && tb[0].t == Ident && (tb[1].t == Comma) && (tb[2].t == OpeningSquareBracket)
}

func parseFuncArgs(tb []TokenString) (args []interface{}, err error) {
var argTokens [][]TokenString

if len(tb) > 3 && isSqrBrStyleBloc(tb) {
argTokens = append(argTokens, []TokenString{tb[0]})
argTokens = append(argTokens, tb[2:])
argTokens = append(argTokens, tb)
} else {
commaIdxs := findAllTokenIndexes(tb, Comma)
if len(commaIdxs) == 0 {
Expand Down Expand Up @@ -620,10 +615,14 @@ func parseArrArgs(tb []TokenString) (args []interface{}, err error) {
stack := make([]TokenString, 0)
count := 0

var subTs []TokenString
for i, b := range tb {
subTs := tb[:2]
arraySubTs := tb[2:]
for i, b := range arraySubTs {
if i == 0 && b.t != OpeningSquareBracket {
return nil, ErrSecondArgPairSqrBr
return nil, fmt.Errorf("second argument needs to be a single pair of square bracket array")
}
if i == len(arraySubTs)-1 && b.t != ClosingSquareBracket {
return nil, fmt.Errorf("last value is not enclosed with closing square bracket")
}

if b.t == OpeningSquareBracket {
Expand All @@ -640,7 +639,7 @@ func parseArrArgs(tb []TokenString) (args []interface{}, err error) {
}
}
if count != 1 {
return nil, fmt.Errorf("array values need to be enclosed in square brackets")
return nil, fmt.Errorf("array values need to be enclosed in a single pair square brackets")
}
if len(stack) != 0 {
return nil, ErrInvalidPlacementSqrBrBloc
Expand Down
5 changes: 4 additions & 1 deletion pkg/driver/cosmos/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ func (ct *Translator) GetSliceTranslatorFunc(op string, alterValueFunc AlterValu
if !ok {
return "", fmt.Errorf("expected group node but got %v", subArgs[0])
}
for _, a := range groupNode.Args {
if len(groupNode.Args) < 2 {
return "", fmt.Errorf("array of values not found")
}
for _, a := range groupNode.Args[1:] {
placeholder = fmt.Sprintf("@p%s", strconv.Itoa(len(ct.args)+1))
convertedValue, err := alterValueFunc(a)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/driver/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ func (mt *Translator) GetSliceTranslatorFunc(op string, alterValueFunc AlterValu
if !ok {
return "", fmt.Errorf("expected group node but got %v", subArgs[0])
}
for _, a := range groupNode.Args {
if len(groupNode.Args) < 2 {
return "", fmt.Errorf("array of values not found")
}
for _, a := range groupNode.Args[1:] {
convertedValue, err := alterValueFunc(a)
if err != nil {
return "", err
Expand Down
3 changes: 0 additions & 3 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ func (p *Parser) GetFieldValidationFunc() ValidationFunc {
n.Args[i] = newVal
}
case *RqlNode:
if v.Op == "group" {
continue
}
err = p.validateFields(v)
if err != nil {
return err
Expand Down

0 comments on commit 7e7a2f5

Please sign in to comment.