Begin to rework the json parsing
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/PaesslerAG/jsonpath"
|
||||
)
|
||||
|
||||
// findMatchingPaths finds nodes in a JSON document that match the given JSONPath
|
||||
// fi ndMatchingPaths finds nodes in a JSON document that match the given JSONPath
|
||||
func findMatchingPaths(jsonDoc interface{}, path string) ([]interface{}, error) {
|
||||
// Use the existing jsonpath library to extract values
|
||||
result, err := jsonpath.Get(path, jsonDoc)
|
||||
@@ -57,7 +57,7 @@ func TestJSONProcessor_Process_NumericValues(t *testing.T) {
|
||||
}`
|
||||
|
||||
p := &JSONProcessor{}
|
||||
result, modCount, matchCount, err := p.ProcessContent(content, "$.books[*].price", "v=v*2")
|
||||
result, modCount, matchCount, err := p.ProcessContent(content, "$.books[*]", "v.price=v.price*2")
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
@@ -71,12 +71,45 @@ func TestJSONProcessor_Process_NumericValues(t *testing.T) {
|
||||
t.Errorf("Expected 2 modifications, got %d", modCount)
|
||||
}
|
||||
|
||||
// Normalize whitespace for comparison
|
||||
normalizedResult := normalizeWhitespace(result)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
// Compare parsed JSON objects instead of formatted strings
|
||||
var resultObj map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(result), &resultObj); err != nil {
|
||||
t.Fatalf("Failed to parse result JSON: %v", err)
|
||||
}
|
||||
|
||||
if normalizedResult != normalizedExpected {
|
||||
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||
var expectedObj map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(expected), &expectedObj); err != nil {
|
||||
t.Fatalf("Failed to parse expected JSON: %v", err)
|
||||
}
|
||||
|
||||
// Compare the first book's price
|
||||
resultBooks, ok := resultObj["books"].([]interface{})
|
||||
if !ok || len(resultBooks) < 1 {
|
||||
t.Fatalf("Expected books array in result")
|
||||
}
|
||||
resultBook1, ok := resultBooks[0].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("Expected first book to be an object")
|
||||
}
|
||||
resultPrice1, ok := resultBook1["price"].(float64)
|
||||
if !ok {
|
||||
t.Fatalf("Expected numeric price in first book")
|
||||
}
|
||||
if resultPrice1 != 89.9 {
|
||||
t.Errorf("Expected first book price to be 89.9, got %v", resultPrice1)
|
||||
}
|
||||
|
||||
// Compare the second book's price
|
||||
resultBook2, ok := resultBooks[1].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("Expected second book to be an object")
|
||||
}
|
||||
resultPrice2, ok := resultBook2["price"].(float64)
|
||||
if !ok {
|
||||
t.Fatalf("Expected numeric price in second book")
|
||||
}
|
||||
if resultPrice2 != 11.9 {
|
||||
t.Errorf("Expected second book price to be 11.9, got %v", resultPrice2)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,12 +124,15 @@ func TestJSONProcessor_Process_StringValues(t *testing.T) {
|
||||
}`
|
||||
|
||||
p := &JSONProcessor{}
|
||||
result, modCount, matchCount, err := p.ProcessContent(content, "$.config.*", "v=v*2")
|
||||
|
||||
result, modCount, matchCount, err := p.ProcessContent(content, "$.config", "for k,vi in pairs(v) do v[k]=vi*2 end")
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing content: %v", err)
|
||||
}
|
||||
|
||||
// Debug info
|
||||
t.Logf("Result: %s", result)
|
||||
t.Logf("Match count: %d, Mod count: %d", matchCount, modCount)
|
||||
|
||||
if matchCount != 3 {
|
||||
t.Errorf("Expected 3 matches, got %d", matchCount)
|
||||
}
|
||||
|
Reference in New Issue
Block a user