Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOM: Re-introduce way to initialise Container from properties #30

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions analytics/placeholder_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func TestIsPossiblePlaceholder(t *testing.T) {

func TestResolvePlaceholders(t *testing.T) {
ds := NewDocumentSet()
ds.AddUnnamedDocument(b.FromMap(map[string]interface{}{
assert.NoError(t, ds.AddUnnamedDocument(b.FromProperties(map[string]interface{}{
"key1.key2.key31": "${key1.key2.key32}",
"key1.key2.key32": 3,
"key1.key2.key33": "${key1.key2.key34}",
"key1.key2.key40": "${key1.key2.key41}",
"key1.key2.key41": "${key1.key2.key42}",
"key1.key2.key42": "${unresolved}",
"key1.key2.key43": "${unresolved}",
}))
})))
res := NewPlaceholderResolverBuilder().
OnResolutionFailure(func(key, value string, coordinates dom.Coordinates) {
t.Logf("resolution failed,key=%s, value=%s, coordinates=%s", key, value, coordinates.String())
Expand All @@ -55,9 +55,9 @@ func TestResolvePlaceholders(t *testing.T) {
Build()
rpt := res.Resolve(ds.AsOne())

assert.Equal(t, 6, len(rpt.FailedKeys))
assert.Equal(t, 3, len(rpt.FailedKeys))
res = NewPlaceholderResolverBuilder().
Build()
rpt = res.Resolve(ds.AsOne())
assert.Equal(t, 6, len(rpt.FailedKeys))
assert.Equal(t, 3, len(rpt.FailedKeys))
}
8 changes: 8 additions & 0 deletions dom/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ func (f *containerFactory) FromMap(in map[string]interface{}) ContainerBuilder {
return &doc
}

func (f *containerFactory) FromProperties(in map[string]interface{}) ContainerBuilder {
b := f.Container()
for k, v := range in {
b.AddValueAt(k, LeafNode(v))
}
return b
}

func (f *containerFactory) Container() ContainerBuilder {
return &containerBuilderImpl{}
}
Expand Down
10 changes: 10 additions & 0 deletions dom/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ func TestFromMap(t *testing.T) {
assert.Equal(t, "abc", c.Child("test1.test2").(Leaf).Value())
}

func TestFromProperties(t *testing.T) {
c := b.FromProperties(map[string]interface{}{
"test1.test2": "abc",
"test1.test22": 123,
})
assert.Equal(t, 1, len(c.Children()))
assert.Equal(t, 2, len(c.Children()["test1"].(Container).Children()))
assert.Equal(t, "abc", c.Lookup("test1.test2").(Leaf).Value())
}

func TestRemoveAt(t *testing.T) {
c := b.FromMap(map[string]interface{}{
"test2": "abc",
Expand Down
2 changes: 2 additions & 0 deletions dom/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ type ContainerFactory interface {
FromReader(r io.Reader, fn DecoderFunc) (ContainerBuilder, error)
// FromMap creates ContainerBuilder pre-populated with data from provided map
FromMap(in map[string]interface{}) ContainerBuilder
// FromProperties is similar to FromMap except that keys are parsed into path before inserting into ContainerBuilder
FromProperties(in map[string]interface{}) ContainerBuilder
// FromAny creates ContainerBuilder from any object. Any error encountered in process will result in panic.
// This method uses YAML codec internally to perform translation between raw interface and map
FromAny(v interface{}) ContainerBuilder
Expand Down
Loading