chore(templates): cleaned the templating api
This commit is contained in:
parent
c1d2b8912d
commit
0726c6811b
4 changed files with 56 additions and 30 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue