Skip to content

Commit

Permalink
feat: supports configuring multiple converters
Browse files Browse the repository at this point in the history
  • Loading branch information
qeesung committed May 13, 2024
1 parent 2101c56 commit 3ebd574
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
6 changes: 3 additions & 3 deletions mql.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func exprToWhereClause(e expr, fValidators map[string]validator, opt ...Option)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
switch {
case opts.withValidateConvertColumn == v.column && !isNil(opts.withValidateConvertFn):
return opts.withValidateConvertFn(v.column, v.comparisonOp, v.value)
switch validateConvertFn, ok := opts.withValidateConvertFns[v.column]; {
case ok && !isNil(validateConvertFn):
return validateConvertFn(v.column, v.comparisonOp, v.value)
default:
columnName := strings.ToLower(v.column)
if n, ok := opts.withColumnMap[columnName]; ok {
Expand Down
35 changes: 35 additions & 0 deletions mql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,41 @@ func TestParse(t *testing.T) {
Args: []any{"success-WithConverter: alice", "[email protected]", 21},
},
},
{
name: "success-WithMultiConverters",
query: "(name = \"alice\" and email=\"[email protected]\") or age > 21",
model: testModel{},
opts: []mql.Option{
mql.WithConverter(
"name",
func(columnName string, comparisonOp mql.ComparisonOp, value *string) (*mql.WhereClause, error) {
return &mql.WhereClause{
// intentionally not the correct condition and
// args, but this makes verifying the test
// easier.
Condition: fmt.Sprintf("success-WithConverter: %s%s?", columnName, comparisonOp),
Args: []any{"success-WithConverter: alice"},
}, nil
},
),
mql.WithConverter(
"email",
func(columnName string, comparisonOp mql.ComparisonOp, value *string) (*mql.WhereClause, error) {
return &mql.WhereClause{
// intentionally not the correct condition and
// args, but this makes verifying the test
// easier.
Condition: fmt.Sprintf("success-WithConverter: %s%s?", columnName, comparisonOp),
Args: []any{"success-WithConverter: email=\"[email protected]\""},
}, nil
},
),
},
want: &mql.WhereClause{
Condition: "((success-WithConverter: name=? and success-WithConverter: email=?) or age>?)",
Args: []any{"success-WithConverter: alice", "success-WithConverter: email=\"[email protected]\"", 21},
},
},
{
name: "err-ignored-field-used-in-query",
query: "email=\"[email protected]\" or name=\"alice\"",
Expand Down
17 changes: 8 additions & 9 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
)

type options struct {
withSkipWhitespace bool
withColumnMap map[string]string
withValidateConvertFn ValidateConvertFunc
withValidateConvertColumn string
withIgnoredFields []string
withPgPlaceholder bool
withSkipWhitespace bool
withColumnMap map[string]string
withValidateConvertFns map[string]ValidateConvertFunc
withIgnoredFields []string
withPgPlaceholder bool
}

// Option - how options are passed as args
type Option func(*options) error

func getDefaultOptions() options {
return options{
withColumnMap: make(map[string]string),
withColumnMap: make(map[string]string),
withValidateConvertFns: make(map[string]ValidateConvertFunc),
}
}

Expand Down Expand Up @@ -67,8 +67,7 @@ func WithConverter(fieldName string, fn ValidateConvertFunc) Option {
return func(o *options) error {
switch {
case fieldName != "" && !isNil(fn):
o.withValidateConvertFn = fn
o.withValidateConvertColumn = fieldName
o.withValidateConvertFns[fieldName] = fn
case fieldName == "" && !isNil(fn):
return fmt.Errorf("%s: missing field name: %w", op, ErrInvalidParameter)
case fieldName != "" && isNil(fn):
Expand Down

0 comments on commit 3ebd574

Please sign in to comment.