(path string)
| 50 | } |
| 51 | |
| 52 | func TransformPath(path string) string { |
| 53 | if path == "" { |
| 54 | return path |
| 55 | } |
| 56 | |
| 57 | legacyExtendedSyntaxRegEx := regexp.MustCompile(`(?i)([^\=]+)=([^\.\=\>\<\~]+)`) |
| 58 | hasFilterRegEx := regexp.MustCompile(`(?i)\[\?.*\)\]`) |
| 59 | indexXPathRegEx := regexp.MustCompile(`\/(\d+|\*)\/`) |
| 60 | trailingIndexXPathRegEx := regexp.MustCompile(`\/(\d+|\*)$`) |
| 61 | rootXPathRegEx := regexp.MustCompile(`^\/`) |
| 62 | numeric := regexp.MustCompile(`^\d+$`) |
| 63 | rewrittenPath := path |
| 64 | |
| 65 | if legacyExtendedSyntaxRegEx.MatchString(path) { |
| 66 | // Using property=value selectors |
| 67 | rewriteTokens := []string{} |
| 68 | tokens := strings.Split(path, ".") |
| 69 | for _, token := range tokens { |
| 70 | rewriteToken := token |
| 71 | if legacyExtendedSyntaxRegEx.MatchString(token) { |
| 72 | filterTokens := legacyExtendedSyntaxRegEx.FindStringSubmatch(token) |
| 73 | if numeric.MatchString((filterTokens[2])) { |
| 74 | rewriteToken = fmt.Sprintf("[?(@.%s=='%s' || @.%s==%s)]", filterTokens[1], filterTokens[2], filterTokens[1], filterTokens[2]) |
| 75 | } else { |
| 76 | rewriteToken = fmt.Sprintf("[?(@.%s=='%s')]", filterTokens[1], filterTokens[2]) |
| 77 | } |
| 78 | } |
| 79 | rewriteTokens = append(rewriteTokens, rewriteToken) |
| 80 | } |
| 81 | rewrittenPath = strings.Join(rewriteTokens, ".") |
| 82 | rewrittenPath = strings.ReplaceAll(rewrittenPath, ".[?", "[?") |
| 83 | } else if strings.Contains(path, "/") && !hasFilterRegEx.MatchString(path) { |
| 84 | // Is XPath |
| 85 | rewrittenPath = indexXPathRegEx.ReplaceAllString(path, "[$1].") |
| 86 | rewrittenPath = trailingIndexXPathRegEx.ReplaceAllString(rewrittenPath, "[$1]") |
| 87 | rewrittenPath = rootXPathRegEx.ReplaceAllLiteralString(rewrittenPath, "$.") |
| 88 | } |
| 89 | |
| 90 | return rewrittenPath |
| 91 | } |
no outgoing calls