mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 14:25:38 +00:00
Compare commits
4 Commits
820c419380
...
8f63daf585
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f63daf585 | ||
|
|
341e2524b9 | ||
|
|
778088d70c | ||
|
|
9a9399ad00 |
@ -110,11 +110,11 @@ country: Australia
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
yq '.country[4:]' sample.yml
|
yq '.country[0:5]' sample.yml
|
||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
ralia
|
Austr
|
||||||
```
|
```
|
||||||
|
|
||||||
## Slicing strings - without the second number
|
## Slicing strings - without the second number
|
||||||
@ -126,11 +126,11 @@ country: Australia
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
yq '.country[0:5]' sample.yml
|
yq '.country[5:]' sample.yml
|
||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
Austr
|
alia
|
||||||
```
|
```
|
||||||
|
|
||||||
## Slicing strings - without the first number
|
## Slicing strings - without the first number
|
||||||
|
|||||||
@ -16,7 +16,7 @@ func getSliceNumber(d *dataTreeNavigator, context Context, node *CandidateNode,
|
|||||||
return parseInt(result.MatchingNodes.Front().Value.(*CandidateNode).Value)
|
return parseInt(result.MatchingNodes.Front().Value.(*CandidateNode).Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sliceStringNode(lhsNode *CandidateNode, firstNumber int, secondNumber int) (*CandidateNode, error) {
|
func sliceStringNode(lhsNode *CandidateNode, firstNumber int, secondNumber int) *CandidateNode {
|
||||||
runes := []rune(lhsNode.Value)
|
runes := []rune(lhsNode.Value)
|
||||||
length := len(runes)
|
length := len(runes)
|
||||||
|
|
||||||
@ -45,7 +45,9 @@ func sliceStringNode(lhsNode *CandidateNode, firstNumber int, secondNumber int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
slicedString := string(runes[relativeFirstNumber:relativeSecondNumber])
|
slicedString := string(runes[relativeFirstNumber:relativeSecondNumber])
|
||||||
return lhsNode.CreateReplacement(ScalarNode, "!!str", slicedString), nil
|
replacement := lhsNode.CreateReplacement(ScalarNode, lhsNode.Tag, slicedString)
|
||||||
|
replacement.Style = lhsNode.Style
|
||||||
|
return replacement
|
||||||
}
|
}
|
||||||
|
|
||||||
func sliceArrayOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func sliceArrayOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
@ -70,11 +72,7 @@ func sliceArrayOperator(d *dataTreeNavigator, context Context, expressionNode *E
|
|||||||
}
|
}
|
||||||
|
|
||||||
if lhsNode.Kind == ScalarNode && lhsNode.guessTagFromCustomType() == "!!str" {
|
if lhsNode.Kind == ScalarNode && lhsNode.guessTagFromCustomType() == "!!str" {
|
||||||
slicedNode, err := sliceStringNode(lhsNode, firstNumber, secondNumber)
|
results.PushBack(sliceStringNode(lhsNode, firstNumber, secondNumber))
|
||||||
if err != nil {
|
|
||||||
return Context{}, err
|
|
||||||
}
|
|
||||||
results.PushBack(slicedNode)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,13 +80,24 @@ func sliceArrayOperator(d *dataTreeNavigator, context Context, expressionNode *E
|
|||||||
if relativeFirstNumber < 0 {
|
if relativeFirstNumber < 0 {
|
||||||
relativeFirstNumber = len(lhsNode.Content) + firstNumber
|
relativeFirstNumber = len(lhsNode.Content) + firstNumber
|
||||||
}
|
}
|
||||||
|
if relativeFirstNumber < 0 {
|
||||||
|
relativeFirstNumber = 0
|
||||||
|
} else if relativeFirstNumber > len(lhsNode.Content) {
|
||||||
|
relativeFirstNumber = len(lhsNode.Content)
|
||||||
|
}
|
||||||
|
|
||||||
relativeSecondNumber := secondNumber
|
relativeSecondNumber := secondNumber
|
||||||
if relativeSecondNumber < 0 {
|
if relativeSecondNumber < 0 {
|
||||||
relativeSecondNumber = len(lhsNode.Content) + secondNumber
|
relativeSecondNumber = len(lhsNode.Content) + secondNumber
|
||||||
|
}
|
||||||
|
if relativeSecondNumber < 0 {
|
||||||
|
relativeSecondNumber = 0
|
||||||
} else if relativeSecondNumber > len(lhsNode.Content) {
|
} else if relativeSecondNumber > len(lhsNode.Content) {
|
||||||
relativeSecondNumber = len(lhsNode.Content)
|
relativeSecondNumber = len(lhsNode.Content)
|
||||||
}
|
}
|
||||||
|
if relativeSecondNumber < relativeFirstNumber {
|
||||||
|
relativeSecondNumber = relativeFirstNumber
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("calculateIndicesToTraverse: slice from %v to %v", relativeFirstNumber, relativeSecondNumber)
|
log.Debugf("calculateIndicesToTraverse: slice from %v to %v", relativeFirstNumber, relativeSecondNumber)
|
||||||
|
|
||||||
|
|||||||
@ -98,21 +98,37 @@ var sliceArrayScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!seq)::- cat1\n",
|
"D0, P[], (!!seq)::- cat1\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `[cat, dog, frog]`,
|
||||||
|
expression: `.[-100:]`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!seq)::- cat\n- dog\n- frog\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `[cat, dog, frog]`,
|
||||||
|
expression: `.[:-100]`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!seq)::[]\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Slicing strings",
|
description: "Slicing strings",
|
||||||
document: `country: Australia`,
|
document: `country: Australia`,
|
||||||
expression: `.country[4:]`,
|
expression: `.country[0:5]`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[country], (!!str)::ralia\n",
|
"D0, P[country], (!!str)::Austr\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Slicing strings - without the second number",
|
description: "Slicing strings - without the second number",
|
||||||
subdescription: "Finishes at the end of the string",
|
subdescription: "Finishes at the end of the string",
|
||||||
document: `country: Australia`,
|
document: `country: Australia`,
|
||||||
expression: `.country[0:5]`,
|
expression: `.country[5:]`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[country], (!!str)::Austr\n",
|
"D0, P[country], (!!str)::alia\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -303,3 +303,4 @@ ralia
|
|||||||
Austr
|
Austr
|
||||||
ustrali
|
ustrali
|
||||||
héllo
|
héllo
|
||||||
|
alia
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user