<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main/command.go24
-rw-r--r--main/main_test.go2
2 files changed, 9 insertions, 17 deletions
diff --git a/main/command.go b/main/command.go
index 04ac7f6..736dce5 100644
--- a/main/command.go
+++ b/main/command.go
@@ -119,24 +119,16 @@ func (cmd SubstituteAppendNextCommand) String() string {
type MergeCommand struct {}
func (cmd MergeCommand) exec(state *ProgramState) {
- nextItem, err := state.Read()
- if err != nil {
- panic("Missing next value")
+ if len(state.value) <= 1 {
+ state.pc++
+ return
}
- state.prevStart = nextItem.PrevStart
- state.start = nextItem.Start
- state.end = nextItem.End
- state.nextEnd = nextItem.NextEnd
-
- if len(state.value) == 0 {
- state.value = []walk.Value {nextItem.Value}
- } else {
- state.value = append(
- state.value[:len(state.value) - 1],
- walk.Merge(state.value[len(state.value) - 1], nextItem.Value)...
- )
- }
+ newVals := walk.Merge(state.value[len(state.value) - 2], state.value[len(state.value) - 1])
+ state.value = append(
+ state.value[:len(state.value) - 2],
+ newVals...
+ )
state.pc++
}
diff --git a/main/main_test.go b/main/main_test.go
index be439a3..1510497 100644
--- a/main/main_test.go
+++ b/main/main_test.go
@@ -75,7 +75,7 @@ func TestMain(t *testing.T) {
},
{
name: "Change full names in place",
- program: "s/#(\"people\" @(. #(\"first_name\" .)#)@)#/{ ms/#(\"people\" @(. (#(\"first_name\" \".{-0}$a\" \"last_name\" \".{-0}$b\")#$_) `#(\"name\" \"$a $b\")#`)@)#/ }",
+ program: "s/#(\"people\" @(. #(\"first_name\" .)#)@)#/{ Nms/#(\"people\" @(. (#(\"first_name\" \".{-0}$a\" \"last_name\" \".{-0}$b\")#$_) `#(\"name\" \"$a $b\")#`)@)#/ }",
input: miscInput,
expected: `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are","values"],"people":[{"name":"Charlie Johnson","age":22},{"name":"Tom Johnson","age":18},{"name":"Charlie Chaplin","age":122},{"name":"John Johnson","age":48}]}`,
},