Skip to content
Merged
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
32 changes: 31 additions & 1 deletion jsonata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package jsonata
import (
"encoding/json"
"fmt"
"regexp"
"reflect"
"sync"
"time"
Expand Down Expand Up @@ -96,8 +97,9 @@ type Expr struct {
// not a valid JSONata expression, Compile returns an error
// of type jparse.Error.
func Compile(expr string) (*Expr, error) {
cleanExpr := replaceQuotesAndCommentsInPaths(expr)

node, err := jparse.Parse(expr)
node, err := jparse.Parse(cleanExpr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -379,3 +381,31 @@ func isLetter(r rune) bool {
func isDigit(r rune) bool {
return (r >= '0' && r <= '9') || unicode.IsDigit(r)
}

/*
enables:
- comments in jsonata code
- fields with any character in their name
*/

var (
reQuotedPath = regexp.MustCompile(`([A-Za-z\$\\*\` + "`" + `])\.[\"']([\s\S]+?)[\"']`)
reQuotedPathStart = regexp.MustCompile(`^[\"']([ \.0-9A-Za-z]+?)[\"']\.([A-Za-z\$\*\"\'])`)
commentsPath = regexp.MustCompile(`\/\*([\s\S]*?)\*\/`)
)

func replaceQuotesAndCommentsInPaths(s string) string {
if reQuotedPathStart.MatchString(s) {
s = reQuotedPathStart.ReplaceAllString(s, "`$1`.$2")
}

for reQuotedPath.MatchString(s) {
s = reQuotedPath.ReplaceAllString(s, "$1.`$2`")
}

for commentsPath.MatchString(s) {
s = commentsPath.ReplaceAllString(s, "")
}

return s
}