Skip to content

Commit

Permalink
DOM: Expose copy of layers from overlay document
Browse files Browse the repository at this point in the history
There are use-cases to access them individually

Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed May 10, 2024
1 parent dee000d commit dbda14e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 5 additions & 3 deletions dom/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ func (m *overlayDocument) Search(fn SearchValueFunc) Coordinates {
return r
}

func (m *overlayDocument) Layers() []string {
c := make([]string, len(m.names))
copy(c, m.names)
func (m *overlayDocument) Layers() map[string]Container {
c := make(map[string]Container, len(m.names))
for n, v := range m.overlays {
c[n] = v.Clone().(Container)
}
return c
}

Expand Down
9 changes: 9 additions & 0 deletions dom/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,12 @@ func TestOverlaySearch(t *testing.T) {
res = d.Search(SearchEqual(2))
assert.Equal(t, 0, len(res))
}

func TestOverlayLayers(t *testing.T) {
d := NewOverlayDocument()
d.Put("layer1", "root.next.next2", LeafNode(1))
d.Put("layer2", "root.other", LeafNode(5))
m := d.Layers()
assert.Equal(t, 2, len(m))
assert.Equal(t, 5, m["layer2"].Children()["root"].(Container).Children()["other"].(Leaf).Value())
}
5 changes: 3 additions & 2 deletions dom/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ type OverlayDocument interface {
Put(overlay, path string, value Node)
// Merged returns read-only, merged view of all overlays
Merged(option ...MergeOption) Container
// Layers return copy of list with all layer names
Layers() []string
// Layers returns a copy of mapping between layer name and its associated Container.
// Containers are cloned using Node.Clone()
Layers() map[string]Container
}

0 comments on commit dbda14e

Please sign in to comment.