Skip to content

Commit

Permalink
call OnEndEdit always(!) if OnBeginEdit called successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
oligon-fvs authored and dhubler committed May 20, 2024
1 parent 0ef5e8b commit c334a23
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
14 changes: 8 additions & 6 deletions node/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ func (e editor) edit(from *Selection, to *Selection, s editStrategy) (err error)
return nil
}

func (e editor) enter(from *Selection, to *Selection, new bool, strategy editStrategy, root bool, bubble bool) error {
if err := to.beginEdit(NodeRequest{New: new, Source: to, EditRoot: root}, bubble); err != nil {
return err
func (e editor) enter(from *Selection, to *Selection, new bool, strategy editStrategy, root bool, bubble bool) (err error) {
if err = to.beginEdit(NodeRequest{New: new, Source: to, EditRoot: root}, bubble); err != nil {
return
}
defer func() {
if endErr := to.endEdit(NodeRequest{New: new, Source: to, EditRoot: root}, bubble); endErr != nil {
err = fmt.Errorf("error during endEdit: %v, previous error: %w", endErr, err)
}
}()
if meta.IsList(from.Meta()) && !from.InsideList {
if err := e.list(from, to, from.Meta().(*meta.List), new, strategy); err != nil {
return err
Expand All @@ -61,9 +66,6 @@ func (e editor) enter(from *Selection, to *Selection, new bool, strategy editStr
}
//fmt.Printf("Ended %s\n", meta.SchemaPath(from.Meta()))
}
if err := to.endEdit(NodeRequest{New: new, Source: to, EditRoot: root}, bubble); err != nil {
return err
}
return nil
}

Expand Down
41 changes: 41 additions & 0 deletions node/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,44 @@ func testDataRoot() map[string]interface{} {
},
}
}

func TestNodeEndEditOnFailure(t *testing.T) {
mstr := `module m { prefix ""; namespace ""; revision 0;
container c {
leaf x {
type string;
}
}
}`
json, _ := nodeutil.ReadJSON(`{"x":4}`)
m, err := parser.LoadModuleFromString(nil, mstr)
if err != nil {
t.Fatal(err)
}
n := &nodeutil.Basic{}
n.OnChild = func(r node.ChildRequest) (node.Node, error) {
return n, nil
}
var actual bytes.Buffer
n.OnBeginEdit = func(r node.NodeRequest) error {
fmt.Fprintf(&actual, "begin %s\n", r.Selection.Meta().Ident())
return nil
}
n.OnEndEdit = func(r node.NodeRequest) error {
fmt.Fprintf(&actual, "end %s\n", r.Selection.Meta().Ident())
return nil
}
root := node.NewBrowser(m, n).Root()
sel, err := root.Find("c")
fc.RequireEqual(t, nil, err)
err = sel.UpdateFrom(json)
if err == nil {
t.Error("Expected sel.UpdateFrom() to fail due to wrong argument")
}

fc.AssertEqual(t, `begin c
begin m
end c
end m
`, actual.String())
}
9 changes: 5 additions & 4 deletions node/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ func (sel *Selection) Delete() (err error) {
if err := sel.beginEdit(NodeRequest{Source: sel, Delete: true, EditRoot: true}, true); err != nil {
return err
}
defer func() {
if endErr := sel.endEdit(NodeRequest{Source: sel, Delete: true, EditRoot: true}, true); endErr != nil {
err = fmt.Errorf("error during endEdit: %v, previous error: %w", endErr, err)
}
}()

if sel.InsideList {
r := ListRequest{
Expand All @@ -404,10 +409,6 @@ func (sel *Selection) Delete() (err error) {
return err
}
}

if err := sel.endEdit(NodeRequest{Source: sel, Delete: true, EditRoot: true}, true); err != nil {
return err
}
return
}

Expand Down

0 comments on commit c334a23

Please sign in to comment.