Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions interpreter/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (a *partActivation) AsPartialActivation() (PartialActivation, bool) {
return a, true
}

// AsPartialActivation walks the activation hierarchy and returns the first PartialActivation, if found.
func AsPartialActivation(vars Activation) (PartialActivation, bool) {
// Only internal activation instances may implement this interface
if pv, ok := vars.(partialActivationConverter); ok {
Expand Down
41 changes: 41 additions & 0 deletions interpreter/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ func TestActivation_ResolveLazy(t *testing.T) {
}
}

func TestActivation_ResolveLazyAny(t *testing.T) {
var v any
now := func() any {
if v == nil {
v = time.Now().Unix()
}
return v
}
a, _ := NewActivation(map[string]any{
"now": now,
})
first, _ := a.ResolveName("now")
second, _ := a.ResolveName("now")
if first != second {
t.Errorf("Got different second, "+
"expected same as first: 1:%v 2:%v", first, second)
}
}

func TestHierarchicalActivation(t *testing.T) {
// compose a parent with more properties than the child
parent, _ := NewActivation(map[string]any{
Expand All @@ -89,3 +108,25 @@ func TestHierarchicalActivation(t *testing.T) {
t.Error("Activation failed to resolve child value of 'c'")
}
}

func TestAsPartialActivation(t *testing.T) {
// compose a parent with more properties than the child
parent, _ := NewPartialActivation(map[string]any{
"a": types.String("world"),
"b": types.Int(-42),
}, NewAttributePattern("c"))
// compose the child such that it shadows the parent
child, _ := NewActivation(map[string]any{
"d": types.String("universe"),
})
combined := NewHierarchicalActivation(parent, child)

// Resolve the shadowed child value.
if part, found := AsPartialActivation(combined); found {
if part != parent {
t.Errorf("AsPartialActivation() got %v, wanted %v", part, parent)
}
} else {
t.Error("AsPartialActivation() failed, did not find parent partial activation")
}
}