Skip to content

Commit

Permalink
feat: remove redundant code for translator functions in mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
tauslim committed Feb 28, 2024
1 parent 70e7e67 commit a128513
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions pkg/driver/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,15 @@ func (mt *Translator) GetJoinTranslatorOpFunc(op string) driver.TranslatorOpFunc
var ops []string
for _, a := range n.Args {
switch v := a.(type) {
case string:
if !isValidField(v) {
return "", fmt.Errorf("invalid field name : %s", v)
}
s = s + v
case *gorql.RqlNode:
var tempS string
tempS, err = mt.where(v)
if err != nil {
return "", err
}
ops = append(ops, tempS)
default:
return "", fmt.Errorf("%s operation need query as arguments", op)
}
}
return fmt.Sprintf(`{"$%s": [%s]}`, op, strings.Join(ops, ", ")), nil
Expand All @@ -190,31 +187,21 @@ func (mt *Translator) GetFieldValueTranslatorFunc(op string, alterValueFunc Alte
sep := ""
for i, a := range n.Args {
s += sep
switch v := a.(type) {
case *gorql.RqlNode:
var tempS string
tempS, err = mt.where(v)
var tempS string
if i == 0 {
if isValidField(a.(string)) {
tempS = quote(a.(string))
} else {
return "", fmt.Errorf("first argument must be a valid field name (arg: %v)", a)
}
} else {
convertedValue, err := alterValueFunc(a)
if err != nil {
return "", err
}
s = s + tempS
default:
var tempS string
if i == 0 {
if isValidField(v.(string)) {
tempS = quote(v.(string))
} else {
return "", fmt.Errorf("first argument must be a valid field name (arg: %s)", v)
}
} else {
convertedValue, err := alterValueFunc(v)
if err != nil {
return "", err
}
s += fmt.Sprintf(`{"$%s": %v}`, op, convertedValue)
}
s += tempS
s += fmt.Sprintf(`{"$%s": %v}`, op, convertedValue)
}
s += tempS
sep = fmt.Sprintf(`: `)
}
return fmt.Sprintf(`{%s}`, s), nil
Expand Down

0 comments on commit a128513

Please sign in to comment.