summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--gonf/files.go16
-rw-r--r--gonf/templates.go29
-rw-r--r--gonf/values.go32
-rw-r--r--gonf/variables.go9
4 files changed, 56 insertions, 30 deletions
diff --git a/gonf/files.go b/gonf/files.go
index ce6f6c8..197f352 100644
--- a/gonf/files.go
+++ b/gonf/files.go
@@ -25,12 +25,22 @@ type FilePromise struct {
status Status
}
-func File(filename Value, contents Value) *FilePromise {
+func File(filename any, contents string) *FilePromise {
return &FilePromise{
chain: nil,
- contents: contents,
+ contents: interfaceToValue(contents),
err: nil,
- filename: filename,
+ filename: interfaceToTemplateValue(filename),
+ status: PROMISED,
+ }
+}
+
+func Template(filename any, contents any) *FilePromise {
+ return &FilePromise{
+ chain: nil,
+ contents: interfaceToTemplateValue(contents),
+ err: nil,
+ filename: interfaceToTemplateValue(filename),
status: PROMISED,
}
}
diff --git a/gonf/templates.go b/gonf/templates.go
index 4d49c71..0b7e11b 100644
--- a/gonf/templates.go
+++ b/gonf/templates.go
@@ -19,27 +19,26 @@ func init() {
// ----- Public ----------------------------------------------------------------
type TemplateValue struct {
contents []byte
- name string
+ data string
}
func (t *TemplateValue) Bytes() []byte {
- var buff bytes.Buffer
- if err := templates.ExecuteTemplate(&buff, t.name, nil /* no data needed */); err != nil {
- slog.Error("template", "step", "ExecuteTemplate", "name", t.name, "error", err)
- return nil
+ if t.contents == nil {
+ tpl := templates.New("")
+ if _, err := tpl.Parse(t.data); err != nil {
+ slog.Error("template", "step", "Parse", "data", t.data, "error", err)
+ return nil
+ }
+ var buff bytes.Buffer
+ if err := tpl.Execute(&buff, nil /* no data needed */); err != nil {
+ slog.Error("template", "step", "Execute", "data", t.data, "error", err)
+ return nil
+ }
+ t.contents = buff.Bytes()
}
- return buff.Bytes()
+ return t.contents
}
func (t TemplateValue) String() string {
return string(t.Bytes()[:])
}
-
-func Template(name string, contents []byte) *TemplateValue {
- tpl := templates.New(name)
- if _, err := tpl.Parse(string(contents)); err != nil {
- slog.Error("template", "step", "Parse", "name", name, "error", err)
- return nil
- }
- return &TemplateValue{contents, name}
-}
diff --git a/gonf/values.go b/gonf/values.go
index 69e4132..6e37c99 100644
--- a/gonf/values.go
+++ b/gonf/values.go
@@ -1,10 +1,34 @@
package gonf
+import "log/slog"
+
type Value interface {
Bytes() []byte
String() string
}
+func interfaceToValue(v any) Value {
+ if vv, ok := v.(string); ok {
+ return &StringValue{vv}
+ }
+ if vv, ok := v.([]byte); ok {
+ return &BytesValue{vv}
+ }
+ slog.Error("interfaceToTemplateValue", "value", v, "error", "Not Implemented")
+ return nil
+}
+
+func interfaceToTemplateValue(v any) Value {
+ if vv, ok := v.(string); ok {
+ return &TemplateValue{data: vv}
+ }
+ if vv, ok := v.([]byte); ok {
+ return &TemplateValue{data: string(vv)}
+ }
+ slog.Error("interfaceToTemplateValue", "value", v, "error", "Not Implemented")
+ return nil
+}
+
// ----- BytesValue -----------------------------------------------------------------
type BytesValue struct {
value []byte
@@ -18,10 +42,6 @@ func (b BytesValue) String() string {
return string(b.value[:])
}
-func Bytes(value []byte) *BytesValue {
- return &BytesValue{value}
-}
-
// ----- StringValue ----------------------------------------------------------------
type StringValue struct {
value string
@@ -34,10 +54,6 @@ func (s StringValue) String() string {
return s.value
}
-func String(value string) *StringValue {
- return &StringValue{value}
-}
-
// TODO lists
// TODO maps
diff --git a/gonf/variables.go b/gonf/variables.go
index e6c845d..2c1247c 100644
--- a/gonf/variables.go
+++ b/gonf/variables.go
@@ -11,7 +11,7 @@ func init() {
}
// ----- Public ----------------------------------------------------------------
-func Default(name string, value Value) *VariablePromise {
+func Default(name string, value string) *VariablePromise {
if v, ok := variables[name]; ok {
if !v.isDefault {
slog.Debug("default would overwrite a variable, ignoring", "name", name, "old_value", v.value, "new_value", value)
@@ -22,19 +22,20 @@ func Default(name string, value Value) *VariablePromise {
v := &VariablePromise{
isDefault: true,
name: name,
- value: value,
+ value: interfaceToTemplateValue(value),
}
variables[name] = v
return v
}
-func Variable(name string, value Value) *VariablePromise {
+
+func Variable(name string, value string) *VariablePromise {
if v, ok := variables[name]; ok && !v.isDefault {
slog.Error("variable is being overwritten", "name", name, "old_value", v, "new_value", value)
}
v := &VariablePromise{
isDefault: false,
name: name,
- value: value,
+ value: interfaceToTemplateValue(value),
}
variables[name] = v
return v