MCPcopy
hub / github.com/ent/ent / ParsePath

Function ParsePath

dialect/sql/sqljson/sqljson.go:607–653  ·  view source on GitHub ↗

ParsePath parses the "dotpath" for the DotPath option. "a.b" => ["a", "b"] "a[1][2]" => ["a", "[1]", "[2]"] "a.\"b.c\" => ["a", "\"b.c\""]

(dotpath string)

Source from the content-addressed store, hash-verified

605// "a[1][2]" => ["a", "[1]", "[2]"]
606// "a.\"b.c\" => ["a", "\"b.c\""]
607func ParsePath(dotpath string) ([]string, error) {
608 var (
609 i, p int
610 path []string
611 )
612 for i < len(dotpath) {
613 switch r := dotpath[i]; {
614 case r == '"':
615 if i == len(dotpath)-1 {
616 return nil, fmt.Errorf("unexpected quote")
617 }
618 idx := strings.IndexRune(dotpath[i+1:], '"')
619 if idx == -1 || idx == 0 {
620 return nil, fmt.Errorf("unbalanced quote")
621 }
622 i += idx + 2
623 case r == '[':
624 if p != i {
625 path = append(path, dotpath[p:i])
626 }
627 p = i
628 if i == len(dotpath)-1 {
629 return nil, fmt.Errorf("unexpected bracket")
630 }
631 idx := strings.IndexRune(dotpath[i:], ']')
632 if idx == -1 || idx == 1 {
633 return nil, fmt.Errorf("unbalanced bracket")
634 }
635 if !isNumber(dotpath[i+1 : i+idx]) {
636 return nil, fmt.Errorf("invalid index %q", dotpath[i:i+idx+1])
637 }
638 i += idx + 1
639 case r == '.' || r == ']':
640 if p != i {
641 path = append(path, dotpath[p:i])
642 }
643 i++
644 p = i
645 default:
646 i++
647 }
648 }
649 if p != i {
650 path = append(path, dotpath[p:i])
651 }
652 return path, nil
653}
654
655// normalizePG adds cast option to the JSON path is the argument type is
656// not string, in order to avoid "missing type casts" error in Postgres.

Callers 2

TestParsePathFunction · 0.92
DotPathFunction · 0.85

Calls 1

isNumberFunction · 0.85

Tested by 1

TestParsePathFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…