Skip to content

Commit

Permalink
DOM: Implement Overlay.Add() function
Browse files Browse the repository at this point in the history
Use this function do populate analytics.DocumentSet instead of Put

Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed May 10, 2024
1 parent dbda14e commit be5a77e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion analytics/document_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (ds *documentSet) filtered(filterFn func(name string, ctx *docContext) bool
o := dom.NewOverlayDocument()
for n, c := range ds.ctxMap {
if filterFn(n, c) {
o.Put(n, "", c.doc)
o.Add(n, c.doc)
}
}
return o
Expand Down
4 changes: 2 additions & 2 deletions analytics/placeholder_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func TestResolvePlaceholders(t *testing.T) {
Build()
rpt := res.Resolve(ds.AsOne())

assert.Equal(t, 3, len(rpt.FailedKeys))
assert.Equal(t, 6, len(rpt.FailedKeys))
res = NewPlaceholderResolverBuilder().
Build()
rpt = res.Resolve(ds.AsOne())
assert.Equal(t, 3, len(rpt.FailedKeys))
assert.Equal(t, 6, len(rpt.FailedKeys))
}
7 changes: 7 additions & 0 deletions dom/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func (m *overlayDocument) Layers() map[string]Container {
return c
}

func (m *overlayDocument) Add(overlay string, value Container) {
cb := m.ensureOverlay(overlay)
for k, v := range value.Children() {
cb.AddValue(k, v)
}
}

func (m *overlayDocument) Put(overlay, path string, value Node) {
if value.IsContainer() {
for k, v := range value.(Container).Flatten() {
Expand Down
9 changes: 9 additions & 0 deletions dom/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ func TestOverlayLayers(t *testing.T) {
assert.Equal(t, 2, len(m))
assert.Equal(t, 5, m["layer2"].Children()["root"].(Container).Children()["other"].(Leaf).Value())
}

func TestOverlayAdd(t *testing.T) {
d := NewOverlayDocument()
d.Add("layerX", b.Container().AddContainer("sub"))
c := b.Container()
c.AddValue("sub1.sub2", LeafNode(123))
d.Add("layer1", c)
assert.Equal(t, 123, d.Layers()["layer1"].Children()["sub1.sub2"].(Leaf).Value())
}
2 changes: 2 additions & 0 deletions dom/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ type OverlayDocument interface {
Search(fn SearchValueFunc) Coordinates
// Populate puts dictionary into overlay at given path
Populate(overlay, path string, data *map[string]interface{})
// Add adds elements from given Container into root of given layer
Add(overlay string, value Container)
// Put puts Node value into overlay at given path
Put(overlay, path string, value Node)
// Merged returns read-only, merged view of all overlays
Expand Down

0 comments on commit be5a77e

Please sign in to comment.