From 4e8223ef3496f390dd84a2a47fc8846afd6f7c76 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 8 Feb 2017 17:14:40 +0100 Subject: [PATCH] feat context: Add support for resource set collections A resource set collection is a resource set with an addition 'include' array configured. It is a short-hand for importing multiple resource sets from the same folder and for excluding/including them as a group. See https://github.com/tazjin/kontemplate/issues/9 for more information. Closes #9 --- context/context.go | 24 ++++++++++++++++++++++++ templater/templater.go | 10 +++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/context/context.go b/context/context.go index 140f11fce..94838f668 100644 --- a/context/context.go +++ b/context/context.go @@ -14,6 +14,10 @@ import ( type ResourceSet struct { Name string `json:"name"` Values map[string]interface{} `json:"values"` + + // Fields for resource set collections + Include []ResourceSet `json:"include"` + Parent *string } type Context struct { @@ -63,3 +67,23 @@ func LoadContextFromFile(filename string) (*Context, error) { return &c, nil } + +// Flattens resource set collections, i.e. resource sets that themselves have an additional 'include' field set. +// Those will be regarded as a short-hand for including multiple resource sets from a subfolder. +// See https://github.com/tazjin/kontemplate/issues/9 for more information. +func flattenResourceSetCollections(rs *[]ResourceSet) *[]ResourceSet { + flattened := make([]ResourceSet, 0) + + for _, r := range *rs { + if len(r.Include) == 0 { + flattened = append(flattened, r) + } else { + for _, subResourceSet := range r.Include { + subResourceSet.Parent = &r.Name + flattened = append(flattened, subResourceSet) + } + } + } + + return &flattened +} diff --git a/templater/templater.go b/templater/templater.go index bb65cd1df..29079eb94 100644 --- a/templater/templater.go +++ b/templater/templater.go @@ -121,7 +121,7 @@ func applyLimits(rs *[]context.ResourceSet, include *[]string, exclude *[]string // Exclude excluded resource sets excluded := make([]context.ResourceSet, 0) for _, r := range *rs { - if !contains(exclude, &r.Name) { + if !matchesResourceSet(exclude, &r) { excluded = append(excluded, r) } } @@ -132,7 +132,7 @@ func applyLimits(rs *[]context.ResourceSet, include *[]string, exclude *[]string } included := make([]context.ResourceSet, 0) for _, r := range excluded { - if contains(include, &r.Name) { + if matchesResourceSet(include, &r) { included = append(included, r) } } @@ -140,10 +140,10 @@ func applyLimits(rs *[]context.ResourceSet, include *[]string, exclude *[]string return &included } -// Check whether a certain string is contained in a string slice -func contains(s *[]string, v *string) bool { +// Check whether an include/exclude string slice matches a resource set +func matchesResourceSet(s *[]string, rs *context.ResourceSet) bool { for _, r := range *s { - if r == *v { + if r == rs.Name || r == *rs.Parent { return true } }