From f291994cfff140c4ff638f04e8390ce5037842be Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Wed, 19 May 2021 14:50:02 -0700 Subject: [PATCH] dagger/compiler: added value.HasAttr Signed-off-by: Sam Alba --- dagger/compiler/value.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/dagger/compiler/value.go b/dagger/compiler/value.go index 9b6e0ee5..89a590bc 100644 --- a/dagger/compiler/value.go +++ b/dagger/compiler/value.go @@ -229,3 +229,32 @@ func (v *Value) IsEmptyStruct() bool { func (v *Value) Cue() cue.Value { return v.val } + +// Returns true if value has a dagger attribute (eg. artifact, secret, input) +func (v *Value) HasAttr(filter ...string) bool { + attrs := v.val.Attributes(cue.FieldAttr) + + for _, attr := range attrs { + name := attr.Name() + // match `@dagger(...)` + if name == "dagger" { + // did not provide filter, match any @dagger attr + if len(filter) == 0 { + return true + } + + // loop over args (CSV content in attribute) + for i := 0; i < attr.NumArgs(); i++ { + key, _ := attr.Arg(i) + // one or several values where provided, filter + for _, val := range filter { + if key == val { + return true + } + } + } + } + } + + return false +}