feat(variables): allow variable promises to be used directly where template values are
This commit is contained in:
parent
0726c6811b
commit
e4eccafed7
2 changed files with 26 additions and 6 deletions
|
@ -1,6 +1,9 @@
|
||||||
package gonf
|
package gonf
|
||||||
|
|
||||||
import "log/slog"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
)
|
||||||
|
|
||||||
type Value interface {
|
type Value interface {
|
||||||
Bytes() []byte
|
Bytes() []byte
|
||||||
|
@ -14,8 +17,11 @@ func interfaceToValue(v any) Value {
|
||||||
if vv, ok := v.([]byte); ok {
|
if vv, ok := v.([]byte); ok {
|
||||||
return &BytesValue{vv}
|
return &BytesValue{vv}
|
||||||
}
|
}
|
||||||
slog.Error("interfaceToTemplateValue", "value", v, "error", "Not Implemented")
|
if vv, ok := v.(*VariablePromise); ok {
|
||||||
return nil
|
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 {
|
func interfaceToTemplateValue(v any) Value {
|
||||||
|
@ -25,8 +31,11 @@ func interfaceToTemplateValue(v any) Value {
|
||||||
if vv, ok := v.([]byte); ok {
|
if vv, ok := v.([]byte); ok {
|
||||||
return &TemplateValue{data: string(vv)}
|
return &TemplateValue{data: string(vv)}
|
||||||
}
|
}
|
||||||
|
if vv, ok := v.(*VariablePromise); ok {
|
||||||
|
return vv
|
||||||
|
}
|
||||||
slog.Error("interfaceToTemplateValue", "value", v, "error", "Not Implemented")
|
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 -----------------------------------------------------------------
|
// ----- BytesValue -----------------------------------------------------------------
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package gonf
|
package gonf
|
||||||
|
|
||||||
import "log/slog"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
)
|
||||||
|
|
||||||
// ----- Globals ---------------------------------------------------------------
|
// ----- Globals ---------------------------------------------------------------
|
||||||
var variables map[string]*VariablePromise
|
var variables map[string]*VariablePromise
|
||||||
|
@ -47,12 +50,20 @@ type VariablePromise struct {
|
||||||
value Value
|
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 --------------------------------------------------------------
|
// ----- Internal --------------------------------------------------------------
|
||||||
func getVariable(name string) string {
|
func getVariable(name string) string {
|
||||||
if v, ok := variables[name]; ok {
|
if v, ok := variables[name]; ok {
|
||||||
return v.value.String()
|
return v.value.String()
|
||||||
} else {
|
} else {
|
||||||
slog.Error("undefined variable or default", "name", name)
|
slog.Error("undefined variable or default", "name", name)
|
||||||
return ""
|
panic(fmt.Sprintf("undefined variable or default %s", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue