Compare commits
36 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb14087598 | |||
66a522aa12 | |||
1a4b4f76f2 | |||
2bfd9f951e | |||
e5092edf53 | |||
e31c0e4e8f | |||
73d93367a0 | |||
64f690f6b4 | |||
34477b2c34 | |||
d5c08d86f5 | |||
68127fe453 | |||
872f2dd46d | |||
4eed05c7c2 | |||
4640281fbf | |||
aba10267d1 | |||
fed140254b | |||
db92033642 | |||
1b0b198297 | |||
15ae116447 | |||
1bcc6735ab | |||
20bab894e3 | |||
396992b3d0 | |||
054dcbf835 | |||
88887b9a12 | |||
533a563dc5 | |||
7fc2956b6d | |||
6f9f3f5eae | |||
d2419b761e | |||
ab98800ca0 | |||
0f7ee521ac | |||
fd81861a64 | |||
430234dd3b | |||
17bb3d4f71 | |||
84e0a8bed6 | |||
bdcf096fdd | |||
1a90046c89 |
9
.vscode/launch.json
vendored
9
.vscode/launch.json
vendored
@@ -9,8 +9,13 @@
|
|||||||
"type": "go",
|
"type": "go",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"program": "${fileDirname}",
|
"program": "${workspaceFolder}",
|
||||||
"args": []
|
"args": [
|
||||||
|
"-mode=json",
|
||||||
|
"$..name",
|
||||||
|
"v='pero'",
|
||||||
|
"test.json"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
116
README.md
Normal file
116
README.md
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
# Big Chef
|
||||||
|
|
||||||
|
A Go-based tool for modifying XML, JSON, and text documents using XPath/JSONPath/Regex expressions and Lua transformations.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Multi-Format Processing**:
|
||||||
|
- XML (XPath)
|
||||||
|
- JSON (JSONPath)
|
||||||
|
- Text (Regex)
|
||||||
|
- **Node Value Modification**: Update text values in XML elements, JSON properties or text matches
|
||||||
|
- **Attribute Manipulation**: Modify XML attributes, JSON object keys or regex capture groups
|
||||||
|
- **Conditional Logic**: Apply transformations based on document content
|
||||||
|
- **Complex Operations**:
|
||||||
|
- Mathematical calculations
|
||||||
|
- String manipulations
|
||||||
|
- Date conversions
|
||||||
|
- Structural changes
|
||||||
|
- Whole ass Lua environment
|
||||||
|
- **Error Handling**: Comprehensive error detection for:
|
||||||
|
- Invalid XML/JSON
|
||||||
|
- Malformed XPath/JSONPath
|
||||||
|
- Lua syntax errors
|
||||||
|
|
||||||
|
## Usage Examples
|
||||||
|
|
||||||
|
### 1. Basic field modification
|
||||||
|
```xml
|
||||||
|
<!-- Input -->
|
||||||
|
<price>44.95</price>
|
||||||
|
|
||||||
|
<!-- Command -->
|
||||||
|
chef -xml "//price" "v=v*2" input.xml
|
||||||
|
|
||||||
|
<!-- Output -->
|
||||||
|
<price>89.9</price>
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Supports glob patterns
|
||||||
|
```xml
|
||||||
|
chef -xml "//price" "v=v*2" data/**.xml
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Attribute Update
|
||||||
|
```xml
|
||||||
|
<!-- Input -->
|
||||||
|
<item price="10.50"/>
|
||||||
|
|
||||||
|
<!-- Command -->
|
||||||
|
chef -xml "//item/@price" "v=v*2" input.xml
|
||||||
|
|
||||||
|
<!-- Output -->
|
||||||
|
<item price="21"/>
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. JSONPath Transformation
|
||||||
|
```json
|
||||||
|
// Input
|
||||||
|
{
|
||||||
|
"products": [
|
||||||
|
{"name": "Widget", "price": 19.99},
|
||||||
|
{"name": "Gadget", "price": 29.99}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command
|
||||||
|
chef -json "$.products[*].price" "v=v*0.75" input.json
|
||||||
|
|
||||||
|
// Output
|
||||||
|
{
|
||||||
|
"products": [
|
||||||
|
{"name": "Widget", "price": 14.99},
|
||||||
|
{"name": "Gadget", "price": 22.49}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Regex Text Replacement
|
||||||
|
Regex works slightly differently, up to 12 match groups are provided as v1..v12 and s1..s12 for numbers and strings respectively.
|
||||||
|
A special shorthand "!num" is also provided that simply expands to `(\d*\.?\d+)`.
|
||||||
|
```xml
|
||||||
|
<!-- Input -->
|
||||||
|
<description>Price: $15.00 Special Offer</description>
|
||||||
|
|
||||||
|
<!-- Command -->
|
||||||
|
chef "Price: $!num Special Offer" "v1 = v1 * 0.92" input.xml
|
||||||
|
|
||||||
|
<!-- Output -->
|
||||||
|
<description>Price: $13.80 Special Offer</description>
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Conditional Transformation
|
||||||
|
```xml
|
||||||
|
<!-- Input -->
|
||||||
|
<item stock="5" price="10.00"/>
|
||||||
|
|
||||||
|
<!-- Command -->
|
||||||
|
chef -xml "//item" "if tonumber(v.stock) > 0 then v.price = v.price * 0.8 end" input.xml
|
||||||
|
|
||||||
|
<!-- Output -->
|
||||||
|
<item stock="5" price="8.00"/>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go build -o chef main.go
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Process XML file
|
||||||
|
./chef -xml "//price" "v=v*1.2" input.xml
|
||||||
|
|
||||||
|
# Process JSON file
|
||||||
|
./chef -json "$.prices[*]" "v=v*0.9" input.json
|
||||||
|
```
|
14
go.mod
14
go.mod
@@ -2,9 +2,17 @@ module modify
|
|||||||
|
|
||||||
go 1.24.1
|
go 1.24.1
|
||||||
|
|
||||||
require github.com/Knetic/govaluate v3.0.0+incompatible
|
require (
|
||||||
|
github.com/PaesslerAG/jsonpath v0.1.1
|
||||||
|
github.com/antchfx/xmlquery v1.4.4
|
||||||
|
github.com/bmatcuk/doublestar/v4 v4.8.1
|
||||||
|
github.com/yuin/gopher-lua v1.1.1
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bmatcuk/doublestar/v4 v4.8.1 // indirect
|
github.com/PaesslerAG/gval v1.0.0 // indirect
|
||||||
github.com/yuin/gopher-lua v1.1.1 // indirect
|
github.com/antchfx/xpath v1.3.3 // indirect
|
||||||
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
|
golang.org/x/net v0.33.0 // indirect
|
||||||
|
golang.org/x/text v0.21.0 // indirect
|
||||||
)
|
)
|
||||||
|
80
go.sum
80
go.sum
@@ -1,6 +1,82 @@
|
|||||||
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
|
github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8=
|
||||||
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
|
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
|
||||||
|
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
|
||||||
|
github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
|
||||||
|
github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
|
||||||
|
github.com/antchfx/xmlquery v1.4.4 h1:mxMEkdYP3pjKSftxss4nUHfjBhnMk4imGoR96FRY2dg=
|
||||||
|
github.com/antchfx/xmlquery v1.4.4/go.mod h1:AEPEEPYE9GnA2mj5Ur2L5Q5/2PycJ0N9Fusrx9b12fc=
|
||||||
|
github.com/antchfx/xpath v1.3.3 h1:tmuPQa1Uye0Ym1Zn65vxPgfltWb/Lxu2jeqIGteJSRs=
|
||||||
|
github.com/antchfx/xpath v1.3.3/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
|
||||||
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
|
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
|
||||||
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
||||||
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
|
||||||
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||||
|
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||||
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
|
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||||
|
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||||
|
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||||
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
|
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||||
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||||
|
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||||
|
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
|
||||||
|
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||||
|
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
|
||||||
|
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||||
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||||
|
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
533
main.go
533
main.go
@@ -3,85 +3,71 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/bmatcuk/doublestar/v4"
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
lua "github.com/yuin/gopher-lua"
|
|
||||||
|
"modify/processor"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Error *log.Logger
|
|
||||||
var Warning *log.Logger
|
|
||||||
var Info *log.Logger
|
|
||||||
var Success *log.Logger
|
|
||||||
|
|
||||||
// ModificationRecord tracks a single value modification
|
|
||||||
type ModificationRecord struct {
|
|
||||||
File string
|
|
||||||
OldValue string
|
|
||||||
NewValue string
|
|
||||||
Operation string
|
|
||||||
Context string
|
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalStats tracks all modifications across files
|
|
||||||
type GlobalStats struct {
|
type GlobalStats struct {
|
||||||
TotalMatches int
|
TotalMatches int
|
||||||
TotalModifications int
|
TotalModifications int
|
||||||
Modifications []ModificationRecord
|
|
||||||
ProcessedFiles int
|
ProcessedFiles int
|
||||||
FailedFiles int
|
FailedFiles int
|
||||||
}
|
}
|
||||||
|
|
||||||
var stats GlobalStats
|
var stats GlobalStats
|
||||||
|
var logger *log.Logger
|
||||||
|
|
||||||
|
var (
|
||||||
|
jsonFlag = flag.Bool("json", false, "Process JSON files")
|
||||||
|
xmlFlag = flag.Bool("xml", false, "Process XML files")
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Configure standard logging to be hidden by default
|
|
||||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||||
log.SetOutput(io.Discard) // Disable default logging to stdout
|
logger = log.New(os.Stdout, "", log.Lmicroseconds|log.Lshortfile)
|
||||||
|
|
||||||
// Set up custom loggers with different severity levels
|
stats = GlobalStats{}
|
||||||
Error = log.New(io.MultiWriter(os.Stderr, os.Stdout),
|
|
||||||
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
|
|
||||||
log.Lmicroseconds|log.Lshortfile)
|
|
||||||
|
|
||||||
Warning = log.New(os.Stdout,
|
|
||||||
fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
|
|
||||||
log.Lmicroseconds|log.Lshortfile)
|
|
||||||
|
|
||||||
Info = log.New(os.Stdout,
|
|
||||||
fmt.Sprintf("%sInfo:%s ", "\033[0;94m", "\033[0m"),
|
|
||||||
log.Lmicroseconds|log.Lshortfile)
|
|
||||||
|
|
||||||
Success = log.New(os.Stdout,
|
|
||||||
fmt.Sprintf("%s✨ SUCCESS:%s ", "\033[0;92m", "\033[0m"),
|
|
||||||
log.Lmicroseconds|log.Lshortfile)
|
|
||||||
|
|
||||||
// Initialize global stats
|
|
||||||
stats = GlobalStats{
|
|
||||||
Modifications: make([]ModificationRecord, 0),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Define flags
|
// TODO: Implement some sort of git integration
|
||||||
|
// Maybe use go-git
|
||||||
|
// Specify a -git flag
|
||||||
|
// If we are operating with git then:
|
||||||
|
// Inmitialize a repo if one doesn't exist (try to open right?)
|
||||||
|
// For each file matched by glob first figure out if it's already tracked
|
||||||
|
// If not tracked then track it and commit (either it alone or maybe multiple together somehow)
|
||||||
|
// Then reset the file (to undo previous modifications)
|
||||||
|
// THEN change the file
|
||||||
|
// In addition add a -undo flag that will ONLY reset the files without changing them
|
||||||
|
// Only for the ones matched by glob
|
||||||
|
// ^ important because binary files would fuck us up
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage: %s <regex_with_capture_groups> <lua_expression> <...files_or_globs>\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] <pattern> <lua_expression> <...files_or_globs>\n", os.Args[0])
|
||||||
|
fmt.Fprintf(os.Stderr, "\nOptions:\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " -json\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " Process JSON files\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " -xml\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " Process XML files\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " -mode string\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " Processing mode: regex, xml, json (default \"regex\")\n")
|
||||||
fmt.Fprintf(os.Stderr, "\nExamples:\n")
|
fmt.Fprintf(os.Stderr, "\nExamples:\n")
|
||||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" data.xml\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, " Regex mode (default):\n")
|
||||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" \"*.xml\"\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" data.xml\n", os.Args[0])
|
||||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>,(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, " XML mode:\n")
|
||||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"=0\" data.xml\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, " %s -xml \"//value\" \"*1.5\" data.xml\n", os.Args[0])
|
||||||
|
fmt.Fprintf(os.Stderr, " JSON mode:\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " %s -json \"$.items[*].value\" \"*1.5\" data.json\n", os.Args[0])
|
||||||
fmt.Fprintf(os.Stderr, "\nNote: v1, v2, etc. are used to refer to capture groups as numbers.\n")
|
fmt.Fprintf(os.Stderr, "\nNote: v1, v2, etc. are used to refer to capture groups as numbers.\n")
|
||||||
fmt.Fprintf(os.Stderr, " s1, s2, etc. are used to refer to capture groups as strings.\n")
|
fmt.Fprintf(os.Stderr, " s1, s2, etc. are used to refer to capture groups as strings.\n")
|
||||||
fmt.Fprintf(os.Stderr, " Helper functions: num(str) converts string to number, str(num) converts number to string\n")
|
fmt.Fprintf(os.Stderr, " Helper functions: num(str) converts string to number, str(num) converts number to string\n")
|
||||||
fmt.Fprintf(os.Stderr, " is_number(str) checks if a string is numeric\n")
|
fmt.Fprintf(os.Stderr, " is_number(str) checks if a string is numeric\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " For XML and JSON, the captured values are exposed as 'v', which can be of any type we capture (string, number, table).\n")
|
||||||
fmt.Fprintf(os.Stderr, " If expression starts with an operator like *, /, +, -, =, etc., v1 is automatically prepended\n")
|
fmt.Fprintf(os.Stderr, " If expression starts with an operator like *, /, +, -, =, etc., v1 is automatically prepended\n")
|
||||||
fmt.Fprintf(os.Stderr, " You can use any valid Lua code, including if statements, loops, etc.\n")
|
fmt.Fprintf(os.Stderr, " You can use any valid Lua code, including if statements, loops, etc.\n")
|
||||||
fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
|
fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
|
||||||
@@ -89,61 +75,55 @@ func main() {
|
|||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
||||||
if len(args) < 3 {
|
if len(args) < 3 {
|
||||||
Error.Println("Insufficient arguments - need regex pattern, lua expression, and at least one file or glob pattern")
|
log.Printf("At least %d arguments are required", 3)
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
regexPattern := args[0]
|
// Get the appropriate pattern and expression based on mode
|
||||||
luaExpr := args[1]
|
var pattern, luaExpr string
|
||||||
filePatterns := args[2:]
|
var filePatterns []string
|
||||||
|
|
||||||
|
pattern = args[0]
|
||||||
|
luaExpr = args[1]
|
||||||
|
filePatterns = args[2:]
|
||||||
|
|
||||||
|
// Prepare the Lua expression
|
||||||
|
originalLuaExpr := luaExpr
|
||||||
|
luaExpr = processor.BuildLuaScript(luaExpr)
|
||||||
|
if originalLuaExpr != luaExpr {
|
||||||
|
logger.Printf("Transformed Lua expression from %q to %q", originalLuaExpr, luaExpr)
|
||||||
|
}
|
||||||
|
|
||||||
// Expand file patterns with glob support
|
// Expand file patterns with glob support
|
||||||
files, err := expandFilePatterns(filePatterns)
|
files, err := expandFilePatterns(filePatterns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("Error expanding file patterns: %v", err)
|
fmt.Fprintf(os.Stderr, "Error expanding file patterns: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
Error.Println("No files found matching the specified patterns")
|
fmt.Fprintf(os.Stderr, "No files found matching the specified patterns\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Info.Printf("Starting modifier with pattern '%s', expression '%s' on %d files", regexPattern, luaExpr, len(files))
|
// Create the processor based on mode
|
||||||
|
var proc processor.Processor
|
||||||
// Prepare the Lua expression
|
switch {
|
||||||
originalLuaExpr := luaExpr
|
case *xmlFlag:
|
||||||
luaExpr = buildLuaScript(luaExpr)
|
proc = &processor.XMLProcessor{}
|
||||||
if originalLuaExpr != luaExpr {
|
logger.Printf("Starting XML modifier with XPath %q, expression %q on %d files",
|
||||||
Info.Printf("Transformed Lua expression from '%s' to '%s'", originalLuaExpr, luaExpr)
|
pattern, luaExpr, len(files))
|
||||||
}
|
case *jsonFlag:
|
||||||
|
proc = &processor.JSONProcessor{}
|
||||||
// Handle special pattern modifications
|
logger.Printf("Starting JSON modifier with JSONPath %q, expression %q on %d files",
|
||||||
originalPattern := regexPattern
|
pattern, luaExpr, len(files))
|
||||||
patternModified := false
|
default:
|
||||||
|
proc = &processor.RegexProcessor{}
|
||||||
if strings.Contains(regexPattern, "!num") {
|
logger.Printf("Starting regex modifier with pattern %q, expression %q on %d files",
|
||||||
regexPattern = strings.ReplaceAll(regexPattern, "!num", "(-?\\d*\\.?\\d+)")
|
pattern, luaExpr, len(files))
|
||||||
patternModified = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure the regex can match across multiple lines by adding (?s) flag
|
|
||||||
if !strings.HasPrefix(regexPattern, "(?s)") {
|
|
||||||
regexPattern = "(?s)" + regexPattern
|
|
||||||
patternModified = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if patternModified {
|
|
||||||
Info.Printf("Modified regex pattern from '%s' to '%s'", originalPattern, regexPattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compile the pattern for file processing
|
|
||||||
pattern, err := regexp.Compile(regexPattern)
|
|
||||||
if err != nil {
|
|
||||||
Error.Printf("Invalid regex pattern '%s': %v", regexPattern, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
@@ -152,375 +132,30 @@ func main() {
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(file string) {
|
go func(file string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
Info.Printf("🔄 Processing file: %s", file)
|
logger.Printf("Processing file: %s", file)
|
||||||
err := processFile(file, pattern, luaExpr, originalLuaExpr)
|
|
||||||
|
// It's a bit fucked, maybe I could do better to call it from proc... But it'll do for now
|
||||||
|
modCount, matchCount, err := processor.Process(proc, file, pattern, luaExpr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("❌ Failed to process file %s: %v", file, err)
|
fmt.Fprintf(os.Stderr, "Failed to process file %s: %v\n", file, err)
|
||||||
stats.FailedFiles++
|
stats.FailedFiles++
|
||||||
} else {
|
} else {
|
||||||
Info.Printf("✅ Successfully processed file: %s", file)
|
logger.Printf("Successfully processed file: %s", file)
|
||||||
stats.ProcessedFiles++
|
stats.ProcessedFiles++
|
||||||
|
stats.TotalMatches += matchCount
|
||||||
|
stats.TotalModifications += modCount
|
||||||
}
|
}
|
||||||
}(file)
|
}(file)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
// Print summary of all modifications
|
// Print summary
|
||||||
printSummary(originalLuaExpr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// printSummary outputs a formatted summary of all modifications made
|
|
||||||
func printSummary(operation string) {
|
|
||||||
if stats.TotalModifications == 0 {
|
if stats.TotalModifications == 0 {
|
||||||
Warning.Printf("No modifications were made in any files")
|
fmt.Fprintf(os.Stderr, "No modifications were made in any files\n")
|
||||||
return
|
} else {
|
||||||
|
fmt.Printf("Operation complete! Modified %d values in %d/%d files\n",
|
||||||
|
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
Success.Printf("Operation complete! Modified %d values in %d/%d files using '%s'",
|
|
||||||
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles, operation)
|
|
||||||
|
|
||||||
// Group modifications by file for better readability
|
|
||||||
fileGroups := make(map[string][]ModificationRecord)
|
|
||||||
for _, mod := range stats.Modifications {
|
|
||||||
fileGroups[mod.File] = append(fileGroups[mod.File], mod)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print modifications by file
|
|
||||||
for file, mods := range fileGroups {
|
|
||||||
Success.Printf("📄 %s: %d modifications", file, len(mods))
|
|
||||||
|
|
||||||
// Show some sample modifications (max 5 per file to avoid overwhelming output)
|
|
||||||
maxSamples := 5
|
|
||||||
if len(mods) > maxSamples {
|
|
||||||
for i := 0; i < maxSamples; i++ {
|
|
||||||
mod := mods[i]
|
|
||||||
Success.Printf(" %d. '%s' → '%s' %s",
|
|
||||||
i+1, limitString(mod.OldValue, 20), limitString(mod.NewValue, 20), mod.Context)
|
|
||||||
}
|
|
||||||
Success.Printf(" ... and %d more modifications", len(mods)-maxSamples)
|
|
||||||
} else {
|
|
||||||
for i, mod := range mods {
|
|
||||||
Success.Printf(" %d. '%s' → '%s' %s",
|
|
||||||
i+1, limitString(mod.OldValue, 20), limitString(mod.NewValue, 20), mod.Context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print a nice visual indicator of success
|
|
||||||
if stats.TotalModifications > 0 {
|
|
||||||
Success.Printf("🎉 All done! Modified %d values successfully!", stats.TotalModifications)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildLuaScript creates a complete Lua script from the expression
|
|
||||||
func buildLuaScript(luaExpr string) string {
|
|
||||||
// Track if we modified the expression
|
|
||||||
modified := false
|
|
||||||
original := luaExpr
|
|
||||||
|
|
||||||
// Auto-prepend v1 for expressions starting with operators
|
|
||||||
if strings.HasPrefix(luaExpr, "*") ||
|
|
||||||
strings.HasPrefix(luaExpr, "/") ||
|
|
||||||
strings.HasPrefix(luaExpr, "+") ||
|
|
||||||
strings.HasPrefix(luaExpr, "-") ||
|
|
||||||
strings.HasPrefix(luaExpr, "^") ||
|
|
||||||
strings.HasPrefix(luaExpr, "%") {
|
|
||||||
luaExpr = "v1 = v1" + luaExpr
|
|
||||||
modified = true
|
|
||||||
} else if strings.HasPrefix(luaExpr, "=") {
|
|
||||||
// Handle direct assignment with = operator
|
|
||||||
luaExpr = "v1 " + luaExpr
|
|
||||||
modified = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add assignment if needed
|
|
||||||
if !strings.Contains(luaExpr, "=") {
|
|
||||||
luaExpr = "v1 = " + luaExpr
|
|
||||||
modified = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace shorthand v[] and s[] with their direct variable names
|
|
||||||
newExpr := strings.ReplaceAll(luaExpr, "v[1]", "v1")
|
|
||||||
newExpr = strings.ReplaceAll(newExpr, "v[2]", "v2")
|
|
||||||
newExpr = strings.ReplaceAll(newExpr, "s[1]", "s1")
|
|
||||||
newExpr = strings.ReplaceAll(newExpr, "s[2]", "s2")
|
|
||||||
|
|
||||||
if newExpr != luaExpr {
|
|
||||||
luaExpr = newExpr
|
|
||||||
modified = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if modified {
|
|
||||||
Info.Printf("Transformed Lua expression: '%s' → '%s'", original, luaExpr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return luaExpr
|
|
||||||
}
|
|
||||||
|
|
||||||
func processFile(filename string, pattern *regexp.Regexp, luaExpr string, originalExpr string) error {
|
|
||||||
fullPath := filepath.Join(".", filename)
|
|
||||||
|
|
||||||
// Read file content
|
|
||||||
content, err := os.ReadFile(fullPath)
|
|
||||||
if err != nil {
|
|
||||||
Error.Printf("Cannot read file %s: %v", fullPath, err)
|
|
||||||
return fmt.Errorf("error reading file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fileContent := string(content)
|
|
||||||
Info.Printf("File %s loaded: %d bytes", fullPath, len(content))
|
|
||||||
|
|
||||||
// Process the content
|
|
||||||
result, modificationCount, matchCount, err := process(fileContent, pattern, luaExpr, filename, originalExpr)
|
|
||||||
if err != nil {
|
|
||||||
Error.Printf("Processing failed for %s: %v", fullPath, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update global stats
|
|
||||||
stats.TotalMatches += matchCount
|
|
||||||
stats.TotalModifications += modificationCount
|
|
||||||
|
|
||||||
if modificationCount == 0 {
|
|
||||||
Warning.Printf("No modifications made to %s - pattern didn't match any content", fullPath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the modified content back
|
|
||||||
err = os.WriteFile(fullPath, []byte(result), 0644)
|
|
||||||
if err != nil {
|
|
||||||
Error.Printf("Failed to save changes to %s: %v", fullPath, err)
|
|
||||||
return fmt.Errorf("error writing file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
Info.Printf("Made %d modifications to %s and saved (%d bytes)",
|
|
||||||
modificationCount, fullPath, len(result))
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func process(data string, pattern *regexp.Regexp, luaExpr string, filename string, originalExpr string) (string, int, int, error) {
|
|
||||||
L := lua.NewState()
|
|
||||||
defer L.Close()
|
|
||||||
|
|
||||||
// Initialize Lua environment
|
|
||||||
modificationCount := 0
|
|
||||||
matchCount := 0
|
|
||||||
|
|
||||||
// Load math library
|
|
||||||
L.Push(L.GetGlobal("require"))
|
|
||||||
L.Push(lua.LString("math"))
|
|
||||||
if err := L.PCall(1, 1, nil); err != nil {
|
|
||||||
Error.Printf("Failed to load Lua math library: %v", err)
|
|
||||||
return data, 0, 0, fmt.Errorf("error loading Lua math library: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize helper functions
|
|
||||||
helperScript := `
|
|
||||||
-- Custom Lua helpers for math operations
|
|
||||||
function min(a, b) return math.min(a, b) end
|
|
||||||
function max(a, b) return math.max(a, b) end
|
|
||||||
function round(x) return math.floor(x + 0.5) end
|
|
||||||
function floor(x) return math.floor(x) end
|
|
||||||
function ceil(x) return math.ceil(x) end
|
|
||||||
|
|
||||||
-- String to number conversion helper
|
|
||||||
function num(str)
|
|
||||||
return tonumber(str) or 0
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Number to string conversion
|
|
||||||
function str(num)
|
|
||||||
return tostring(num)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Check if string is numeric
|
|
||||||
function is_number(str)
|
|
||||||
return tonumber(str) ~= nil
|
|
||||||
end
|
|
||||||
`
|
|
||||||
if err := L.DoString(helperScript); err != nil {
|
|
||||||
Error.Printf("Failed to load Lua helper functions: %v", err)
|
|
||||||
return data, 0, 0, fmt.Errorf("error loading helper functions: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process all regex matches
|
|
||||||
result := pattern.ReplaceAllStringFunc(data, func(match string) string {
|
|
||||||
matchCount++
|
|
||||||
captures := pattern.FindStringSubmatch(match)
|
|
||||||
if len(captures) <= 1 {
|
|
||||||
// No capture groups, return unchanged
|
|
||||||
Warning.Printf("Match found but no capture groups: %s", limitString(match, 50))
|
|
||||||
return match
|
|
||||||
}
|
|
||||||
Info.Printf("Match found: %s", limitString(match, 50))
|
|
||||||
|
|
||||||
// Set up global variables v1, v2, etc. for the Lua context
|
|
||||||
captureValues := make([]string, len(captures)-1)
|
|
||||||
for i, capture := range captures[1:] {
|
|
||||||
captureValues[i] = capture
|
|
||||||
// Set the raw string value with s prefix
|
|
||||||
L.SetGlobal(fmt.Sprintf("s%d", i+1), lua.LString(capture))
|
|
||||||
|
|
||||||
// Also set numeric version with v prefix if possible
|
|
||||||
floatVal, err := strconv.ParseFloat(capture, 64)
|
|
||||||
if err == nil {
|
|
||||||
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LNumber(floatVal))
|
|
||||||
} else {
|
|
||||||
// For non-numeric values, set v also to the string value
|
|
||||||
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LString(capture))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute the user's Lua code
|
|
||||||
if err := L.DoString(luaExpr); err != nil {
|
|
||||||
Error.Printf("Lua execution failed for match '%s': %v", limitString(match, 50), err)
|
|
||||||
return match // Return unchanged on error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the modified values after Lua execution
|
|
||||||
modifications := make(map[int]string)
|
|
||||||
for i := 0; i < len(captures)-1 && i < 12; i++ {
|
|
||||||
// Check both v and s variables to see if any were modified
|
|
||||||
vVarName := fmt.Sprintf("v%d", i+1)
|
|
||||||
sVarName := fmt.Sprintf("s%d", i+1)
|
|
||||||
|
|
||||||
// First check the v-prefixed numeric variable
|
|
||||||
vLuaVal := L.GetGlobal(vVarName)
|
|
||||||
sLuaVal := L.GetGlobal(sVarName)
|
|
||||||
|
|
||||||
oldVal := captures[i+1]
|
|
||||||
var newVal string
|
|
||||||
var useModification bool
|
|
||||||
|
|
||||||
// First priority: check if the string variable was modified
|
|
||||||
if sLuaVal != lua.LNil {
|
|
||||||
if sStr, ok := sLuaVal.(lua.LString); ok {
|
|
||||||
newStrVal := string(sStr)
|
|
||||||
if newStrVal != oldVal {
|
|
||||||
newVal = newStrVal
|
|
||||||
useModification = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Second priority: if string wasn't modified, check numeric variable
|
|
||||||
if !useModification && vLuaVal != lua.LNil {
|
|
||||||
switch v := vLuaVal.(type) {
|
|
||||||
case lua.LNumber:
|
|
||||||
newNumVal := strconv.FormatFloat(float64(v), 'f', -1, 64)
|
|
||||||
if newNumVal != oldVal {
|
|
||||||
newVal = newNumVal
|
|
||||||
useModification = true
|
|
||||||
}
|
|
||||||
case lua.LString:
|
|
||||||
newStrVal := string(v)
|
|
||||||
if newStrVal != oldVal {
|
|
||||||
newVal = newStrVal
|
|
||||||
useModification = true
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
newDefaultVal := fmt.Sprintf("%v", v)
|
|
||||||
if newDefaultVal != oldVal {
|
|
||||||
newVal = newDefaultVal
|
|
||||||
useModification = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record the modification if anything changed
|
|
||||||
if useModification {
|
|
||||||
modifications[i] = newVal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply modifications to the matched text
|
|
||||||
if len(modifications) == 0 {
|
|
||||||
return match // No changes
|
|
||||||
}
|
|
||||||
|
|
||||||
result := match
|
|
||||||
for i, newVal := range modifications {
|
|
||||||
oldVal := captures[i+1]
|
|
||||||
// Special handling for empty capture groups
|
|
||||||
if oldVal == "" {
|
|
||||||
// Find the position where the empty capture group should be
|
|
||||||
// by analyzing the regex pattern and current match
|
|
||||||
parts := pattern.SubexpNames()
|
|
||||||
if i+1 < len(parts) && parts[i+1] != "" {
|
|
||||||
// Named capture groups
|
|
||||||
subPattern := fmt.Sprintf("(?P<%s>)", parts[i+1])
|
|
||||||
emptyGroupPattern := regexp.MustCompile(subPattern)
|
|
||||||
if loc := emptyGroupPattern.FindStringIndex(result); loc != nil {
|
|
||||||
// Insert the new value at the capture group location
|
|
||||||
result = result[:loc[0]] + newVal + result[loc[1]:]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// For unnamed capture groups, we need to find where they would be in the regex
|
|
||||||
// This is a simplification that might not work for complex regex patterns
|
|
||||||
// but should handle the test case with <value></value>
|
|
||||||
tagPattern := regexp.MustCompile("<value></value>")
|
|
||||||
if loc := tagPattern.FindStringIndex(result); loc != nil {
|
|
||||||
// Replace the empty tag content with our new value
|
|
||||||
result = result[:loc[0]+7] + newVal + result[loc[1]-8:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Normal replacement for non-empty capture groups
|
|
||||||
result = strings.Replace(result, oldVal, newVal, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract a bit of context from the match for better reporting
|
|
||||||
contextStart := max(0, strings.Index(match, oldVal)-10)
|
|
||||||
contextLength := min(30, len(match)-contextStart)
|
|
||||||
if contextStart+contextLength > len(match) {
|
|
||||||
contextLength = len(match) - contextStart
|
|
||||||
}
|
|
||||||
contextStr := "..." + match[contextStart:contextStart+contextLength] + "..."
|
|
||||||
|
|
||||||
// Log the modification
|
|
||||||
Info.Printf("Modified value [%d]: '%s' → '%s'", i+1, limitString(oldVal, 30), limitString(newVal, 30))
|
|
||||||
|
|
||||||
// Record the modification for summary
|
|
||||||
stats.Modifications = append(stats.Modifications, ModificationRecord{
|
|
||||||
File: filename,
|
|
||||||
OldValue: oldVal,
|
|
||||||
NewValue: newVal,
|
|
||||||
Operation: originalExpr,
|
|
||||||
Context: fmt.Sprintf("(in %s)", limitString(contextStr, 30)),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
modificationCount++
|
|
||||||
return result
|
|
||||||
})
|
|
||||||
|
|
||||||
return result, modificationCount, matchCount, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// limitString truncates a string to maxLen and adds "..." if truncated
|
|
||||||
func limitString(s string, maxLen int) string {
|
|
||||||
s = strings.ReplaceAll(s, "\n", "\\n")
|
|
||||||
if len(s) <= maxLen {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
return s[:maxLen-3] + "..."
|
|
||||||
}
|
|
||||||
|
|
||||||
// max returns the maximum of two integers
|
|
||||||
func max(a, b int) int {
|
|
||||||
if a > b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// min returns the minimum of two integers
|
|
||||||
func min(a, b int) int {
|
|
||||||
if a < b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func expandFilePatterns(patterns []string) ([]string, error) {
|
func expandFilePatterns(patterns []string) ([]string, error) {
|
||||||
@@ -537,7 +172,7 @@ func expandFilePatterns(patterns []string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(files) > 0 {
|
if len(files) > 0 {
|
||||||
Info.Printf("Found %d files to process", len(files))
|
logger.Printf("Found %d files to process", len(files))
|
||||||
}
|
}
|
||||||
return files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
1136
main_test.go
1136
main_test.go
File diff suppressed because it is too large
Load Diff
165
processor/json.go
Normal file
165
processor/json.go
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"modify/processor/jsonpath"
|
||||||
|
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
// JSONProcessor implements the Processor interface for JSON documents
|
||||||
|
type JSONProcessor struct{}
|
||||||
|
|
||||||
|
// ProcessContent implements the Processor interface for JSONProcessor
|
||||||
|
func (p *JSONProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) {
|
||||||
|
// Parse JSON document
|
||||||
|
var jsonData interface{}
|
||||||
|
err := json.Unmarshal([]byte(content), &jsonData)
|
||||||
|
if err != nil {
|
||||||
|
return content, 0, 0, fmt.Errorf("error parsing JSON: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find nodes matching the JSONPath pattern
|
||||||
|
nodes, err := jsonpath.Get(jsonData, pattern)
|
||||||
|
if err != nil {
|
||||||
|
return content, 0, 0, fmt.Errorf("error getting nodes: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
matchCount := len(nodes)
|
||||||
|
if matchCount == 0 {
|
||||||
|
return content, 0, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
modCount := 0
|
||||||
|
for _, node := range nodes {
|
||||||
|
log.Printf("Processing node at path: %s with value: %v", node.Path, node.Value)
|
||||||
|
|
||||||
|
// Initialize Lua
|
||||||
|
L, err := NewLuaState()
|
||||||
|
if err != nil {
|
||||||
|
return content, len(nodes), 0, fmt.Errorf("error creating Lua state: %v", err)
|
||||||
|
}
|
||||||
|
defer L.Close()
|
||||||
|
log.Println("Lua state initialized successfully.")
|
||||||
|
|
||||||
|
err = p.ToLua(L, node.Value)
|
||||||
|
if err != nil {
|
||||||
|
return content, len(nodes), 0, fmt.Errorf("error converting to Lua: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Converted node value to Lua: %v", node.Value)
|
||||||
|
|
||||||
|
originalScript := luaExpr
|
||||||
|
fullScript := BuildLuaScript(luaExpr)
|
||||||
|
log.Printf("Original script: %q, Full script: %q", originalScript, fullScript)
|
||||||
|
|
||||||
|
// Execute Lua script
|
||||||
|
log.Printf("Executing Lua script: %q", fullScript)
|
||||||
|
if err := L.DoString(fullScript); err != nil {
|
||||||
|
return content, len(nodes), 0, fmt.Errorf("error executing Lua %q: %v", fullScript, err)
|
||||||
|
}
|
||||||
|
log.Println("Lua script executed successfully.")
|
||||||
|
|
||||||
|
// Get modified value
|
||||||
|
result, err := p.FromLua(L)
|
||||||
|
if err != nil {
|
||||||
|
return content, len(nodes), 0, fmt.Errorf("error getting result from Lua: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Retrieved modified value from Lua: %v", result)
|
||||||
|
|
||||||
|
modified := false
|
||||||
|
modified = L.GetGlobal("modified").String() == "true"
|
||||||
|
if !modified {
|
||||||
|
log.Printf("No changes made to node at path: %s", node.Path)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the modification to the JSON data
|
||||||
|
err = p.updateJSONValue(jsonData, node.Path, result)
|
||||||
|
if err != nil {
|
||||||
|
return content, len(nodes), 0, fmt.Errorf("error updating JSON: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Updated JSON at path: %s with new value: %v", node.Path, result)
|
||||||
|
modCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the modified JSON back to a string with same formatting
|
||||||
|
var jsonBytes []byte
|
||||||
|
jsonBytes, err = json.MarshalIndent(jsonData, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return content, modCount, matchCount, fmt.Errorf("error marshalling JSON: %v", err)
|
||||||
|
}
|
||||||
|
return string(jsonBytes), modCount, matchCount, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateJSONValue updates a value in the JSON structure based on its JSONPath
|
||||||
|
func (p *JSONProcessor) updateJSONValue(jsonData interface{}, path string, newValue interface{}) error {
|
||||||
|
// Special handling for root node
|
||||||
|
if path == "$" {
|
||||||
|
// For the root node, we'll copy the value to the jsonData reference
|
||||||
|
// This is a special case since we can't directly replace the interface{} variable
|
||||||
|
|
||||||
|
// We need to handle different types of root elements
|
||||||
|
switch rootValue := newValue.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
// For objects, we need to copy over all keys
|
||||||
|
rootMap, ok := jsonData.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
// If the original wasn't a map, completely replace it with the new map
|
||||||
|
// This is handled by the jsonpath.Set function
|
||||||
|
return jsonpath.Set(jsonData, path, newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the original map
|
||||||
|
for k := range rootMap {
|
||||||
|
delete(rootMap, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy all keys from the new map
|
||||||
|
for k, v := range rootValue {
|
||||||
|
rootMap[k] = v
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case []interface{}:
|
||||||
|
// For arrays, we need to handle similarly
|
||||||
|
rootArray, ok := jsonData.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
// If the original wasn't an array, use jsonpath.Set
|
||||||
|
return jsonpath.Set(jsonData, path, newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear and recreate the array
|
||||||
|
*&rootArray = rootValue
|
||||||
|
return nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
// For other types, use jsonpath.Set
|
||||||
|
return jsonpath.Set(jsonData, path, newValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For non-root paths, use the regular Set method
|
||||||
|
err := jsonpath.Set(jsonData, path, newValue)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update JSON value at path '%s': %w", path, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToLua converts JSON values to Lua variables
|
||||||
|
func (p *JSONProcessor) ToLua(L *lua.LState, data interface{}) error {
|
||||||
|
table, err := ToLua(L, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
L.SetGlobal("v", table)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromLua retrieves values from Lua
|
||||||
|
func (p *JSONProcessor) FromLua(L *lua.LState) (interface{}, error) {
|
||||||
|
luaValue := L.GetGlobal("v")
|
||||||
|
return FromLua(L, luaValue)
|
||||||
|
}
|
1771
processor/json_test.go
Normal file
1771
processor/json_test.go
Normal file
File diff suppressed because it is too large
Load Diff
490
processor/jsonpath/jsonpath.go
Normal file
490
processor/jsonpath/jsonpath.go
Normal file
@@ -0,0 +1,490 @@
|
|||||||
|
package jsonpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// JSONStep represents a single step in a JSONPath query
|
||||||
|
type JSONStep struct {
|
||||||
|
Type StepType
|
||||||
|
Key string // For Child/RecursiveDescent
|
||||||
|
Index int // For Index (use -1 for wildcard "*")
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSONNode represents a value in the JSON data with its path
|
||||||
|
type JSONNode struct {
|
||||||
|
Value interface{} // The value found at the path
|
||||||
|
Path string // The exact JSONPath where the value was found
|
||||||
|
}
|
||||||
|
|
||||||
|
// StepType defines the types of steps in a JSONPath
|
||||||
|
type StepType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
RootStep StepType = iota // $ - The root element
|
||||||
|
ChildStep // .key - Direct child access
|
||||||
|
RecursiveDescentStep // ..key - Recursive search for key
|
||||||
|
WildcardStep // .* - All children of an object
|
||||||
|
IndexStep // [n] - Array index access (or [*] for all elements)
|
||||||
|
)
|
||||||
|
|
||||||
|
// TraversalMode determines how the traversal behaves
|
||||||
|
type TraversalMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
CollectMode TraversalMode = iota // Just collect matched nodes
|
||||||
|
ModifyFirstMode // Modify first matching node
|
||||||
|
ModifyAllMode // Modify all matching nodes
|
||||||
|
)
|
||||||
|
|
||||||
|
// ParseJSONPath parses a JSONPath string into a sequence of steps
|
||||||
|
func ParseJSONPath(path string) ([]JSONStep, error) {
|
||||||
|
if len(path) == 0 || path[0] != '$' {
|
||||||
|
return nil, fmt.Errorf("path must start with $; received: %q", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
steps := []JSONStep{}
|
||||||
|
i := 0
|
||||||
|
|
||||||
|
for i < len(path) {
|
||||||
|
switch path[i] {
|
||||||
|
case '$':
|
||||||
|
steps = append(steps, JSONStep{Type: RootStep})
|
||||||
|
i++
|
||||||
|
case '.':
|
||||||
|
i++
|
||||||
|
if i < len(path) && path[i] == '.' {
|
||||||
|
// Recursive descent
|
||||||
|
i++
|
||||||
|
key, nextPos := readKey(path, i)
|
||||||
|
steps = append(steps, JSONStep{Type: RecursiveDescentStep, Key: key})
|
||||||
|
i = nextPos
|
||||||
|
} else {
|
||||||
|
// Child step or wildcard
|
||||||
|
key, nextPos := readKey(path, i)
|
||||||
|
if key == "*" {
|
||||||
|
steps = append(steps, JSONStep{Type: WildcardStep})
|
||||||
|
} else {
|
||||||
|
steps = append(steps, JSONStep{Type: ChildStep, Key: key})
|
||||||
|
}
|
||||||
|
i = nextPos
|
||||||
|
}
|
||||||
|
case '[':
|
||||||
|
// Index step
|
||||||
|
i++
|
||||||
|
indexStr, nextPos := readIndex(path, i)
|
||||||
|
if indexStr == "*" {
|
||||||
|
steps = append(steps, JSONStep{Type: IndexStep, Index: -1})
|
||||||
|
} else {
|
||||||
|
index, err := strconv.Atoi(indexStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid index: %s; error: %w", indexStr, err)
|
||||||
|
}
|
||||||
|
steps = append(steps, JSONStep{Type: IndexStep, Index: index})
|
||||||
|
}
|
||||||
|
i = nextPos + 1 // Skip closing ]
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unexpected character: %c at position %d; path: %q", path[i], i, path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return steps, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readKey extracts a key name from the path
|
||||||
|
func readKey(path string, start int) (string, int) {
|
||||||
|
i := start
|
||||||
|
for ; i < len(path); i++ {
|
||||||
|
if path[i] == '.' || path[i] == '[' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return path[start:i], i
|
||||||
|
}
|
||||||
|
|
||||||
|
// readIndex extracts an array index or wildcard from the path
|
||||||
|
func readIndex(path string, start int) (string, int) {
|
||||||
|
i := start
|
||||||
|
for ; i < len(path); i++ {
|
||||||
|
if path[i] == ']' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return path[start:i], i
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get retrieves values with their paths from data at the specified JSONPath
|
||||||
|
// Each returned JSONNode contains both the value and its exact path in the data structure
|
||||||
|
func Get(data interface{}, path string) ([]JSONNode, error) {
|
||||||
|
steps, err := ParseJSONPath(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse JSONPath %q: %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := []JSONNode{}
|
||||||
|
err = traverseWithPaths(data, steps, &results, "$")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to traverse JSONPath %q: %w", path, err)
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set updates the value at the specified JSONPath in the original data structure.
|
||||||
|
// It only modifies the first matching node.
|
||||||
|
func Set(data interface{}, path string, value interface{}) error {
|
||||||
|
steps, err := ParseJSONPath(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse JSONPath %q: %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
success := false
|
||||||
|
err = setWithPath(data, steps, &success, value, "$", ModifyFirstMode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", path, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAll updates all matching values at the specified JSONPath.
|
||||||
|
func SetAll(data interface{}, path string, value interface{}) error {
|
||||||
|
steps, err := ParseJSONPath(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse JSONPath %q: %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
success := false
|
||||||
|
err = setWithPath(data, steps, &success, value, "$", ModifyAllMode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", path, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// setWithPath modifies values while tracking paths
|
||||||
|
func setWithPath(node interface{}, steps []JSONStep, success *bool, value interface{}, currentPath string, mode TraversalMode) error {
|
||||||
|
if node == nil || *success && mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip root step
|
||||||
|
actualSteps := steps
|
||||||
|
if len(steps) > 0 && steps[0].Type == RootStep {
|
||||||
|
actualSteps = steps[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have no steps left, we're setting the root value
|
||||||
|
if len(actualSteps) == 0 {
|
||||||
|
// For the root node, we need to handle it differently depending on what's passed in
|
||||||
|
// since we can't directly replace the interface{} variable
|
||||||
|
|
||||||
|
// We'll signal success and let the JSONProcessor handle updating the root
|
||||||
|
*success = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the first step
|
||||||
|
step := actualSteps[0]
|
||||||
|
remainingSteps := actualSteps[1:]
|
||||||
|
isLastStep := len(remainingSteps) == 0
|
||||||
|
|
||||||
|
switch step.Type {
|
||||||
|
case ChildStep:
|
||||||
|
m, ok := node.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("node at path %q is not a map; actual type: %T", currentPath, node)
|
||||||
|
}
|
||||||
|
|
||||||
|
childPath := currentPath + "." + step.Key
|
||||||
|
|
||||||
|
if isLastStep {
|
||||||
|
// We've reached the target, set the value
|
||||||
|
m[step.Key] = value
|
||||||
|
*success = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create intermediate nodes if necessary
|
||||||
|
child, exists := m[step.Key]
|
||||||
|
if !exists {
|
||||||
|
// Create missing intermediate node
|
||||||
|
if len(remainingSteps) > 0 && remainingSteps[0].Type == IndexStep {
|
||||||
|
child = []interface{}{}
|
||||||
|
} else {
|
||||||
|
child = map[string]interface{}{}
|
||||||
|
}
|
||||||
|
m[step.Key] = child
|
||||||
|
}
|
||||||
|
|
||||||
|
err := setWithPath(child, remainingSteps, success, value, childPath, mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
case IndexStep:
|
||||||
|
arr, ok := node.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("node at path %q is not an array; actual type: %T", currentPath, node)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle wildcard index
|
||||||
|
if step.Index == -1 {
|
||||||
|
for i, item := range arr {
|
||||||
|
itemPath := fmt.Sprintf("%s[%d]", currentPath, i)
|
||||||
|
if isLastStep {
|
||||||
|
arr[i] = value
|
||||||
|
*success = true
|
||||||
|
if mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := setWithPath(item, remainingSteps, success, value, itemPath, mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", itemPath, err)
|
||||||
|
}
|
||||||
|
if *success && mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle specific index
|
||||||
|
if step.Index >= 0 && step.Index < len(arr) {
|
||||||
|
item := arr[step.Index]
|
||||||
|
itemPath := fmt.Sprintf("%s[%d]", currentPath, step.Index)
|
||||||
|
if isLastStep {
|
||||||
|
arr[step.Index] = value
|
||||||
|
*success = true
|
||||||
|
} else {
|
||||||
|
err := setWithPath(item, remainingSteps, success, value, itemPath, mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", itemPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case RecursiveDescentStep:
|
||||||
|
// For recursive descent, first check direct match at this level
|
||||||
|
if m, ok := node.(map[string]interface{}); ok && step.Key != "*" {
|
||||||
|
if val, exists := m[step.Key]; exists {
|
||||||
|
directPath := currentPath + "." + step.Key
|
||||||
|
if isLastStep {
|
||||||
|
m[step.Key] = value
|
||||||
|
*success = true
|
||||||
|
if mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := setWithPath(val, remainingSteps, success, value, directPath, mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", directPath, err)
|
||||||
|
}
|
||||||
|
if *success && mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then continue recursion to all children
|
||||||
|
switch n := node.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
for k, v := range n {
|
||||||
|
childPath := currentPath + "." + k
|
||||||
|
// Skip keys we've already processed directly
|
||||||
|
if step.Key != "*" && k == step.Key {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err := setWithPath(v, steps, success, value, childPath, mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
if *success && mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
for i, v := range n {
|
||||||
|
childPath := fmt.Sprintf("%s[%d]", currentPath, i)
|
||||||
|
err := setWithPath(v, steps, success, value, childPath, mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
if *success && mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case WildcardStep:
|
||||||
|
m, ok := node.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("node at path %q is not a map; actual type: %T", currentPath, node)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
childPath := currentPath + "." + k
|
||||||
|
if isLastStep {
|
||||||
|
m[k] = value
|
||||||
|
*success = true
|
||||||
|
if mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := setWithPath(v, remainingSteps, success, value, childPath, mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set value at JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
if *success && mode == ModifyFirstMode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// traverseWithPaths tracks both nodes and their paths during traversal
|
||||||
|
func traverseWithPaths(node interface{}, steps []JSONStep, results *[]JSONNode, currentPath string) error {
|
||||||
|
if len(steps) == 0 || node == nil {
|
||||||
|
return fmt.Errorf("cannot traverse with empty steps or nil node; steps length: %d, node: %v", len(steps), node)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip root step
|
||||||
|
actualSteps := steps
|
||||||
|
if steps[0].Type == RootStep {
|
||||||
|
if len(steps) == 1 {
|
||||||
|
*results = append(*results, JSONNode{Value: node, Path: currentPath})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
actualSteps = steps[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the first step
|
||||||
|
step := actualSteps[0]
|
||||||
|
remainingSteps := actualSteps[1:]
|
||||||
|
isLastStep := len(remainingSteps) == 0
|
||||||
|
|
||||||
|
switch step.Type {
|
||||||
|
case ChildStep:
|
||||||
|
m, ok := node.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("node is not a map; actual type: %T", node)
|
||||||
|
}
|
||||||
|
|
||||||
|
child, exists := m[step.Key]
|
||||||
|
if !exists {
|
||||||
|
return fmt.Errorf("key not found: %s in node at path: %s", step.Key, currentPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
childPath := currentPath + "." + step.Key
|
||||||
|
if isLastStep {
|
||||||
|
*results = append(*results, JSONNode{Value: child, Path: childPath})
|
||||||
|
} else {
|
||||||
|
err := traverseWithPaths(child, remainingSteps, results, childPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to traverse JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case IndexStep:
|
||||||
|
arr, ok := node.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("node is not an array; actual type: %T", node)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle wildcard index
|
||||||
|
if step.Index == -1 {
|
||||||
|
for i, item := range arr {
|
||||||
|
itemPath := fmt.Sprintf("%s[%d]", currentPath, i)
|
||||||
|
if isLastStep {
|
||||||
|
*results = append(*results, JSONNode{Value: item, Path: itemPath})
|
||||||
|
} else {
|
||||||
|
err := traverseWithPaths(item, remainingSteps, results, itemPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to traverse JSONPath %q: %w", itemPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle specific index
|
||||||
|
if step.Index >= 0 && step.Index < len(arr) {
|
||||||
|
item := arr[step.Index]
|
||||||
|
itemPath := fmt.Sprintf("%s[%d]", currentPath, step.Index)
|
||||||
|
if isLastStep {
|
||||||
|
*results = append(*results, JSONNode{Value: item, Path: itemPath})
|
||||||
|
} else {
|
||||||
|
err := traverseWithPaths(item, remainingSteps, results, itemPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to traverse JSONPath %q: %w", itemPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("index %d out of bounds for array at path: %s", step.Index, currentPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
case RecursiveDescentStep:
|
||||||
|
// For recursive descent, first check direct match at this level
|
||||||
|
if m, ok := node.(map[string]interface{}); ok && step.Key != "*" {
|
||||||
|
if val, exists := m[step.Key]; exists {
|
||||||
|
directPath := currentPath + "." + step.Key
|
||||||
|
if isLastStep {
|
||||||
|
*results = append(*results, JSONNode{Value: val, Path: directPath})
|
||||||
|
} else {
|
||||||
|
err := traverseWithPaths(val, remainingSteps, results, directPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to traverse JSONPath %q: %w", directPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For wildcard, collect this node
|
||||||
|
if step.Key == "*" && isLastStep {
|
||||||
|
*results = append(*results, JSONNode{Value: node, Path: currentPath})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then continue recursion to all children
|
||||||
|
switch n := node.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
for k, v := range n {
|
||||||
|
childPath := currentPath + "." + k
|
||||||
|
err := traverseWithPaths(v, steps, results, childPath) // Use the same steps
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to traverse JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
for i, v := range n {
|
||||||
|
childPath := fmt.Sprintf("%s[%d]", currentPath, i)
|
||||||
|
err := traverseWithPaths(v, steps, results, childPath) // Use the same steps
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to traverse JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case WildcardStep:
|
||||||
|
m, ok := node.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("node is not a map; actual type: %T", node)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
childPath := currentPath + "." + k
|
||||||
|
if isLastStep {
|
||||||
|
*results = append(*results, JSONNode{Value: v, Path: childPath})
|
||||||
|
} else {
|
||||||
|
err := traverseWithPaths(v, remainingSteps, results, childPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to traverse JSONPath %q: %w", childPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
577
processor/jsonpath/jsonpath_get_set_test.go
Normal file
577
processor/jsonpath/jsonpath_get_set_test.go
Normal file
@@ -0,0 +1,577 @@
|
|||||||
|
package jsonpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetWithPathsBasic(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
data map[string]interface{}
|
||||||
|
path string
|
||||||
|
expected []JSONNode
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple property",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30,
|
||||||
|
},
|
||||||
|
path: "$.name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "John", Path: "$.name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nested property",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$.user.name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "John", Path: "$.user.name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "array access",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"users": []interface{}{
|
||||||
|
map[string]interface{}{"name": "John", "age": 30},
|
||||||
|
map[string]interface{}{"name": "Jane", "age": 25},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$.users[1].name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "Jane", Path: "$.users[1].name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wildcard",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"users": []interface{}{
|
||||||
|
map[string]interface{}{"name": "John", "age": 30},
|
||||||
|
map[string]interface{}{"name": "Jane", "age": 25},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$.users[*].name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "John", Path: "$.users[0].name"},
|
||||||
|
{Value: "Jane", Path: "$.users[1].name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "recursive descent",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"profile": map[string]interface{}{
|
||||||
|
"email": "john@example.com",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"admin": map[string]interface{}{
|
||||||
|
"email": "admin@example.com",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$..email",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "john@example.com", Path: "$.user.profile.email"},
|
||||||
|
{Value: "admin@example.com", Path: "$.admin.email"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nonexistent path",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$.user.email",
|
||||||
|
expected: []JSONNode{},
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := Get(tt.data, tt.path)
|
||||||
|
if err != nil {
|
||||||
|
if !tt.error {
|
||||||
|
t.Errorf("GetWithPaths() returned error: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For nonexistent path, we expect empty slice
|
||||||
|
if tt.name == "nonexistent path" {
|
||||||
|
if len(result) > 0 {
|
||||||
|
t.Errorf("GetWithPaths() returned %v, expected empty result", result)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if lengths match
|
||||||
|
if len(result) != len(tt.expected) {
|
||||||
|
t.Errorf("GetWithPaths() returned %d items, expected %d", len(result), len(tt.expected))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For wildcard results, we need to check containment rather than exact order
|
||||||
|
if tt.name == "wildcard" || tt.name == "recursive descent" {
|
||||||
|
// For each expected item, check if it exists in the results by both value and path
|
||||||
|
for _, expected := range tt.expected {
|
||||||
|
found := false
|
||||||
|
for _, r := range result {
|
||||||
|
if reflect.DeepEqual(r.Value, expected.Value) && r.Path == expected.Path {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("GetWithPaths() missing expected value: %v with path: %s", expected.Value, expected.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Otherwise check exact equality of both values and paths
|
||||||
|
for i, expected := range tt.expected {
|
||||||
|
if !reflect.DeepEqual(result[i].Value, expected.Value) {
|
||||||
|
t.Errorf("GetWithPaths() value at [%d] = %v, expected %v", i, result[i].Value, expected.Value)
|
||||||
|
}
|
||||||
|
if result[i].Path != expected.Path {
|
||||||
|
t.Errorf("GetWithPaths() path at [%d] = %s, expected %s", i, result[i].Path, expected.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSet(t *testing.T) {
|
||||||
|
t.Run("simple property", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30,
|
||||||
|
}
|
||||||
|
err := Set(data, "$.name", "Jane")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if data["name"] != "Jane" {
|
||||||
|
t.Errorf("Set() failed: expected name to be 'Jane', got %v", data["name"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("nested property", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := Set(data, "$.user.name", "Jane")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, ok := data["user"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User is not a map")
|
||||||
|
}
|
||||||
|
if user["name"] != "Jane" {
|
||||||
|
t.Errorf("Set() failed: expected user.name to be 'Jane', got %v", user["name"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("array element", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"users": []interface{}{
|
||||||
|
map[string]interface{}{"name": "John", "age": 30},
|
||||||
|
map[string]interface{}{"name": "Jane", "age": 25},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := Set(data, "$.users[0].name", "Bob")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
users, ok := data["users"].([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Users is not a slice")
|
||||||
|
}
|
||||||
|
user0, ok := users[0].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User is not a map")
|
||||||
|
}
|
||||||
|
if user0["name"] != "Bob" {
|
||||||
|
t.Errorf("Set() failed: expected users[0].name to be 'Bob', got %v", user0["name"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("complex value", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"profile": map[string]interface{}{
|
||||||
|
"email": "john@example.com",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
newProfile := map[string]interface{}{
|
||||||
|
"email": "john.doe@example.com",
|
||||||
|
"phone": "123-456-7890",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Set(data, "$.user.profile", newProfile)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userMap, ok := data["user"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User is not a map")
|
||||||
|
}
|
||||||
|
|
||||||
|
profile, ok := userMap["profile"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Profile is not a map")
|
||||||
|
}
|
||||||
|
|
||||||
|
if profile["email"] != "john.doe@example.com" || profile["phone"] != "123-456-7890" {
|
||||||
|
t.Errorf("Set() failed: expected profile to be updated with new values")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("create new property", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Set(data, "$.user.email", "john@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userMap, ok := data["user"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User is not a map")
|
||||||
|
}
|
||||||
|
|
||||||
|
if email, exists := userMap["email"]; !exists || email != "john@example.com" {
|
||||||
|
t.Errorf("Set() failed: expected user.email to be 'john@example.com', got %v", userMap["email"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("create nested properties", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Set(data, "$.user.contact.email", "john@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userMap, ok := data["user"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User is not a map")
|
||||||
|
}
|
||||||
|
|
||||||
|
contact, ok := userMap["contact"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Contact is not a map")
|
||||||
|
}
|
||||||
|
|
||||||
|
if email, exists := contact["email"]; !exists || email != "john@example.com" {
|
||||||
|
t.Errorf("Set() failed: expected user.contact.email to be 'john@example.com', got %v", contact["email"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("create array and element", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should create an empty addresses array, but won't be able to set index 0
|
||||||
|
// since the array is empty
|
||||||
|
err := Set(data, "$.user.addresses[0].street", "123 Main St")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("multiple targets (should only update first)", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"users": []interface{}{
|
||||||
|
map[string]interface{}{"active": true},
|
||||||
|
map[string]interface{}{"active": true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Set(data, "$.users[*].active", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
users, ok := data["users"].([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Users is not a slice")
|
||||||
|
}
|
||||||
|
|
||||||
|
user0, ok := users[0].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User0 is not a map")
|
||||||
|
}
|
||||||
|
|
||||||
|
user1, ok := users[1].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User1 is not a map")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only the first one should be changed
|
||||||
|
if active, exists := user0["active"]; !exists || active != false {
|
||||||
|
t.Errorf("Set() failed: expected users[0].active to be false, got %v", user0["active"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// The second one should remain unchanged
|
||||||
|
if active, exists := user1["active"]; !exists || active != true {
|
||||||
|
t.Errorf("Set() incorrectly modified users[1].active: expected true, got %v", user1["active"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("setting on root should not fail (anymore)", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Set(data, "$", "Jane")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data should be unchanged
|
||||||
|
if data["name"] != "John" {
|
||||||
|
t.Errorf("Data was modified when setting on root")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetAll(t *testing.T) {
|
||||||
|
t.Run("simple property", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30,
|
||||||
|
}
|
||||||
|
err := SetAll(data, "$.name", "Jane")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SetAll() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if data["name"] != "Jane" {
|
||||||
|
t.Errorf("SetAll() failed: expected name to be 'Jane', got %v", data["name"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("all array elements", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"users": []interface{}{
|
||||||
|
map[string]interface{}{"active": true},
|
||||||
|
map[string]interface{}{"active": true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := SetAll(data, "$.users[*].active", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SetAll() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
users, ok := data["users"].([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Users is not a slice")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Both elements should be updated
|
||||||
|
for i, user := range users {
|
||||||
|
userMap, ok := user.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("User%d is not a map", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
if active, exists := userMap["active"]; !exists || active != false {
|
||||||
|
t.Errorf("SetAll() failed: expected users[%d].active to be false, got %v", i, userMap["active"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("recursive descent", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"profile": map[string]interface{}{
|
||||||
|
"active": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"admin": map[string]interface{}{
|
||||||
|
"profile": map[string]interface{}{
|
||||||
|
"active": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := SetAll(data, "$..active", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SetAll() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check user profile
|
||||||
|
userProfile, ok := data["user"].(map[string]interface{})["profile"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Failed to access user.profile")
|
||||||
|
}
|
||||||
|
if active, exists := userProfile["active"]; !exists || active != false {
|
||||||
|
t.Errorf("SetAll() didn't update user.profile.active, got: %v", active)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check admin profile
|
||||||
|
adminProfile, ok := data["admin"].(map[string]interface{})["profile"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Failed to access admin.profile")
|
||||||
|
}
|
||||||
|
if active, exists := adminProfile["active"]; !exists || active != false {
|
||||||
|
t.Errorf("SetAll() didn't update admin.profile.active, got: %v", active)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetWithPathsExtended(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
data map[string]interface{}
|
||||||
|
path string
|
||||||
|
expected []JSONNode
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple property",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30,
|
||||||
|
},
|
||||||
|
path: "$.name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "John", Path: "$.name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nested property",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$.user.name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "John", Path: "$.user.name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "array access",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"users": []interface{}{
|
||||||
|
map[string]interface{}{"name": "John", "age": 30},
|
||||||
|
map[string]interface{}{"name": "Jane", "age": 25},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$.users[1].name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "Jane", Path: "$.users[1].name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wildcard",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"users": []interface{}{
|
||||||
|
map[string]interface{}{"name": "John", "age": 30},
|
||||||
|
map[string]interface{}{"name": "Jane", "age": 25},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$.users[*].name",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "John", Path: "$.users[0].name"},
|
||||||
|
{Value: "Jane", Path: "$.users[1].name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "recursive descent",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"name": "John",
|
||||||
|
"profile": map[string]interface{}{
|
||||||
|
"email": "john@example.com",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"admin": map[string]interface{}{
|
||||||
|
"email": "admin@example.com",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "$..email",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "john@example.com", Path: "$.user.profile.email"},
|
||||||
|
{Value: "admin@example.com", Path: "$.admin.email"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := Get(tt.data, tt.path)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GetWithPaths() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if lengths match
|
||||||
|
if len(result) != len(tt.expected) {
|
||||||
|
t.Errorf("GetWithPaths() returned %d items, expected %d", len(result), len(tt.expected))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For each expected item, find its match in the results and verify both value and path
|
||||||
|
for _, expected := range tt.expected {
|
||||||
|
found := false
|
||||||
|
for _, r := range result {
|
||||||
|
// Check if value matches
|
||||||
|
if reflect.DeepEqual(r.Value, expected.Value) {
|
||||||
|
found = true
|
||||||
|
// Check if path matches
|
||||||
|
if r.Path != expected.Path {
|
||||||
|
t.Errorf("Path mismatch for value %v: got %s, expected %s", r.Value, r.Path, expected.Path)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("Expected node with value %v and path %s not found in results", expected.Value, expected.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
318
processor/jsonpath/jsonpath_test.go
Normal file
318
processor/jsonpath/jsonpath_test.go
Normal file
@@ -0,0 +1,318 @@
|
|||||||
|
package jsonpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testData = map[string]interface{}{
|
||||||
|
"store": map[string]interface{}{
|
||||||
|
"book": []interface{}{
|
||||||
|
map[string]interface{}{
|
||||||
|
"title": "The Fellowship of the Ring",
|
||||||
|
"price": 22.99,
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"title": "The Two Towers",
|
||||||
|
"price": 23.45,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"bicycle": map[string]interface{}{
|
||||||
|
"color": "red",
|
||||||
|
"price": 199.95,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
steps []JSONStep
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
path: "$.store.bicycle.color",
|
||||||
|
steps: []JSONStep{
|
||||||
|
{Type: RootStep},
|
||||||
|
{Type: ChildStep, Key: "store"},
|
||||||
|
{Type: ChildStep, Key: "bicycle"},
|
||||||
|
{Type: ChildStep, Key: "color"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "$..price",
|
||||||
|
steps: []JSONStep{
|
||||||
|
{Type: RootStep},
|
||||||
|
{Type: RecursiveDescentStep, Key: "price"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "$.store.book[*].title",
|
||||||
|
steps: []JSONStep{
|
||||||
|
{Type: RootStep},
|
||||||
|
{Type: ChildStep, Key: "store"},
|
||||||
|
{Type: ChildStep, Key: "book"},
|
||||||
|
{Type: IndexStep, Index: -1}, // Wildcard
|
||||||
|
{Type: ChildStep, Key: "title"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "$.store.book[0]",
|
||||||
|
steps: []JSONStep{
|
||||||
|
{Type: RootStep},
|
||||||
|
{Type: ChildStep, Key: "store"},
|
||||||
|
{Type: ChildStep, Key: "book"},
|
||||||
|
{Type: IndexStep, Index: 0},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "invalid.path",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "$.store.book[abc]",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.path, func(t *testing.T) {
|
||||||
|
steps, err := ParseJSONPath(tt.path)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Fatalf("ParseJSONPath() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
if !tt.wantErr && !reflect.DeepEqual(steps, tt.steps) {
|
||||||
|
t.Errorf("ParseJSONPath() steps = %+v, want %+v", steps, tt.steps)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEvaluator(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
expected []JSONNode
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple_property_access",
|
||||||
|
path: "$.store.bicycle.color",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "red", Path: "$.store.bicycle.color"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "array_index_access",
|
||||||
|
path: "$.store.book[0].title",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "The Fellowship of the Ring", Path: "$.store.book[0].title"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wildcard_array_access",
|
||||||
|
path: "$.store.book[*].title",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "The Fellowship of the Ring", Path: "$.store.book[0].title"},
|
||||||
|
{Value: "The Two Towers", Path: "$.store.book[1].title"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "recursive_price_search",
|
||||||
|
path: "$..price",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: 22.99, Path: "$.store.book[0].price"},
|
||||||
|
{Value: 23.45, Path: "$.store.book[1].price"},
|
||||||
|
{Value: 199.95, Path: "$.store.bicycle.price"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wildcard_recursive",
|
||||||
|
path: "$..*",
|
||||||
|
expected: []JSONNode{
|
||||||
|
// These will be compared by value only, paths will be validated separately
|
||||||
|
{Value: testData["store"].(map[string]interface{})["book"]},
|
||||||
|
{Value: testData["store"].(map[string]interface{})["bicycle"]},
|
||||||
|
{Value: testData["store"].(map[string]interface{})["book"].([]interface{})[0]},
|
||||||
|
{Value: testData["store"].(map[string]interface{})["book"].([]interface{})[1]},
|
||||||
|
{Value: "The Fellowship of the Ring"},
|
||||||
|
{Value: 22.99},
|
||||||
|
{Value: "The Two Towers"},
|
||||||
|
{Value: 23.45},
|
||||||
|
{Value: "red"},
|
||||||
|
{Value: 199.95},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid_index",
|
||||||
|
path: "$.store.book[5]",
|
||||||
|
expected: []JSONNode{},
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nonexistent_property",
|
||||||
|
path: "$.store.nonexistent",
|
||||||
|
expected: []JSONNode{},
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// Use GetWithPaths directly
|
||||||
|
result, err := Get(testData, tt.path)
|
||||||
|
if err != nil {
|
||||||
|
if !tt.error {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Special handling for wildcard recursive test
|
||||||
|
if tt.name == "wildcard_recursive" {
|
||||||
|
// Skip length check for wildcard recursive since it might vary
|
||||||
|
// Just verify that each expected item is in the results
|
||||||
|
|
||||||
|
// Validate values match and paths are filled in
|
||||||
|
for _, e := range tt.expected {
|
||||||
|
found := false
|
||||||
|
for _, r := range result {
|
||||||
|
if reflect.DeepEqual(r.Value, e.Value) {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("Expected value %v not found in results", e.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result) != len(tt.expected) {
|
||||||
|
t.Errorf("Expected %d items, got %d", len(tt.expected), len(result))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate both values and paths
|
||||||
|
for i, e := range tt.expected {
|
||||||
|
if i < len(result) {
|
||||||
|
if !reflect.DeepEqual(result[i].Value, e.Value) {
|
||||||
|
t.Errorf("Value at [%d]: got %v, expected %v", i, result[i].Value, e.Value)
|
||||||
|
}
|
||||||
|
if result[i].Path != e.Path {
|
||||||
|
t.Errorf("Path at [%d]: got %s, expected %s", i, result[i].Path, e.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeCases(t *testing.T) {
|
||||||
|
t.Run("empty_data", func(t *testing.T) {
|
||||||
|
result, err := Get(nil, "$.a.b")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error for empty data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) > 0 {
|
||||||
|
t.Errorf("Expected empty result, got %v", result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty_path", func(t *testing.T) {
|
||||||
|
_, err := ParseJSONPath("")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for empty path")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("numeric_keys", func(t *testing.T) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"42": "answer",
|
||||||
|
}
|
||||||
|
result, err := Get(data, "$.42")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) == 0 || result[0].Value != "answer" {
|
||||||
|
t.Errorf("Expected 'answer', got %v", result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetWithPaths(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
expected []JSONNode
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple_property_access",
|
||||||
|
path: "$.store.bicycle.color",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "red", Path: "$.store.bicycle.color"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "array_index_access",
|
||||||
|
path: "$.store.book[0].title",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "The Fellowship of the Ring", Path: "$.store.book[0].title"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wildcard_array_access",
|
||||||
|
path: "$.store.book[*].title",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: "The Fellowship of the Ring", Path: "$.store.book[0].title"},
|
||||||
|
{Value: "The Two Towers", Path: "$.store.book[1].title"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "recursive_price_search",
|
||||||
|
path: "$..price",
|
||||||
|
expected: []JSONNode{
|
||||||
|
{Value: 22.99, Path: "$.store.book[0].price"},
|
||||||
|
{Value: 23.45, Path: "$.store.book[1].price"},
|
||||||
|
{Value: 199.95, Path: "$.store.bicycle.price"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := Get(testData, tt.path)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if lengths match
|
||||||
|
if len(result) != len(tt.expected) {
|
||||||
|
t.Errorf("GetWithPaths() returned %d items, expected %d", len(result), len(tt.expected))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For each expected item, find its match in the results and verify both value and path
|
||||||
|
for _, expected := range tt.expected {
|
||||||
|
found := false
|
||||||
|
for _, r := range result {
|
||||||
|
// First verify the value matches
|
||||||
|
if reflect.DeepEqual(r.Value, expected.Value) {
|
||||||
|
found = true
|
||||||
|
// Then verify the path matches
|
||||||
|
if r.Path != expected.Path {
|
||||||
|
t.Errorf("Path mismatch for value %v: got %s, expected %s", r.Value, r.Path, expected.Path)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("Expected node with value %v and path %s not found in results", expected.Value, expected.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
312
processor/processor.go
Normal file
312
processor/processor.go
Normal file
@@ -0,0 +1,312 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/antchfx/xmlquery"
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Processor defines the interface for all file processors
|
||||||
|
type Processor interface {
|
||||||
|
// Process handles processing a file with the given pattern and Lua expression
|
||||||
|
// Now implemented as a base function in processor.go
|
||||||
|
// Process(filename string, pattern string, luaExpr string) (int, int, error)
|
||||||
|
|
||||||
|
// ProcessContent handles processing a string content directly with the given pattern and Lua expression
|
||||||
|
// Returns the modified content, modification count, match count, and any error
|
||||||
|
ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error)
|
||||||
|
|
||||||
|
// ToLua converts processor-specific data to Lua variables
|
||||||
|
ToLua(L *lua.LState, data interface{}) error
|
||||||
|
|
||||||
|
// FromLua retrieves modified data from Lua
|
||||||
|
FromLua(L *lua.LState) (interface{}, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModificationRecord tracks a single value modification
|
||||||
|
type ModificationRecord struct {
|
||||||
|
File string
|
||||||
|
OldValue string
|
||||||
|
NewValue string
|
||||||
|
Operation string
|
||||||
|
Context string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLuaState() (*lua.LState, error) {
|
||||||
|
L := lua.NewState()
|
||||||
|
// defer L.Close()
|
||||||
|
|
||||||
|
// Load math library
|
||||||
|
L.Push(L.GetGlobal("require"))
|
||||||
|
L.Push(lua.LString("math"))
|
||||||
|
if err := L.PCall(1, 1, nil); err != nil {
|
||||||
|
return nil, fmt.Errorf("error loading Lua math library: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize helper functions
|
||||||
|
if err := InitLuaHelpers(L); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return L, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Process(p Processor, filename string, pattern string, luaExpr string) (int, int, error) {
|
||||||
|
// Read file content
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, fmt.Errorf("error getting current working directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fullPath := filepath.Join(cwd, filename)
|
||||||
|
content, err := os.ReadFile(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, fmt.Errorf("error reading file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileContent := string(content)
|
||||||
|
|
||||||
|
// Process the content
|
||||||
|
modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we made modifications, save the file
|
||||||
|
if modCount > 0 {
|
||||||
|
err = os.WriteFile(fullPath, []byte(modifiedContent), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, fmt.Errorf("error writing file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return modCount, matchCount, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToLua converts a struct or map to a Lua table recursively
|
||||||
|
func ToLua(L *lua.LState, data interface{}) (lua.LValue, error) {
|
||||||
|
switch v := data.(type) {
|
||||||
|
case *xmlquery.Node:
|
||||||
|
luaTable := L.NewTable()
|
||||||
|
luaTable.RawSetString("text", lua.LString(v.Data))
|
||||||
|
// Should be a map, simple key value pairs
|
||||||
|
attr, err := ToLua(L, v.Attr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
luaTable.RawSetString("attr", attr)
|
||||||
|
return luaTable, nil
|
||||||
|
case map[string]interface{}:
|
||||||
|
luaTable := L.NewTable()
|
||||||
|
for key, value := range v {
|
||||||
|
luaValue, err := ToLua(L, value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
luaTable.RawSetString(key, luaValue)
|
||||||
|
}
|
||||||
|
return luaTable, nil
|
||||||
|
case []interface{}:
|
||||||
|
luaTable := L.NewTable()
|
||||||
|
for i, value := range v {
|
||||||
|
luaValue, err := ToLua(L, value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
luaTable.RawSetInt(i+1, luaValue) // Lua arrays are 1-indexed
|
||||||
|
}
|
||||||
|
return luaTable, nil
|
||||||
|
case string:
|
||||||
|
return lua.LString(v), nil
|
||||||
|
case bool:
|
||||||
|
return lua.LBool(v), nil
|
||||||
|
case float64:
|
||||||
|
return lua.LNumber(v), nil
|
||||||
|
case nil:
|
||||||
|
return lua.LNil, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported data type: %T", data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromLua converts a Lua table to a struct or map recursively
|
||||||
|
func FromLua(L *lua.LState, luaValue lua.LValue) (interface{}, error) {
|
||||||
|
switch v := luaValue.(type) {
|
||||||
|
// Well shit...
|
||||||
|
// Tables in lua are both maps and arrays
|
||||||
|
// As arrays they are ordered and as maps, obviously, not
|
||||||
|
// So when we parse them to a go map we fuck up the order for arrays
|
||||||
|
// We have to find a better way....
|
||||||
|
case *lua.LTable:
|
||||||
|
isArray, err := IsLuaTableArray(L, v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if isArray {
|
||||||
|
result := make([]interface{}, 0)
|
||||||
|
v.ForEach(func(key lua.LValue, value lua.LValue) {
|
||||||
|
converted, _ := FromLua(L, value)
|
||||||
|
result = append(result, converted)
|
||||||
|
})
|
||||||
|
return result, nil
|
||||||
|
} else {
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
v.ForEach(func(key lua.LValue, value lua.LValue) {
|
||||||
|
converted, _ := FromLua(L, value)
|
||||||
|
result[key.String()] = converted
|
||||||
|
})
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
case lua.LString:
|
||||||
|
return string(v), nil
|
||||||
|
case lua.LBool:
|
||||||
|
return bool(v), nil
|
||||||
|
case lua.LNumber:
|
||||||
|
return float64(v), nil
|
||||||
|
default:
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsLuaTableArray(L *lua.LState, v *lua.LTable) (bool, error) {
|
||||||
|
L.SetGlobal("table_to_check", v)
|
||||||
|
|
||||||
|
// Use our predefined helper function from InitLuaHelpers
|
||||||
|
err := L.DoString(`is_array = isArray(table_to_check)`)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("error determining if table is array: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the result of our Lua function
|
||||||
|
isArray := L.GetGlobal("is_array")
|
||||||
|
// LVIsFalse returns true if a given LValue is a nil or false otherwise false.
|
||||||
|
if !lua.LVIsFalse(isArray) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitLuaHelpers initializes common Lua helper functions
|
||||||
|
func InitLuaHelpers(L *lua.LState) error {
|
||||||
|
helperScript := `
|
||||||
|
-- Custom Lua helpers for math operations
|
||||||
|
function min(a, b) return math.min(a, b) end
|
||||||
|
function max(a, b) return math.max(a, b) end
|
||||||
|
function round(x, n)
|
||||||
|
if n == nil then n = 0 end
|
||||||
|
return math.floor(x * 10^n + 0.5) / 10^n
|
||||||
|
end
|
||||||
|
function floor(x) return math.floor(x) end
|
||||||
|
function ceil(x) return math.ceil(x) end
|
||||||
|
function upper(s) return string.upper(s) end
|
||||||
|
function lower(s) return string.lower(s) end
|
||||||
|
|
||||||
|
-- String to number conversion helper
|
||||||
|
function num(str)
|
||||||
|
return tonumber(str) or 0
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Number to string conversion
|
||||||
|
function str(num)
|
||||||
|
return tostring(num)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check if string is numeric
|
||||||
|
function is_number(str)
|
||||||
|
return tonumber(str) ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function isArray(t)
|
||||||
|
if type(t) ~= "table" then return false end
|
||||||
|
local max = 0
|
||||||
|
local count = 0
|
||||||
|
for k, _ in pairs(t) do
|
||||||
|
if type(k) ~= "number" or k < 1 or math.floor(k) ~= k then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
max = math.max(max, k)
|
||||||
|
count = count + 1
|
||||||
|
end
|
||||||
|
return max == count
|
||||||
|
end
|
||||||
|
|
||||||
|
modified = false
|
||||||
|
`
|
||||||
|
if err := L.DoString(helperScript); err != nil {
|
||||||
|
return fmt.Errorf("error loading helper functions: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper utility functions
|
||||||
|
|
||||||
|
// LimitString truncates a string to maxLen and adds "..." if truncated
|
||||||
|
func LimitString(s string, maxLen int) string {
|
||||||
|
s = strings.ReplaceAll(s, "\n", "\\n")
|
||||||
|
if len(s) <= maxLen {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return s[:maxLen-3] + "..."
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrependLuaAssignment(luaExpr string) string {
|
||||||
|
// Auto-prepend v1 for expressions starting with operators
|
||||||
|
if strings.HasPrefix(luaExpr, "*") ||
|
||||||
|
strings.HasPrefix(luaExpr, "/") ||
|
||||||
|
strings.HasPrefix(luaExpr, "+") ||
|
||||||
|
strings.HasPrefix(luaExpr, "-") ||
|
||||||
|
strings.HasPrefix(luaExpr, "^") ||
|
||||||
|
strings.HasPrefix(luaExpr, "%") {
|
||||||
|
luaExpr = "v1 = v1" + luaExpr
|
||||||
|
} else if strings.HasPrefix(luaExpr, "=") {
|
||||||
|
// Handle direct assignment with = operator
|
||||||
|
luaExpr = "v1 " + luaExpr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add assignment if needed
|
||||||
|
if !strings.Contains(luaExpr, "=") {
|
||||||
|
luaExpr = "v1 = " + luaExpr
|
||||||
|
}
|
||||||
|
return luaExpr
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildLuaScript prepares a Lua expression from shorthand notation
|
||||||
|
func BuildLuaScript(luaExpr string) string {
|
||||||
|
luaExpr = PrependLuaAssignment(luaExpr)
|
||||||
|
|
||||||
|
// This allows the user to specify whether or not they modified a value
|
||||||
|
// If they do nothing we assume they did modify (no return at all)
|
||||||
|
// If they return before our return then they themselves specify what they did
|
||||||
|
// If nothing is returned lua assumes nil
|
||||||
|
// So we can say our value was modified if the return value is either nil or true
|
||||||
|
// If the return value is false then the user wants to keep the original
|
||||||
|
fullScript := fmt.Sprintf(`
|
||||||
|
function run()
|
||||||
|
%s
|
||||||
|
end
|
||||||
|
local res = run()
|
||||||
|
modified = res == nil or res
|
||||||
|
`, luaExpr)
|
||||||
|
|
||||||
|
return fullScript
|
||||||
|
}
|
||||||
|
|
||||||
|
// Max returns the maximum of two integers
|
||||||
|
func Max(a, b int) int {
|
||||||
|
if a > b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min returns the minimum of two integers
|
||||||
|
func Min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
166
processor/regex.go
Normal file
166
processor/regex.go
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegexProcessor implements the Processor interface using regex patterns
|
||||||
|
type RegexProcessor struct{}
|
||||||
|
|
||||||
|
// ToLua sets capture groups as Lua variables (v1, v2, etc. for numeric values and s1, s2, etc. for strings)
|
||||||
|
func (p *RegexProcessor) ToLua(L *lua.LState, data interface{}) error {
|
||||||
|
captures, ok := data.([]string)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("expected []string for captures, got %T", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variables for each capture group, starting from v1/s1 for the first capture
|
||||||
|
for i := 0; i < len(captures); i++ {
|
||||||
|
// Set string version (always available as s1, s2, etc.)
|
||||||
|
L.SetGlobal(fmt.Sprintf("s%d", i+1), lua.LString(captures[i]))
|
||||||
|
|
||||||
|
// Try to convert to number and set v1, v2, etc.
|
||||||
|
if val, err := strconv.ParseFloat(captures[i], 64); err == nil {
|
||||||
|
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LNumber(val))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromLua implements the Processor interface for RegexProcessor
|
||||||
|
func (p *RegexProcessor) FromLua(L *lua.LState) (interface{}, error) {
|
||||||
|
// Get the modified values after Lua execution
|
||||||
|
modifications := make(map[int]string)
|
||||||
|
|
||||||
|
// Check for modifications to v1-v12 and s1-s12
|
||||||
|
for i := 0; i < 12; i++ {
|
||||||
|
// Check both v and s variables to see if any were modified
|
||||||
|
vVarName := fmt.Sprintf("v%d", i+1)
|
||||||
|
sVarName := fmt.Sprintf("s%d", i+1)
|
||||||
|
|
||||||
|
vLuaVal := L.GetGlobal(vVarName)
|
||||||
|
sLuaVal := L.GetGlobal(sVarName)
|
||||||
|
|
||||||
|
// If our value is a number then it's very likely we want it to be a number
|
||||||
|
// And not a string
|
||||||
|
// If we do want it to be a string we will cast it into a string in lua
|
||||||
|
// wait that wouldn't work... Casting v to a string would not load it here
|
||||||
|
if vLuaVal.Type() == lua.LTNumber {
|
||||||
|
modifications[i] = vLuaVal.String()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if sLuaVal.Type() == lua.LTString {
|
||||||
|
modifications[i] = sLuaVal.String()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return modifications, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessContent applies regex replacement with Lua processing
|
||||||
|
func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) {
|
||||||
|
// Handle special pattern modifications
|
||||||
|
if !strings.HasPrefix(pattern, "(?s)") {
|
||||||
|
pattern = "(?s)" + pattern
|
||||||
|
}
|
||||||
|
|
||||||
|
compiledPattern, err := regexp.Compile(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return "", 0, 0, fmt.Errorf("error compiling pattern: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
previous := luaExpr
|
||||||
|
luaExpr = BuildLuaScript(luaExpr)
|
||||||
|
fmt.Printf("Changing Lua expression from: %s to: %s\n", previous, luaExpr)
|
||||||
|
|
||||||
|
L, err := NewLuaState()
|
||||||
|
if err != nil {
|
||||||
|
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
|
||||||
|
}
|
||||||
|
defer L.Close()
|
||||||
|
|
||||||
|
// Initialize Lua environment
|
||||||
|
modificationCount := 0
|
||||||
|
|
||||||
|
// Process all regex matches
|
||||||
|
result := content
|
||||||
|
indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
|
||||||
|
// We walk backwards because we're replacing something with something else that might be longer
|
||||||
|
// And in the case it is longer than the original all indicces past that change will be fucked up
|
||||||
|
// By going backwards we fuck up all the indices to the end of the file that we don't care about
|
||||||
|
// Because there either aren't any (last match) or they're already modified (subsequent matches)
|
||||||
|
for i := len(indices) - 1; i >= 0; i-- {
|
||||||
|
matchIndices := indices[i]
|
||||||
|
// Why we're doing this whole song and dance of indices is to properly handle empty matches
|
||||||
|
// Plus it's a little cleaner to surgically replace our matches
|
||||||
|
// If we were to use string.replace and encountered an empty match there'd be nothing to replace
|
||||||
|
// But using indices an empty match would have its starting and ending indices be the same
|
||||||
|
// So when we're cutting open the array we say 0:7 + modified + 7:end
|
||||||
|
// As if concatenating in the middle of the array
|
||||||
|
// Plus it supports lookarounds
|
||||||
|
match := content[matchIndices[0]:matchIndices[1]]
|
||||||
|
|
||||||
|
groups := matchIndices[2:]
|
||||||
|
if len(groups) <= 0 {
|
||||||
|
fmt.Println("No capture groups for lua to chew on")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(groups)%2 == 1 {
|
||||||
|
fmt.Println("Odd number of indices of groups, what the fuck?")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
captures := make([]string, 0, len(groups)/2)
|
||||||
|
for j := 0; j < len(groups); j += 2 {
|
||||||
|
captures = append(captures, content[groups[j]:groups[j+1]])
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := p.ToLua(L, captures); err != nil {
|
||||||
|
fmt.Println("Error setting Lua variables:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := L.DoString(luaExpr); err != nil {
|
||||||
|
fmt.Printf("Error executing Lua code %s for group %s: %v", luaExpr, captures, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get modifications from Lua
|
||||||
|
modResult, err := p.FromLua(L)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting modifications:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply modifications to the matched text
|
||||||
|
modsMap, ok := modResult.(map[int]string)
|
||||||
|
if !ok || len(modsMap) == 0 {
|
||||||
|
fmt.Println("No modifications to apply")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the modifications to the original match
|
||||||
|
replacement := match
|
||||||
|
for i := len(modsMap) - 1; i >= 0; i-- {
|
||||||
|
newVal := modsMap[i]
|
||||||
|
// Indices of the group are relative to content
|
||||||
|
// To relate them to match we have to subtract the match start index
|
||||||
|
groupStart := groups[i*2] - matchIndices[0]
|
||||||
|
groupEnd := groups[i*2+1] - matchIndices[0]
|
||||||
|
replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
|
||||||
|
}
|
||||||
|
|
||||||
|
modificationCount++
|
||||||
|
result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, modificationCount, len(indices), nil
|
||||||
|
}
|
549
processor/regex_test.go
Normal file
549
processor/regex_test.go
Normal file
@@ -0,0 +1,549 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestLogger implements the Logger interface for testing
|
||||||
|
type TestLogger struct {
|
||||||
|
T *testing.T // Reference to the test's *testing.T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *TestLogger) Printf(format string, v ...interface{}) {
|
||||||
|
if l.T != nil {
|
||||||
|
l.T.Logf(format, v...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to normalize whitespace for comparison
|
||||||
|
func normalizeWhitespace(s string) string {
|
||||||
|
// Replace all whitespace with a single space
|
||||||
|
re := regexp.MustCompile(`\s+`)
|
||||||
|
return re.ReplaceAllString(strings.TrimSpace(s), " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildLuaScript(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
input string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"s1 .. '_suffix'", "v1 = s1 .. '_suffix'"},
|
||||||
|
{"v1 * 1.5", "v1 = v1 * 1.5"},
|
||||||
|
{"v1 + 10", "v1 = v1 + 10"},
|
||||||
|
{"v1 * 2", "v1 = v1 * 2"},
|
||||||
|
{"v1 * v2", "v1 = v1 * v2"},
|
||||||
|
{"v1 / v2", "v1 = v1 / v2"},
|
||||||
|
{"12", "v1 = 12"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
result := PrependLuaAssignment(c.input)
|
||||||
|
if result != c.expected {
|
||||||
|
t.Errorf("BuildLuaScript(%q): expected %q, got %q", c.input, c.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleValueMultiplication(t *testing.T) {
|
||||||
|
content := `<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
expected := `<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `(?s)<value>(\d+)</value>`, "v1 = v1*1.5")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 1 {
|
||||||
|
t.Errorf("Expected 1 match, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 1 {
|
||||||
|
t.Errorf("Expected 1 modification, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShorthandNotation(t *testing.T) {
|
||||||
|
content := `<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
expected := `<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `(?s)<value>(\d+)</value>`, "v1*1.5")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 1 {
|
||||||
|
t.Errorf("Expected 1 match, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 1 {
|
||||||
|
t.Errorf("Expected 1 modification, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShorthandNotationFloats(t *testing.T) {
|
||||||
|
content := `<config>
|
||||||
|
<item>
|
||||||
|
<value>10.5</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
expected := `<config>
|
||||||
|
<item>
|
||||||
|
<value>15.75</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `(?s)<value>(\d+\.\d+)</value>`, "v1*1.5")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 1 {
|
||||||
|
t.Errorf("Expected 1 match, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 1 {
|
||||||
|
t.Errorf("Expected 1 modification, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArrayNotation(t *testing.T) {
|
||||||
|
content := `<config>
|
||||||
|
<prices>
|
||||||
|
<price>10</price>
|
||||||
|
<price>20</price>
|
||||||
|
<price>30</price>
|
||||||
|
</prices>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
expected := `<config>
|
||||||
|
<prices>
|
||||||
|
<price>20</price>
|
||||||
|
<price>40</price>
|
||||||
|
<price>60</price>
|
||||||
|
</prices>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `(?s)<price>(\d+)</price>`, "v1*2")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 3 {
|
||||||
|
t.Errorf("Expected 3 matches, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 3 {
|
||||||
|
t.Errorf("Expected 3 modifications, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultipleNumericMatches(t *testing.T) {
|
||||||
|
content := `<data>
|
||||||
|
<entry>50</entry>
|
||||||
|
<entry>100</entry>
|
||||||
|
<entry>200</entry>
|
||||||
|
</data>`
|
||||||
|
|
||||||
|
expected := `<data>
|
||||||
|
<entry>100</entry>
|
||||||
|
<entry>200</entry>
|
||||||
|
<entry>400</entry>
|
||||||
|
</data>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `<entry>(\d+)</entry>`, "v1*2")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 3 {
|
||||||
|
t.Errorf("Expected 3 matches, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 3 {
|
||||||
|
t.Errorf("Expected 3 modifications, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultipleStringMatches(t *testing.T) {
|
||||||
|
content := `<data>
|
||||||
|
<name>John</name>
|
||||||
|
<name>Mary</name>
|
||||||
|
</data>`
|
||||||
|
|
||||||
|
expected := `<data>
|
||||||
|
<name>John_modified</name>
|
||||||
|
<name>Mary_modified</name>
|
||||||
|
</data>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `<name>([A-Za-z]+)</name>`, `s1 = s1 .. "_modified"`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 2 {
|
||||||
|
t.Errorf("Expected 2 matches, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 2 {
|
||||||
|
t.Errorf("Expected 2 modifications, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStringUpperCase(t *testing.T) {
|
||||||
|
content := `<users>
|
||||||
|
<user>John</user>
|
||||||
|
<user>Mary</user>
|
||||||
|
</users>`
|
||||||
|
|
||||||
|
expected := `<users>
|
||||||
|
<user>JOHN</user>
|
||||||
|
<user>MARY</user>
|
||||||
|
</users>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
// Convert names to uppercase using Lua string function
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `<user>([A-Za-z]+)</user>`, `s1 = string.upper(s1)`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 2 {
|
||||||
|
t.Errorf("Expected 2 matches, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 2 {
|
||||||
|
t.Errorf("Expected 2 modifications, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStringConcatenation(t *testing.T) {
|
||||||
|
content := `<products>
|
||||||
|
<product>Apple</product>
|
||||||
|
<product>Banana</product>
|
||||||
|
</products>`
|
||||||
|
|
||||||
|
expected := `<products>
|
||||||
|
<product>Apple_fruit</product>
|
||||||
|
<product>Banana_fruit</product>
|
||||||
|
</products>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `<product>([A-Za-z]+)</product>`, `s1 = s1 .. "_fruit"`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 2 {
|
||||||
|
t.Errorf("Expected 2 matches, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 2 {
|
||||||
|
t.Errorf("Expected 2 modifications, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added from main_test.go
|
||||||
|
func TestDecimalValues(t *testing.T) {
|
||||||
|
content := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>10.5</value>
|
||||||
|
<multiplier>2.5</multiplier>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>26.25</value>
|
||||||
|
<multiplier>2.5</multiplier>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
luaExpr := BuildLuaScript("v1 = v1 * v2")
|
||||||
|
|
||||||
|
modifiedContent, _, _, err := p.ProcessContent(content, regex.String(), luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||||
|
normalizedExpected := normalizeWhitespace(expected)
|
||||||
|
if normalizedModified != normalizedExpected {
|
||||||
|
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added from main_test.go
|
||||||
|
func TestLuaMathFunctions(t *testing.T) {
|
||||||
|
content := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>16</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>4</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
luaExpr := BuildLuaScript("v1 = math.sqrt(v1)")
|
||||||
|
|
||||||
|
modifiedContent, _, _, err := p.ProcessContent(content, regex.String(), luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||||
|
normalizedExpected := normalizeWhitespace(expected)
|
||||||
|
if normalizedModified != normalizedExpected {
|
||||||
|
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added from main_test.go
|
||||||
|
func TestDirectAssignment(t *testing.T) {
|
||||||
|
content := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>0</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
luaExpr := BuildLuaScript("=0")
|
||||||
|
|
||||||
|
modifiedContent, _, _, err := p.ProcessContent(content, regex.String(), luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||||
|
normalizedExpected := normalizeWhitespace(expected)
|
||||||
|
if normalizedModified != normalizedExpected {
|
||||||
|
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added from main_test.go
|
||||||
|
func TestStringAndNumericOperations(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
regexPattern string
|
||||||
|
luaExpression string
|
||||||
|
expectedOutput string
|
||||||
|
expectedMods int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Basic numeric multiplication",
|
||||||
|
input: "<value>42</value>",
|
||||||
|
regexPattern: "<value>(\\d+)</value>",
|
||||||
|
luaExpression: "v1 = v1 * 2",
|
||||||
|
expectedOutput: "<value>84</value>",
|
||||||
|
expectedMods: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Basic string manipulation",
|
||||||
|
input: "<name>test</name>",
|
||||||
|
regexPattern: "<name>(.*?)</name>",
|
||||||
|
luaExpression: "s1 = string.upper(s1)",
|
||||||
|
expectedOutput: "<name>TEST</name>",
|
||||||
|
expectedMods: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "String concatenation",
|
||||||
|
input: "<id>abc123</id>",
|
||||||
|
regexPattern: "<id>(.*?)</id>",
|
||||||
|
luaExpression: "s1 = s1 .. '_modified'",
|
||||||
|
expectedOutput: "<id>abc123_modified</id>",
|
||||||
|
expectedMods: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Numeric value from string using num()",
|
||||||
|
input: "<price>19.99</price>",
|
||||||
|
regexPattern: "<price>(.*?)</price>",
|
||||||
|
luaExpression: "v1 = num(s1) * 1.2",
|
||||||
|
expectedOutput: "<price>23.987999999999996</price>",
|
||||||
|
expectedMods: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// Compile the regex pattern with multiline support
|
||||||
|
pattern := "(?s)" + tt.regexPattern
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
luaExpr := BuildLuaScript(tt.luaExpression)
|
||||||
|
|
||||||
|
// Process with our function
|
||||||
|
result, modCount, _, err := p.ProcessContent(tt.input, pattern, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Process function failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check results
|
||||||
|
if result != tt.expectedOutput {
|
||||||
|
t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modCount != tt.expectedMods {
|
||||||
|
t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added from main_test.go
|
||||||
|
func TestEdgeCases(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
regexPattern string
|
||||||
|
luaExpression string
|
||||||
|
expectedOutput string
|
||||||
|
expectedMods int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Empty capture group",
|
||||||
|
input: "<value></value>",
|
||||||
|
regexPattern: "<value>(.*?)</value>",
|
||||||
|
luaExpression: "s1 = 'filled'",
|
||||||
|
expectedOutput: "<value>filled</value>",
|
||||||
|
expectedMods: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-numeric string with numeric operation",
|
||||||
|
input: "<value>abc</value>",
|
||||||
|
regexPattern: "<value>(.*?)</value>",
|
||||||
|
luaExpression: "v1 = v1 * 2", // This would fail if we didn't handle strings properly
|
||||||
|
expectedOutput: "<value>abc</value>", // Should remain unchanged
|
||||||
|
expectedMods: 0, // No modifications
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid number conversion",
|
||||||
|
input: "<value>abc</value>",
|
||||||
|
regexPattern: "<value>(.*?)</value>",
|
||||||
|
luaExpression: "v1 = num(s1) + 10", // num(s1) should return 0
|
||||||
|
expectedOutput: "<value>10</value>",
|
||||||
|
expectedMods: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Multiline string",
|
||||||
|
input: "<text>Line 1\nLine 2</text>",
|
||||||
|
regexPattern: "<text>(.*?)</text>",
|
||||||
|
luaExpression: "s1 = string.gsub(s1, '\\n', ' - ')",
|
||||||
|
expectedOutput: "<text>Line 1 - Line 2</text>",
|
||||||
|
expectedMods: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// Make sure the regex can match across multiple lines
|
||||||
|
pattern := "(?s)" + tt.regexPattern
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
luaExpr := BuildLuaScript(tt.luaExpression)
|
||||||
|
|
||||||
|
// Process with our function
|
||||||
|
result, modCount, _, err := p.ProcessContent(tt.input, pattern, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Process function failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check results
|
||||||
|
if result != tt.expectedOutput {
|
||||||
|
t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modCount != tt.expectedMods {
|
||||||
|
t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
412
processor/xml.go
Normal file
412
processor/xml.go
Normal file
@@ -0,0 +1,412 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"modify/processor/xpath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/antchfx/xmlquery"
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
// XMLProcessor implements the Processor interface for XML documents
|
||||||
|
type XMLProcessor struct{}
|
||||||
|
|
||||||
|
// ProcessContent implements the Processor interface for XMLProcessor
|
||||||
|
func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr string) (string, int, int, error) {
|
||||||
|
// Parse XML document
|
||||||
|
// We can't really use encoding/xml here because it requires a pre defined struct
|
||||||
|
// And we HAVE TO parse dynamic unknown XML
|
||||||
|
doc, err := xmlquery.Parse(strings.NewReader(content))
|
||||||
|
if err != nil {
|
||||||
|
return content, 0, 0, fmt.Errorf("error parsing XML: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find nodes matching the XPath pattern
|
||||||
|
nodes, err := xpath.Get(doc, path)
|
||||||
|
if err != nil {
|
||||||
|
return content, 0, 0, fmt.Errorf("error executing XPath: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
matchCount := len(nodes)
|
||||||
|
if matchCount == 0 {
|
||||||
|
return content, 0, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply modifications to each node
|
||||||
|
modCount := 0
|
||||||
|
for _, node := range nodes {
|
||||||
|
L, err := NewLuaState()
|
||||||
|
if err != nil {
|
||||||
|
return content, 0, 0, fmt.Errorf("error creating Lua state: %v", err)
|
||||||
|
}
|
||||||
|
defer L.Close()
|
||||||
|
|
||||||
|
err = p.ToLua(L, node)
|
||||||
|
if err != nil {
|
||||||
|
return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = L.DoString(BuildLuaScript(luaExpr))
|
||||||
|
if err != nil {
|
||||||
|
return content, modCount, matchCount, fmt.Errorf("error executing Lua: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := p.FromLua(L)
|
||||||
|
if err != nil {
|
||||||
|
return content, modCount, matchCount, fmt.Errorf("error getting result from Lua: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("%#v", result)
|
||||||
|
|
||||||
|
modified := false
|
||||||
|
modified = L.GetGlobal("modified").String() == "true"
|
||||||
|
if !modified {
|
||||||
|
log.Printf("No changes made to node at path: %s", node.Data)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply modification based on the result
|
||||||
|
if updatedValue, ok := result.(string); ok {
|
||||||
|
// If the result is a simple string, update the node value directly
|
||||||
|
xpath.Set(doc, path, updatedValue)
|
||||||
|
} else if nodeData, ok := result.(map[string]interface{}); ok {
|
||||||
|
// If the result is a map, apply more complex updates
|
||||||
|
updateNodeFromMap(node, nodeData)
|
||||||
|
}
|
||||||
|
|
||||||
|
modCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize the modified XML document to string
|
||||||
|
if doc.FirstChild != nil && doc.FirstChild.Type == xmlquery.DeclarationNode {
|
||||||
|
// If we have an XML declaration, start with it
|
||||||
|
declaration := doc.FirstChild.OutputXML(true)
|
||||||
|
// Remove the firstChild (declaration) before serializing the rest of the document
|
||||||
|
doc.FirstChild = doc.FirstChild.NextSibling
|
||||||
|
return ConvertToNamedEntities(declaration + doc.OutputXML(true)), modCount, matchCount, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert numeric entities to named entities for better readability
|
||||||
|
return ConvertToNamedEntities(doc.OutputXML(true)), modCount, matchCount, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) error {
|
||||||
|
table, err := p.ToLuaTable(L, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
L.SetGlobal("v", table)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToLua converts XML node values to Lua variables
|
||||||
|
func (p *XMLProcessor) ToLuaTable(L *lua.LState, data interface{}) (lua.LValue, error) {
|
||||||
|
// Check if data is an xmlquery.Node
|
||||||
|
node, ok := data.(*xmlquery.Node)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("expected xmlquery.Node, got %T", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a simple table with essential data
|
||||||
|
table := L.NewTable()
|
||||||
|
|
||||||
|
// For element nodes, just provide basic info
|
||||||
|
L.SetField(table, "type", lua.LString(nodeTypeToString(node.Type)))
|
||||||
|
L.SetField(table, "name", lua.LString(node.Data))
|
||||||
|
L.SetField(table, "value", lua.LString(node.InnerText()))
|
||||||
|
|
||||||
|
// Add children if any
|
||||||
|
children := L.NewTable()
|
||||||
|
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
||||||
|
childTable, err := p.ToLuaTable(L, child)
|
||||||
|
if err == nil {
|
||||||
|
children.Append(childTable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
L.SetField(table, "children", children)
|
||||||
|
|
||||||
|
attrs := L.NewTable()
|
||||||
|
if len(node.Attr) > 0 {
|
||||||
|
for _, attr := range node.Attr {
|
||||||
|
L.SetField(attrs, attr.Name.Local, lua.LString(attr.Value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
L.SetField(table, "attr", attrs)
|
||||||
|
|
||||||
|
return table, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromLua gets modified values from Lua
|
||||||
|
func (p *XMLProcessor) FromLua(L *lua.LState) (interface{}, error) {
|
||||||
|
luaValue := L.GetGlobal("v")
|
||||||
|
|
||||||
|
// Handle string values directly
|
||||||
|
if luaValue.Type() == lua.LTString {
|
||||||
|
return luaValue.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle tables (for attributes and more complex updates)
|
||||||
|
if luaValue.Type() == lua.LTTable {
|
||||||
|
return luaTableToMap(L, luaValue.(*lua.LTable)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return luaValue.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple helper to convert a Lua table to a Go map
|
||||||
|
func luaTableToMap(L *lua.LState, table *lua.LTable) map[string]interface{} {
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
|
||||||
|
table.ForEach(func(k, v lua.LValue) {
|
||||||
|
if k.Type() == lua.LTString {
|
||||||
|
key := k.String()
|
||||||
|
|
||||||
|
if v.Type() == lua.LTTable {
|
||||||
|
result[key] = luaTableToMap(L, v.(*lua.LTable))
|
||||||
|
} else {
|
||||||
|
result[key] = v.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple helper to convert node type to string
|
||||||
|
func nodeTypeToString(nodeType xmlquery.NodeType) string {
|
||||||
|
switch nodeType {
|
||||||
|
case xmlquery.ElementNode:
|
||||||
|
return "element"
|
||||||
|
case xmlquery.TextNode:
|
||||||
|
return "text"
|
||||||
|
case xmlquery.AttributeNode:
|
||||||
|
return "attribute"
|
||||||
|
default:
|
||||||
|
return "other"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to update an XML node from a map
|
||||||
|
func updateNodeFromMap(node *xmlquery.Node, data map[string]interface{}) {
|
||||||
|
// Update node value if present
|
||||||
|
if value, ok := data["value"]; ok {
|
||||||
|
if strValue, ok := value.(string); ok {
|
||||||
|
// For element nodes, replace text content
|
||||||
|
if node.Type == xmlquery.ElementNode {
|
||||||
|
// Find the first text child if it exists
|
||||||
|
var textNode *xmlquery.Node
|
||||||
|
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
||||||
|
if child.Type == xmlquery.TextNode {
|
||||||
|
textNode = child
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if textNode != nil {
|
||||||
|
// Update existing text node
|
||||||
|
textNode.Data = strValue
|
||||||
|
} else {
|
||||||
|
// Create new text node
|
||||||
|
newText := &xmlquery.Node{
|
||||||
|
Type: xmlquery.TextNode,
|
||||||
|
Data: strValue,
|
||||||
|
Parent: node,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert at beginning of children
|
||||||
|
if node.FirstChild != nil {
|
||||||
|
newText.NextSibling = node.FirstChild
|
||||||
|
node.FirstChild.PrevSibling = newText
|
||||||
|
node.FirstChild = newText
|
||||||
|
} else {
|
||||||
|
node.FirstChild = newText
|
||||||
|
node.LastChild = newText
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if node.Type == xmlquery.TextNode {
|
||||||
|
// Directly update text node
|
||||||
|
node.Data = strValue
|
||||||
|
} else if node.Type == xmlquery.AttributeNode {
|
||||||
|
// Update attribute value
|
||||||
|
if node.Parent != nil {
|
||||||
|
for i, attr := range node.Parent.Attr {
|
||||||
|
if attr.Name.Local == node.Data {
|
||||||
|
node.Parent.Attr[i].Value = strValue
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update attributes if present
|
||||||
|
if attrs, ok := data["attr"].(map[string]interface{}); ok && node.Type == xmlquery.ElementNode {
|
||||||
|
for name, value := range attrs {
|
||||||
|
if strValue, ok := value.(string); ok {
|
||||||
|
// Look for existing attribute
|
||||||
|
found := false
|
||||||
|
for i, attr := range node.Attr {
|
||||||
|
if attr.Name.Local == name {
|
||||||
|
node.Attr[i].Value = strValue
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new attribute if not found
|
||||||
|
if !found {
|
||||||
|
node.Attr = append(node.Attr, xmlquery.Attr{
|
||||||
|
Name: struct {
|
||||||
|
Space, Local string
|
||||||
|
}{Local: name},
|
||||||
|
Value: strValue,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to get a string representation of node type
|
||||||
|
func nodeTypeName(nodeType xmlquery.NodeType) string {
|
||||||
|
switch nodeType {
|
||||||
|
case xmlquery.ElementNode:
|
||||||
|
return "element"
|
||||||
|
case xmlquery.TextNode:
|
||||||
|
return "text"
|
||||||
|
case xmlquery.AttributeNode:
|
||||||
|
return "attribute"
|
||||||
|
case xmlquery.CommentNode:
|
||||||
|
return "comment"
|
||||||
|
case xmlquery.DeclarationNode:
|
||||||
|
return "declaration"
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConvertToNamedEntities replaces numeric XML entities with their named counterparts
|
||||||
|
func ConvertToNamedEntities(xml string) string {
|
||||||
|
// Basic XML entities
|
||||||
|
replacements := map[string]string{
|
||||||
|
// Basic XML entities
|
||||||
|
""": """, // double quote
|
||||||
|
"'": "'", // single quote
|
||||||
|
"<": "<", // less than
|
||||||
|
">": ">", // greater than
|
||||||
|
"&": "&", // ampersand
|
||||||
|
|
||||||
|
// Common symbols
|
||||||
|
" ": " ", // non-breaking space
|
||||||
|
"©": "©", // copyright
|
||||||
|
"®": "®", // registered trademark
|
||||||
|
"€": "€", // euro
|
||||||
|
"£": "£", // pound
|
||||||
|
"¥": "¥", // yen
|
||||||
|
"¢": "¢", // cent
|
||||||
|
"§": "§", // section
|
||||||
|
"™": "™", // trademark
|
||||||
|
"♠": "♠", // spade
|
||||||
|
"♣": "♣", // club
|
||||||
|
"♥": "♥", // heart
|
||||||
|
"♦": "♦", // diamond
|
||||||
|
|
||||||
|
// Special characters
|
||||||
|
"¡": "¡", // inverted exclamation
|
||||||
|
"¿": "¿", // inverted question
|
||||||
|
"«": "«", // left angle quotes
|
||||||
|
"»": "»", // right angle quotes
|
||||||
|
"·": "·", // middle dot
|
||||||
|
"•": "•", // bullet
|
||||||
|
"…": "…", // horizontal ellipsis
|
||||||
|
"′": "′", // prime
|
||||||
|
"″": "″", // double prime
|
||||||
|
"‾": "‾", // overline
|
||||||
|
"⁄": "⁄", // fraction slash
|
||||||
|
|
||||||
|
// Math symbols
|
||||||
|
"±": "±", // plus-minus
|
||||||
|
"×": "×", // multiplication
|
||||||
|
"÷": "÷", // division
|
||||||
|
"∞": "∞", // infinity
|
||||||
|
"≈": "≈", // almost equal
|
||||||
|
"≠": "≠", // not equal
|
||||||
|
"≤": "≤", // less than or equal
|
||||||
|
"≥": "≥", // greater than or equal
|
||||||
|
"∑": "∑", // summation
|
||||||
|
"√": "√", // square root
|
||||||
|
"∫": "∫", // integral
|
||||||
|
|
||||||
|
// Accented characters
|
||||||
|
"À": "À", // A grave
|
||||||
|
"Á": "Á", // A acute
|
||||||
|
"Â": "Â", // A circumflex
|
||||||
|
"Ã": "Ã", // A tilde
|
||||||
|
"Ä": "Ä", // A umlaut
|
||||||
|
"Å": "Å", // A ring
|
||||||
|
"Æ": "Æ", // AE ligature
|
||||||
|
"Ç": "Ç", // C cedilla
|
||||||
|
"È": "È", // E grave
|
||||||
|
"É": "É", // E acute
|
||||||
|
"Ê": "Ê", // E circumflex
|
||||||
|
"Ë": "Ë", // E umlaut
|
||||||
|
"Ì": "Ì", // I grave
|
||||||
|
"Í": "Í", // I acute
|
||||||
|
"Î": "Î", // I circumflex
|
||||||
|
"Ï": "Ï", // I umlaut
|
||||||
|
"Ð": "Ð", // Eth
|
||||||
|
"Ñ": "Ñ", // N tilde
|
||||||
|
"Ò": "Ò", // O grave
|
||||||
|
"Ó": "Ó", // O acute
|
||||||
|
"Ô": "Ô", // O circumflex
|
||||||
|
"Õ": "Õ", // O tilde
|
||||||
|
"Ö": "Ö", // O umlaut
|
||||||
|
"Ø": "Ø", // O slash
|
||||||
|
"Ù": "Ù", // U grave
|
||||||
|
"Ú": "Ú", // U acute
|
||||||
|
"Û": "Û", // U circumflex
|
||||||
|
"Ü": "Ü", // U umlaut
|
||||||
|
"Ý": "Ý", // Y acute
|
||||||
|
"Þ": "Þ", // Thorn
|
||||||
|
"ß": "ß", // Sharp s
|
||||||
|
"à": "à", // a grave
|
||||||
|
"á": "á", // a acute
|
||||||
|
"â": "â", // a circumflex
|
||||||
|
"ã": "ã", // a tilde
|
||||||
|
"ä": "ä", // a umlaut
|
||||||
|
"å": "å", // a ring
|
||||||
|
"æ": "æ", // ae ligature
|
||||||
|
"ç": "ç", // c cedilla
|
||||||
|
"è": "è", // e grave
|
||||||
|
"é": "é", // e acute
|
||||||
|
"ê": "ê", // e circumflex
|
||||||
|
"ë": "ë", // e umlaut
|
||||||
|
"ì": "ì", // i grave
|
||||||
|
"í": "í", // i acute
|
||||||
|
"î": "î", // i circumflex
|
||||||
|
"ï": "ï", // i umlaut
|
||||||
|
"ð": "ð", // eth
|
||||||
|
"ñ": "ñ", // n tilde
|
||||||
|
"ò": "ò", // o grave
|
||||||
|
"ó": "ó", // o acute
|
||||||
|
"ô": "ô", // o circumflex
|
||||||
|
"õ": "õ", // o tilde
|
||||||
|
"ö": "ö", // o umlaut
|
||||||
|
"ø": "ø", // o slash
|
||||||
|
"ù": "ù", // u grave
|
||||||
|
"ú": "ú", // u acute
|
||||||
|
"û": "û", // u circumflex
|
||||||
|
"ü": "ü", // u umlaut
|
||||||
|
"ý": "ý", // y acute
|
||||||
|
"þ": "þ", // thorn
|
||||||
|
"ÿ": "ÿ", // y umlaut
|
||||||
|
}
|
||||||
|
|
||||||
|
result := xml
|
||||||
|
for numeric, named := range replacements {
|
||||||
|
result = strings.ReplaceAll(result, numeric, named)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
1775
processor/xml_test.go
Normal file
1775
processor/xml_test.go
Normal file
File diff suppressed because it is too large
Load Diff
4
processor/xpath/parser_manual_test.go
Normal file
4
processor/xpath/parser_manual_test.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// The package is now using github.com/antchfx/xmlquery for XPath parsing.
|
||||||
|
// The parsing functionality tests have been removed since we're now
|
||||||
|
// delegating XPath parsing to the xmlquery library.
|
||||||
|
package xpath
|
4
processor/xpath/parser_test.go
Normal file
4
processor/xpath/parser_test.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// The package is now using github.com/antchfx/xmlquery for XPath parsing.
|
||||||
|
// The parsing functionality tests have been removed since we're now
|
||||||
|
// delegating XPath parsing to the xmlquery library.
|
||||||
|
package xpath
|
133
processor/xpath/xpath.go
Normal file
133
processor/xpath/xpath.go
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
package xpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/antchfx/xmlquery"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get retrieves nodes from XML data using an XPath expression
|
||||||
|
func Get(node *xmlquery.Node, path string) ([]*xmlquery.Node, error) {
|
||||||
|
if node == nil {
|
||||||
|
return nil, errors.New("nil node provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute xpath query directly
|
||||||
|
nodes, err := xmlquery.QueryAll(node, path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to execute XPath query: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set updates a single node in the XML data using an XPath expression
|
||||||
|
func Set(node *xmlquery.Node, path string, value interface{}) error {
|
||||||
|
if node == nil {
|
||||||
|
return errors.New("nil node provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the node to update
|
||||||
|
nodes, err := xmlquery.QueryAll(node, path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to execute XPath query: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(nodes) == 0 {
|
||||||
|
return fmt.Errorf("no nodes found for path: %s", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the first matching node
|
||||||
|
updateNodeValue(nodes[0], value)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAll updates all nodes that match the XPath expression
|
||||||
|
func SetAll(node *xmlquery.Node, path string, value interface{}) error {
|
||||||
|
if node == nil {
|
||||||
|
return errors.New("nil node provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find all nodes to update
|
||||||
|
nodes, err := xmlquery.QueryAll(node, path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to execute XPath query: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(nodes) == 0 {
|
||||||
|
return fmt.Errorf("no nodes found for path: %s", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update all matching nodes
|
||||||
|
for _, matchNode := range nodes {
|
||||||
|
updateNodeValue(matchNode, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to update a node's value
|
||||||
|
func updateNodeValue(node *xmlquery.Node, value interface{}) {
|
||||||
|
strValue := fmt.Sprintf("%v", value)
|
||||||
|
|
||||||
|
// Handle different node types
|
||||||
|
switch node.Type {
|
||||||
|
case xmlquery.AttributeNode:
|
||||||
|
// For attribute nodes, update the attribute value
|
||||||
|
parent := node.Parent
|
||||||
|
if parent != nil {
|
||||||
|
for i, attr := range parent.Attr {
|
||||||
|
if attr.Name.Local == node.Data {
|
||||||
|
parent.Attr[i].Value = strValue
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case xmlquery.TextNode:
|
||||||
|
// For text nodes, update the text content
|
||||||
|
node.Data = strValue
|
||||||
|
case xmlquery.ElementNode:
|
||||||
|
// For element nodes, clear existing text children and add a new text node
|
||||||
|
// First, remove all existing text children
|
||||||
|
var nonTextChildren []*xmlquery.Node
|
||||||
|
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
||||||
|
if child.Type != xmlquery.TextNode {
|
||||||
|
nonTextChildren = append(nonTextChildren, child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear all children
|
||||||
|
node.FirstChild = nil
|
||||||
|
node.LastChild = nil
|
||||||
|
|
||||||
|
// Add a new text node
|
||||||
|
textNode := &xmlquery.Node{
|
||||||
|
Type: xmlquery.TextNode,
|
||||||
|
Data: strValue,
|
||||||
|
Parent: node,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the text node as the first child
|
||||||
|
node.FirstChild = textNode
|
||||||
|
node.LastChild = textNode
|
||||||
|
|
||||||
|
// Add back non-text children
|
||||||
|
for _, child := range nonTextChildren {
|
||||||
|
child.Parent = node
|
||||||
|
|
||||||
|
// If this is the first child being added back
|
||||||
|
if node.FirstChild == textNode && node.LastChild == textNode {
|
||||||
|
node.FirstChild.NextSibling = child
|
||||||
|
child.PrevSibling = node.FirstChild
|
||||||
|
node.LastChild = child
|
||||||
|
} else {
|
||||||
|
// Add to the end of the chain
|
||||||
|
node.LastChild.NextSibling = child
|
||||||
|
child.PrevSibling = node.LastChild
|
||||||
|
node.LastChild = child
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
474
processor/xpath/xpath_test.go
Normal file
474
processor/xpath/xpath_test.go
Normal file
@@ -0,0 +1,474 @@
|
|||||||
|
package xpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/antchfx/xmlquery"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Parse test XML data once at the beginning for use in multiple tests
|
||||||
|
func parseTestXML(t *testing.T, xmlData string) *xmlquery.Node {
|
||||||
|
doc, err := xmlquery.Parse(strings.NewReader(xmlData))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to parse test XML: %v", err)
|
||||||
|
}
|
||||||
|
return doc
|
||||||
|
}
|
||||||
|
|
||||||
|
// XML test data as a string for our tests
|
||||||
|
var testXML = `
|
||||||
|
<store>
|
||||||
|
<book category="fiction">
|
||||||
|
<title lang="en">The Fellowship of the Ring</title>
|
||||||
|
<author>J.R.R. Tolkien</author>
|
||||||
|
<year>1954</year>
|
||||||
|
<price>22.99</price>
|
||||||
|
</book>
|
||||||
|
<book category="fiction">
|
||||||
|
<title lang="en">The Two Towers</title>
|
||||||
|
<author>J.R.R. Tolkien</author>
|
||||||
|
<year>1954</year>
|
||||||
|
<price>23.45</price>
|
||||||
|
</book>
|
||||||
|
<book category="technical">
|
||||||
|
<title lang="en">Learning XML</title>
|
||||||
|
<author>Erik T. Ray</author>
|
||||||
|
<year>2003</year>
|
||||||
|
<price>39.95</price>
|
||||||
|
</book>
|
||||||
|
<bicycle>
|
||||||
|
<color>red</color>
|
||||||
|
<price>199.95</price>
|
||||||
|
</bicycle>
|
||||||
|
</store>
|
||||||
|
`
|
||||||
|
|
||||||
|
func TestEvaluator(t *testing.T) {
|
||||||
|
// Parse the test XML data once for all test cases
|
||||||
|
doc := parseTestXML(t, testXML)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple_element_access",
|
||||||
|
path: "/store/bicycle/color",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "recursive_element_access",
|
||||||
|
path: "//price",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wildcard_element_access",
|
||||||
|
path: "/store/book/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "attribute_exists_predicate",
|
||||||
|
path: "//title[@lang]",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "attribute_equals_predicate",
|
||||||
|
path: "//title[@lang='en']",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "value_comparison_predicate",
|
||||||
|
path: "/store/book[price>35.00]/title",
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "last_predicate",
|
||||||
|
path: "/store/book[last()]/title",
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "last_minus_predicate",
|
||||||
|
path: "/store/book[last()-1]/title",
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "position_predicate",
|
||||||
|
path: "/store/book[position()<3]/title",
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid_index",
|
||||||
|
path: "/store/book[10]/title",
|
||||||
|
error: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nonexistent_element",
|
||||||
|
path: "/store/nonexistent",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := Get(doc, tt.path)
|
||||||
|
|
||||||
|
// Handle expected errors
|
||||||
|
if tt.error {
|
||||||
|
if err == nil && len(result) == 0 {
|
||||||
|
// If we expected an error but got empty results instead, that's okay
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// If we got an error as expected, that's okay
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
// If we didn't expect an error but got one, that's a test failure
|
||||||
|
t.Errorf("Get(%q) returned unexpected error: %v", tt.path, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Special cases where we don't care about exact matches
|
||||||
|
switch tt.name {
|
||||||
|
case "wildcard_element_access":
|
||||||
|
// Just check that we got some elements
|
||||||
|
if len(result) == 0 {
|
||||||
|
t.Errorf("Expected multiple elements for wildcard, got none")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case "attribute_exists_predicate", "attribute_equals_predicate":
|
||||||
|
// Just check that we got some titles
|
||||||
|
if len(result) == 0 {
|
||||||
|
t.Errorf("Expected titles with lang attribute, got none")
|
||||||
|
}
|
||||||
|
// Ensure all are title elements
|
||||||
|
for _, node := range result {
|
||||||
|
if node.Data != "title" {
|
||||||
|
t.Errorf("Expected title elements, got: %s", node.Data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case "nonexistent_element":
|
||||||
|
// Just check that we got empty results
|
||||||
|
if len(result) != 0 {
|
||||||
|
t.Errorf("Expected empty results for nonexistent element, got %d items", len(result))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For other cases, just verify we got results
|
||||||
|
if len(result) == 0 {
|
||||||
|
t.Errorf("Expected results for path %s, got none", tt.path)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeCases(t *testing.T) {
|
||||||
|
t.Run("nil_node", func(t *testing.T) {
|
||||||
|
result, err := Get(nil, "/store/book")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error for nil node")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) > 0 {
|
||||||
|
t.Errorf("Expected empty result, got %v", result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid_xml", func(t *testing.T) {
|
||||||
|
invalidXML, err := xmlquery.Parse(strings.NewReader("<invalid>xml"))
|
||||||
|
if err != nil {
|
||||||
|
// If parsing fails, that's expected
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = Get(invalidXML, "/store")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for invalid XML structure")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// For these tests with the simple XML, we expect just one result
|
||||||
|
simpleXML := `<root><book><title lang="en">Test</title></book></root>`
|
||||||
|
doc := parseTestXML(t, simpleXML)
|
||||||
|
|
||||||
|
t.Run("current_node", func(t *testing.T) {
|
||||||
|
result, err := Get(doc, "/root/book/.")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) > 1 {
|
||||||
|
t.Errorf("Expected at most 1 result, got %d", len(result))
|
||||||
|
}
|
||||||
|
if len(result) > 0 {
|
||||||
|
// Verify it's the book node
|
||||||
|
if result[0].Data != "book" {
|
||||||
|
t.Errorf("Expected book node, got %v", result[0].Data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("attributes", func(t *testing.T) {
|
||||||
|
result, err := Get(doc, "/root/book/title/@lang")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) != 1 || result[0].InnerText() != "en" {
|
||||||
|
t.Errorf("Expected 'en', got %v", result[0].InnerText())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetWithPaths(t *testing.T) {
|
||||||
|
// Use a simplified, well-formed XML document
|
||||||
|
simpleXML := `<store>
|
||||||
|
<book category="fiction">
|
||||||
|
<title lang="en">The Book Title</title>
|
||||||
|
<author>Author Name</author>
|
||||||
|
<price>19.99</price>
|
||||||
|
</book>
|
||||||
|
<bicycle>
|
||||||
|
<color>red</color>
|
||||||
|
<price>199.95</price>
|
||||||
|
</bicycle>
|
||||||
|
</store>`
|
||||||
|
|
||||||
|
// Parse the XML for testing
|
||||||
|
doc := parseTestXML(t, simpleXML)
|
||||||
|
|
||||||
|
// Debug: Print the test XML
|
||||||
|
t.Logf("Test XML:\n%s", simpleXML)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
expectedValue string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple_element_access",
|
||||||
|
path: "/store/bicycle/color",
|
||||||
|
expectedValue: "red",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "attribute_access",
|
||||||
|
path: "/store/book/title/@lang",
|
||||||
|
expectedValue: "en",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "recursive_with_attribute",
|
||||||
|
path: "//title[@lang='en']",
|
||||||
|
expectedValue: "The Book Title",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// Debug: Print the path we're looking for
|
||||||
|
t.Logf("Looking for path: %s", tt.path)
|
||||||
|
|
||||||
|
result, err := Get(doc, tt.path)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get(%q) returned error: %v", tt.path, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug: Print the results
|
||||||
|
t.Logf("Got %d results", len(result))
|
||||||
|
for i, r := range result {
|
||||||
|
t.Logf("Result %d: Node=%s, Value=%v", i, r.Data, r.InnerText())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that we got results
|
||||||
|
if len(result) == 0 {
|
||||||
|
t.Errorf("Get(%q) returned no results", tt.path)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For attribute access test, do more specific checks
|
||||||
|
if tt.name == "attribute_access" {
|
||||||
|
// Check the first result's value matches expected
|
||||||
|
if result[0].InnerText() != tt.expectedValue {
|
||||||
|
t.Errorf("Attribute value: got %v, expected %s", result[0].InnerText(), tt.expectedValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For simple element access, check the text content
|
||||||
|
if tt.name == "simple_element_access" {
|
||||||
|
if text := result[0].InnerText(); text != tt.expectedValue {
|
||||||
|
t.Errorf("Element text: got %s, expected %s", text, tt.expectedValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For recursive with attribute test, check title elements with lang="en"
|
||||||
|
if tt.name == "recursive_with_attribute" {
|
||||||
|
for _, node := range result {
|
||||||
|
// Check the node is a title
|
||||||
|
if node.Data != "title" {
|
||||||
|
t.Errorf("Expected title element, got %s", node.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check text content
|
||||||
|
if text := node.InnerText(); text != tt.expectedValue {
|
||||||
|
t.Errorf("Text content: got %s, expected %s", text, tt.expectedValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check attributes - find the lang attribute
|
||||||
|
hasLang := false
|
||||||
|
for _, attr := range node.Attr {
|
||||||
|
if attr.Name.Local == "lang" && attr.Value == "en" {
|
||||||
|
hasLang = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hasLang {
|
||||||
|
t.Errorf("Expected lang=\"en\" attribute, but it was not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSet(t *testing.T) {
|
||||||
|
t.Run("simple element", func(t *testing.T) {
|
||||||
|
xmlData := `<root><name>John</name></root>`
|
||||||
|
doc := parseTestXML(t, xmlData)
|
||||||
|
|
||||||
|
err := Set(doc, "/root/name", "Jane")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the change
|
||||||
|
result, err := Get(doc, "/root/name")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) != 1 {
|
||||||
|
t.Errorf("Expected 1 result, got %d", len(result))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check text content
|
||||||
|
if text := result[0].InnerText(); text != "Jane" {
|
||||||
|
t.Errorf("Expected text 'Jane', got '%s'", text)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("attribute", func(t *testing.T) {
|
||||||
|
xmlData := `<root><element id="123"></element></root>`
|
||||||
|
doc := parseTestXML(t, xmlData)
|
||||||
|
|
||||||
|
err := Set(doc, "/root/element/@id", "456")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the change
|
||||||
|
result, err := Get(doc, "/root/element/@id")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) != 1 {
|
||||||
|
t.Errorf("Expected 1 result, got %d", len(result))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For attributes, check the inner text
|
||||||
|
if text := result[0].InnerText(); text != "456" {
|
||||||
|
t.Errorf("Expected attribute value '456', got '%s'", text)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("indexed element", func(t *testing.T) {
|
||||||
|
xmlData := `<root><items><item>first</item><item>second</item></items></root>`
|
||||||
|
doc := parseTestXML(t, xmlData)
|
||||||
|
|
||||||
|
err := Set(doc, "/root/items/item[1]", "changed")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Set() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the change using XPath that specifically targets the first item
|
||||||
|
result, err := Get(doc, "/root/items/item[1]")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we have results
|
||||||
|
if len(result) == 0 {
|
||||||
|
t.Errorf("Expected at least one result for /root/items/item[1]")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check text content
|
||||||
|
if text := result[0].InnerText(); text != "changed" {
|
||||||
|
t.Errorf("Expected text 'changed', got '%s'", text)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetAll(t *testing.T) {
|
||||||
|
t.Run("multiple elements", func(t *testing.T) {
|
||||||
|
xmlData := `<root><items><item>first</item><item>second</item></items></root>`
|
||||||
|
doc := parseTestXML(t, xmlData)
|
||||||
|
|
||||||
|
err := SetAll(doc, "//item", "changed")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SetAll() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify all items are changed
|
||||||
|
result, err := Get(doc, "//item")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) != 2 {
|
||||||
|
t.Errorf("Expected 2 results, got %d", len(result))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check each node
|
||||||
|
for i, node := range result {
|
||||||
|
if text := node.InnerText(); text != "changed" {
|
||||||
|
t.Errorf("Item %d: expected text 'changed', got '%s'", i, text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("attributes", func(t *testing.T) {
|
||||||
|
xmlData := `<root><item id="1"/><item id="2"/></root>`
|
||||||
|
doc := parseTestXML(t, xmlData)
|
||||||
|
|
||||||
|
err := SetAll(doc, "//item/@id", "new")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SetAll() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify all attributes are changed
|
||||||
|
result, err := Get(doc, "//item/@id")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Get() returned error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(result) != 2 {
|
||||||
|
t.Errorf("Expected 2 results, got %d", len(result))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For attributes, check inner text
|
||||||
|
for i, node := range result {
|
||||||
|
if text := node.InnerText(); text != "new" {
|
||||||
|
t.Errorf("Attribute %d: expected value 'new', got '%s'", i, text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
48
release.sh
Normal file
48
release.sh
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "Figuring out the tag..."
|
||||||
|
TAG=$(git describe --tags --exact-match 2>/dev/null || echo "")
|
||||||
|
if [ -z "$TAG" ]; then
|
||||||
|
# Get the latest tag
|
||||||
|
LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
|
||||||
|
# Increment the patch version
|
||||||
|
IFS='.' read -r -a VERSION_PARTS <<< "$LATEST_TAG"
|
||||||
|
VERSION_PARTS[2]=$((VERSION_PARTS[2]+1))
|
||||||
|
TAG="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}"
|
||||||
|
# Create a new tag
|
||||||
|
git tag $TAG
|
||||||
|
git push origin $TAG
|
||||||
|
fi
|
||||||
|
echo "Tag: $TAG"
|
||||||
|
|
||||||
|
echo "Building the thing..."
|
||||||
|
go build -o chef.exe .
|
||||||
|
|
||||||
|
echo "Creating a release..."
|
||||||
|
TOKEN="$GITEA_API_KEY"
|
||||||
|
GITEA="https://git.site.quack-lab.dev"
|
||||||
|
REPO="dave/BigChef"
|
||||||
|
# Create a release
|
||||||
|
RELEASE_RESPONSE=$(curl -s -X POST \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
-H "Accept: application/json" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"tag_name": "'"$TAG"'",
|
||||||
|
"name": "'"$TAG"'",
|
||||||
|
"draft": false,
|
||||||
|
"prerelease": false
|
||||||
|
}' \
|
||||||
|
$GITEA/api/v1/repos/$REPO/releases)
|
||||||
|
|
||||||
|
# Extract the release ID
|
||||||
|
echo $RELEASE_RESPONSE
|
||||||
|
RELEASE_ID=$(echo $RELEASE_RESPONSE | awk -F'"id":' '{print $2+0; exit}')
|
||||||
|
echo "Release ID: $RELEASE_ID"
|
||||||
|
|
||||||
|
echo "Uploading the things..."
|
||||||
|
curl -X POST \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
-F "attachment=@chef.exe" \
|
||||||
|
"$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=chef.exe"
|
||||||
|
rm chef.exe
|
Reference in New Issue
Block a user