Skip to content

Commit

Permalink
Add support for $regularExpression{pattern,options} syntax from pyMongo
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gibbs committed Nov 30, 2020
1 parent dc3764d commit 0f8f29f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion bson/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func init() {
funcExt.DecodeConst("undefined", Undefined)

jsonExt.DecodeKeyed("$regex", jdecRegEx)
// peter-gibbs - Handle canonical regex from python
jsonExt.DecodeKeyed("$regularExpression", jdecRegEx)
jsonExt.EncodeType(RegEx{}, jencRegEx)

funcExt.DecodeFunc("ObjectId", "$oidFunc", "Id")
Expand Down Expand Up @@ -244,7 +246,26 @@ func jdecRegEx(data []byte) (interface{}, error) {
if err != nil {
return nil, err
}
return RegEx{v.Regex, v.Options}, nil
if v.Regex != "" {
return RegEx{v.Regex, v.Options}, nil
}

// peter-gibbs - Handle canonical regex from python '$regularExpression{pattern,options}'
var vc struct {
Regex struct {
Pattern string `json:"pattern"`
Options string `json:"options"`
} `json:"$regularExpression"`
}
err = jdec(data, &vc)
if err != nil {
return nil, err
}
if vc.Regex.Pattern != "" {
return RegEx{vc.Regex.Pattern, vc.Regex.Options}, nil
}

return nil, fmt.Errorf("invalid regex object: %s", data)
}

func jencRegEx(v interface{}) ([]byte, error) {
Expand Down

0 comments on commit 0f8f29f

Please sign in to comment.