feat(variables): implemented a list of strings value type for template variables

This commit is contained in:
Julien Dessaux 2024-02-17 22:41:14 +01:00
parent db5cba791e
commit 784ba6bc86
Signed by: adyxax
GPG key ID: F92E51B86E07177E
2 changed files with 32 additions and 2 deletions

View file

@ -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?

View file

@ -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 {