1
0
mirror of https://github.com/TomWright/dasel.git synced 2022-05-22 02:32:45 +03:00

Merge pull request #160 from TomWright/issue-159

Fix an issue when parsing selectors containing unicode characters
This commit is contained in:
Tom Wright
2021-08-30 16:14:00 +01:00
committed by GitHub
5 changed files with 45 additions and 5 deletions

View File

@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `-v`, `--value` flag to workaround [dash issue](https://github.com/TomWright/dasel/issues/117).
### Fixed
- Fixed an issue in which unicode characters could cause issues when parsing selectors.
## [v1.19.0] - 2021-08-14
### Added

View File

@@ -648,6 +648,18 @@ ECB,European Central Bank
ESTAT,Eurostat
ILO,International Labor Organization
`, nil, "-w", "csv"))
// https://github.com/TomWright/dasel/issues/159
t.Run("SelectFilterOnUnicode", selectTest(`
{
"data": [
{"name": "Fu Shun", "ship_type": "Destroyer"},
{"name": "Sheffield", "ship_type": "Light Cruiser"},
{"name": "Ägir", "ship_type": "Large Cruiser"}
]
}
`, "json", ".data.(name=Ägir).ship_type", `"Large Cruiser"
`, nil))
}
func TestRootCmd_Select_JSON_Format(t *testing.T) {

View File

@@ -369,6 +369,18 @@ func TestParseSelector(t *testing.T) {
},
},
}))
t.Run("Unicode", testParseSelector(".(name=Ägir).b", dasel.Selector{
Raw: ".(name=Ägir).b",
Current: ".(name=Ägir)",
Remaining: ".b",
Type: "DYNAMIC",
Conditions: []dasel.Condition{
&dasel.EqualCondition{
Key: "name",
Value: "Ägir",
},
},
}))
}
func extractValues(nodes []*dasel.Node) []interface{} {

View File

@@ -15,10 +15,12 @@ func ExtractNextSelector(input string) (string, int) {
i := 0
read := 0
for k, v := range input {
curRuneStr := string(v)
curRuneLength := len(curRuneStr)
if escapedIndex == k-1 && k != 0 {
// last character was escape character
res += string(v)
read++
res += curRuneStr
read += curRuneLength
continue
}
@@ -30,15 +32,15 @@ func ExtractNextSelector(input string) (string, int) {
if v == '\\' {
escapedIndex = k
read++
read += curRuneLength
continue
}
if i == 0 && v == '.' && k != 0 {
break
}
res += string(v)
read++
res += curRuneStr
read += curRuneLength
}
return res, read
}

View File

@@ -49,6 +49,7 @@ func TestExtractNextSelector(t *testing.T) {
t.Run("SimpleProp", testExtractNextSelector(`.name`, `.name`, 5))
t.Run("SimpleIndex", testExtractNextSelector(`.[123]`, `.[123]`, 6))
t.Run("SimpleLength", testExtractNextSelector(`.[#]`, `.[#]`, 4))
t.Run("UnicodeCharacter", testExtractNextSelector(`.(name=Ägir).asd`, `.(name=Ägir)`, 13))
}
func TestDynamicSelectorToGroups(t *testing.T) {
@@ -74,6 +75,10 @@ func TestDynamicSelectorToGroups(t *testing.T) {
"a=.",
"b=2",
}))
t.Run("Unicode", testDynamicSelectorToGroups("(a=.)(b=Ägir)", []string{
"a=.",
"b=Ägir",
}))
}
func TestFindDynamicSelectorParts(t *testing.T) {
@@ -102,6 +107,11 @@ func TestFindDynamicSelectorParts(t *testing.T) {
Comparison: "<=",
Value: "b",
}))
t.Run("UnicodeEqual", testFindDynamicSelectorParts("name=Ägir", dasel.DynamicSelectorParts{
Key: "name",
Comparison: "=",
Value: "Ägir",
}))
t.Run("NestedGroupIgnored", testFindDynamicSelectorParts("(.(x=y)).x=1", dasel.DynamicSelectorParts{
Key: "(.(x=y)).x",
Comparison: "=",