From 784ba6bc86b70859af19ab03e0a145c4e40c85d9 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 17 Feb 2024 22:41:14 +0100 Subject: feat(variables): implemented a list of strings value type for template variables --- gonf/values.go | 18 ++++++++++++++++-- gonf/variables.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/gonf/values.go b/gonf/values.go index 56743e2..509b6bf 100644 --- a/gonf/values.go +++ b/gonf/values.go @@ -3,6 +3,7 @@ package gonf import ( "fmt" "log/slog" + "strings" ) type Value interface { @@ -71,6 +72,21 @@ func (i IntValue) String() string { return string(i.value) } +// ----- StringsListValue ------------------------------------------------------ +type StringsListValue struct { + value []string +} + +func (s *StringsListValue) Append(v ...string) { + s.value = append(s.value, v...) +} +func (s StringsListValue) Bytes() []byte { + return []byte(s.String()) +} +func (s StringsListValue) String() string { + return strings.Join(s.value, "\n") +} + // ----- StringValue ----------------------------------------------------------- type StringValue struct { value string @@ -83,8 +99,6 @@ func (s StringValue) String() string { return s.value } -// TODO lists - // TODO maps // TODO what else? diff --git a/gonf/variables.go b/gonf/variables.go index 17f8eb9..ed09d31 100644 --- a/gonf/variables.go +++ b/gonf/variables.go @@ -14,6 +14,22 @@ func init() { } // ----- Public ---------------------------------------------------------------- +func AppendVariable(name string, values ...string) *VariablePromise { + if v, ok := variables[name]; ok { + if l, ok := v.value.(*StringsListValue); ok { + l.Append(values...) + } + return v + } + v := &VariablePromise{ + isDefault: false, + name: name, + value: &StringsListValue{values}, + } + variables[name] = v + return v +} + func Default(name string, value string) *VariablePromise { if v, ok := variables[name]; ok { if !v.isDefault { -- cgit v1.2.3