From 5673af108f66d600f8af9b103370219e262eafd7 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 11 Jun 2024 15:39:51 -0400 Subject: [PATCH] hcl2template: split GetVarsByType in two functions GetVarsByType is a function that gets a list of Traversals from a hcl Block. This approach works when what we are visiting is indeed one, however when we can get an immediate list of Traversals, but want to filter them based on their roots, we have to reimplement parts of that function. Therefore, we split this function in two, GetVarsByType still keeps its current behaviour, but the filtering step is exposed as another function now: FilterTraversalsByType, so we can reuse it elsewhere. --- hcl2template/utils.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hcl2template/utils.go b/hcl2template/utils.go index ce9486ed6..7e4bd3d06 100644 --- a/hcl2template/utils.go +++ b/hcl2template/utils.go @@ -205,6 +205,13 @@ func GetVarsByType(block *hcl.Block, topLevelLabels ...string) []hcl.Traversal { } } + return FilterTraversalsByType(travs, topLevelLabels...) +} + +// FilterTraversalsByType lets the caller filter the traversals per top-level type. +// +// This can then be used to detect dependencies between block types. +func FilterTraversalsByType(travs []hcl.Traversal, topLevelLabels ...string) []hcl.Traversal { var rets []hcl.Traversal for _, t := range travs { varRootname := t.RootName()