diff options
author | Julien Dessaux | 2024-02-13 08:09:43 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-07 00:59:46 +0100 |
commit | e4eccafed79d739762ea791d97d5a2c9da191eb9 (patch) | |
tree | adba995f4695038253f1aafcbfae94b7119b1ec5 | |
parent | chore(templates): cleaned the templating api (diff) | |
download | gonf-e4eccafed79d739762ea791d97d5a2c9da191eb9.tar.gz gonf-e4eccafed79d739762ea791d97d5a2c9da191eb9.tar.bz2 gonf-e4eccafed79d739762ea791d97d5a2c9da191eb9.zip |
feat(variables): allow variable promises to be used directly where template values are
Diffstat (limited to '')
-rw-r--r-- | gonf/values.go | 17 | ||||
-rw-r--r-- | gonf/variables.go | 15 |
2 files changed, 26 insertions, 6 deletions
diff --git a/gonf/values.go b/gonf/values.go index 6e37c99..b77e703 100644 --- a/gonf/values.go +++ b/gonf/values.go @@ -1,6 +1,9 @@ package gonf -import "log/slog" +import ( + "fmt" + "log/slog" +) type Value interface { Bytes() []byte @@ -14,8 +17,11 @@ func interfaceToValue(v any) Value { if vv, ok := v.([]byte); ok { return &BytesValue{vv} } - slog.Error("interfaceToTemplateValue", "value", v, "error", "Not Implemented") - return nil + if vv, ok := v.(*VariablePromise); ok { + return vv + } + slog.Error("interfaceToValue", "value", v, "error", "Not Implemented") + panic(fmt.Sprintf("interfaceToValue cannot take type %T as argument. Value was %#v.", v, v)) } func interfaceToTemplateValue(v any) Value { @@ -25,8 +31,11 @@ func interfaceToTemplateValue(v any) Value { if vv, ok := v.([]byte); ok { return &TemplateValue{data: string(vv)} } + if vv, ok := v.(*VariablePromise); ok { + return vv + } slog.Error("interfaceToTemplateValue", "value", v, "error", "Not Implemented") - return nil + panic(fmt.Sprintf("interfaceToTemplateValue cannot take type %T as argument. Value was %#v.", v, v)) } // ----- BytesValue ----------------------------------------------------------------- diff --git a/gonf/variables.go b/gonf/variables.go index 2c1247c..17f8eb9 100644 --- a/gonf/variables.go +++ b/gonf/variables.go @@ -1,6 +1,9 @@ package gonf -import "log/slog" +import ( + "fmt" + "log/slog" +) // ----- Globals --------------------------------------------------------------- var variables map[string]*VariablePromise @@ -47,12 +50,20 @@ type VariablePromise struct { value Value } +// We want VariablePromise to satisfy the Value interface +func (s VariablePromise) Bytes() []byte { + return s.value.Bytes() +} +func (s VariablePromise) String() string { + return s.value.String() +} + // ----- Internal -------------------------------------------------------------- func getVariable(name string) string { if v, ok := variables[name]; ok { return v.value.String() } else { slog.Error("undefined variable or default", "name", name) - return "" + panic(fmt.Sprintf("undefined variable or default %s", name)) } } |