Repository navigation
Feats negative number for override.go #1749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,8 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/tidwall/gjson" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/tidwall/sjson" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "regexp" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -151,7 +153,9 @@ func checkConditions(jsonStr string, conditions []ConditionOperation, logic stri | |||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func checkSingleCondition(jsonStr string, condition ConditionOperation) (bool, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| value := gjson.Get(jsonStr, condition.Path) | ||||||||||||||||||||||||||||||||||||||||||||||||
| // 处理负数索引 | ||||||||||||||||||||||||||||||||||||||||||||||||
| path := processNegativeIndex(jsonStr, condition.Path) | ||||||||||||||||||||||||||||||||||||||||||||||||
| value := gjson.Get(jsonStr, path) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if !value.Exists() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if condition.PassMissingKey { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return true, nil | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -177,6 +181,37 @@ func checkSingleCondition(jsonStr string, condition ConditionOperation) (bool, e | |||||||||||||||||||||||||||||||||||||||||||||||
| return result, nil | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func processNegativeIndex(jsonStr string, path string) string { | ||||||||||||||||||||||||||||||||||||||||||||||||
| re := regexp.MustCompile(`\.(-\d+)`) | ||||||||||||||||||||||||||||||||||||||||||||||||
| matches := re.FindAllStringSubmatch(path, -1) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if len(matches) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return path | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| result := path | ||||||||||||||||||||||||||||||||||||||||||||||||
| for _, match := range matches { | ||||||||||||||||||||||||||||||||||||||||||||||||
| negIndex := match[1] | ||||||||||||||||||||||||||||||||||||||||||||||||
| index, _ := strconv.Atoi(negIndex) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| arrayPath := strings.Split(path, negIndex)[0] | ||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.HasSuffix(arrayPath, ".") { | ||||||||||||||||||||||||||||||||||||||||||||||||
| arrayPath = arrayPath[:len(arrayPath)-1] | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| array := gjson.Get(jsonStr, arrayPath) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if array.IsArray() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| length := len(array.Array()) | ||||||||||||||||||||||||||||||||||||||||||||||||
| actualIndex := length + index | ||||||||||||||||||||||||||||||||||||||||||||||||
| if actualIndex >= 0 && actualIndex < length { | ||||||||||||||||||||||||||||||||||||||||||||||||
| result = strings.Replace(result, match[0], "."+strconv.Itoa(actualIndex), 1) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // compareGjsonValues 直接比较两个gjson.Result,支持所有比较模式 | ||||||||||||||||||||||||||||||||||||||||||||||||
| func compareGjsonValues(jsonValue, targetValue gjson.Result, mode string) (bool, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| switch mode { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -274,21 +309,25 @@ func applyOperations(jsonStr string, operations []ParamOperation) (string, error | |||||||||||||||||||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||||||||||||||||||||
| continue // 条件不满足,跳过当前操作 | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| // 处理路径中的负数索引 | ||||||||||||||||||||||||||||||||||||||||||||||||
| opPath := processNegativeIndex(result, op.Path) | ||||||||||||||||||||||||||||||||||||||||||||||||
| opFrom := processNegativeIndex(result, op.From) | ||||||||||||||||||||||||||||||||||||||||||||||||
| opTo := processNegativeIndex(result, op.To) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+312
to
316
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate required fields after preprocessing. Prevent operations with empty paths (e.g., missing from/to for move) to avoid surprises like querying the root with empty path. opPath := processNegativeIndex(result, op.Path)
opFrom := processNegativeIndex(result, op.From)
opTo := processNegativeIndex(result, op.To)
+ // basic validation
+ switch op.Mode {
+ case "move":
+ if opFrom == "" || opTo == "" {
+ return "", fmt.Errorf("move requires both 'from' and 'to'")
+ }
+ default:
+ if opPath == "" {
+ return "", fmt.Errorf("%s requires non-empty 'path'", op.Mode)
+ }
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
| switch op.Mode { | ||||||||||||||||||||||||||||||||||||||||||||||||
| case "delete": | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = sjson.Delete(result, op.Path) | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = sjson.Delete(result, opPath) | ||||||||||||||||||||||||||||||||||||||||||||||||
| case "set": | ||||||||||||||||||||||||||||||||||||||||||||||||
| if op.KeepOrigin && gjson.Get(result, op.Path).Exists() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if op.KeepOrigin && gjson.Get(result, opPath).Exists() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = sjson.Set(result, op.Path, op.Value) | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = sjson.Set(result, opPath, op.Value) | ||||||||||||||||||||||||||||||||||||||||||||||||
| case "move": | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = moveValue(result, op.From, op.To) | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = moveValue(result, opFrom, opTo) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Moving into a descendant of source will delete the newly set value. Current order (set then delete) removes the destination when toPath is under fromPath. Handle descendant case by deleting first, then setting with the captured value. Proposed change in moveValue (outside this hunk): func moveValue(jsonStr, fromPath, toPath string) (string, error) {
sourceValue := gjson.Get(jsonStr, fromPath)
if !sourceValue.Exists() {
return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
}
- result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
- if err != nil {
- return "", err
- }
- return sjson.Delete(result, fromPath)
+ // Delete-first if destination is a descendant of source.
+ if toPath == fromPath || strings.HasPrefix(toPath, fromPath+".") {
+ tmp, err := sjson.Delete(jsonStr, fromPath)
+ if err != nil {
+ return "", err
+ }
+ return sjson.Set(tmp, toPath, sourceValue.Value())
+ }
+ result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
+ if err != nil {
+ return "", err
+ }
+ return sjson.Delete(result, fromPath)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
| case "prepend": | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = modifyValue(result, op.Path, op.Value, op.KeepOrigin, true) | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = modifyValue(result, opPath, op.Value, op.KeepOrigin, true) | ||||||||||||||||||||||||||||||||||||||||||||||||
| case "append": | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = modifyValue(result, op.Path, op.Value, op.KeepOrigin, false) | ||||||||||||||||||||||||||||||||||||||||||||||||
| result, err = modifyValue(result, opPath, op.Value, op.KeepOrigin, false) | ||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return "", fmt.Errorf("unknown operation: %s", op.Mode) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: nested/multiple negative indices can resolve incorrectly.
arrayPath is computed from the original path and may still contain unresolved negative indices, breaking lookups for subsequent matches (e.g., a.-1.b.-2). Iterate left-to-right on the progressively updated result and compute arrayPath via match indices.
Apply this diff: