1
0
Fork 0

Change selector syntax

Old: /bananas/[0]/weight
New: .bananas#0.weight
This commit is contained in:
Gregory Eremin 2015-02-23 20:32:54 +07:00
parent 6f19875d32
commit a8137d5b87
1 changed files with 28 additions and 16 deletions

View File

@ -88,28 +88,40 @@ func parseSelectors(sels []string) map[string]*context {
}
func parseSelector(sel string) []expectation {
tmp := strings.Replace(sel, ".", "/.", -1)
tmp = strings.Replace(tmp, "#", "/#", -1)
parts := strings.Split(tmp[1:], "/")
exps := []expectation{}
parts := strings.Split(sel[1:], "/")
for _, part := range parts {
c := expectation{}
if len(part) > 2 && part[:1] == "[" && part[len(part)-1:] == "]" {
if len(part) < 2 {
panic("Invalid selector: " + sel)
} else if part[:1] == "." {
c.typ = object
} else if part[:1] == "#" {
c.typ = array
part = part[1 : len(part)-1]
if part == "*" {
} else {
panic("Invalid selector: " + sel)
}
if part[1:2] == "*" {
c.greedy = true
} else if i, err := strconv.ParseInt(part, 10, 64); err == nil {
if len(part) > 2 {
panic("Invalid selector: " + sel)
}
}
if c.greedy {
} else if c.typ == object {
c.key = part[1:]
} else if i, err := strconv.ParseInt(part[1:], 10, 64); err == nil {
c.index = i
} else {
panic("Array index should be numeric: " + part)
}
} else {
c.typ = object
if part == "*" {
c.greedy = true
} else {
c.key = part
}
}
exps = append(exps, c)
}