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
|
status Status
|
||||||
}
|
}
|
||||||
|
|
||||||
func File(filename Value, contents Value) *FilePromise {
|
func File(filename any, contents string) *FilePromise {
|
||||||
return &FilePromise{
|
return &FilePromise{
|
||||||
chain: nil,
|
chain: nil,
|
||||||
contents: contents,
|
contents: interfaceToValue(contents),
|
||||||
err: nil,
|
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,
|
status: PROMISED,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,27 +19,26 @@ func init() {
|
||||||
// ----- Public ----------------------------------------------------------------
|
// ----- Public ----------------------------------------------------------------
|
||||||
type TemplateValue struct {
|
type TemplateValue struct {
|
||||||
contents []byte
|
contents []byte
|
||||||
name string
|
data string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TemplateValue) Bytes() []byte {
|
func (t *TemplateValue) Bytes() []byte {
|
||||||
var buff bytes.Buffer
|
if t.contents == nil {
|
||||||
if err := templates.ExecuteTemplate(&buff, t.name, nil /* no data needed */); err != nil {
|
tpl := templates.New("")
|
||||||
slog.Error("template", "step", "ExecuteTemplate", "name", t.name, "error", err)
|
if _, err := tpl.Parse(t.data); err != nil {
|
||||||
|
slog.Error("template", "step", "Parse", "data", t.data, "error", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return buff.Bytes()
|
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 t.contents
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TemplateValue) String() string {
|
func (t TemplateValue) String() string {
|
||||||
return string(t.Bytes()[:])
|
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
|
package gonf
|
||||||
|
|
||||||
|
import "log/slog"
|
||||||
|
|
||||||
type Value interface {
|
type Value interface {
|
||||||
Bytes() []byte
|
Bytes() []byte
|
||||||
String() string
|
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 -----------------------------------------------------------------
|
// ----- BytesValue -----------------------------------------------------------------
|
||||||
type BytesValue struct {
|
type BytesValue struct {
|
||||||
value []byte
|
value []byte
|
||||||
|
@ -18,10 +42,6 @@ func (b BytesValue) String() string {
|
||||||
return string(b.value[:])
|
return string(b.value[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func Bytes(value []byte) *BytesValue {
|
|
||||||
return &BytesValue{value}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----- StringValue ----------------------------------------------------------------
|
// ----- StringValue ----------------------------------------------------------------
|
||||||
type StringValue struct {
|
type StringValue struct {
|
||||||
value string
|
value string
|
||||||
|
@ -34,10 +54,6 @@ func (s StringValue) String() string {
|
||||||
return s.value
|
return s.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func String(value string) *StringValue {
|
|
||||||
return &StringValue{value}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO lists
|
// TODO lists
|
||||||
|
|
||||||
// TODO maps
|
// TODO maps
|
||||||
|
|
|
@ -11,7 +11,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----- Public ----------------------------------------------------------------
|
// ----- Public ----------------------------------------------------------------
|
||||||
func Default(name string, value Value) *VariablePromise {
|
func Default(name string, value string) *VariablePromise {
|
||||||
if v, ok := variables[name]; ok {
|
if v, ok := variables[name]; ok {
|
||||||
if !v.isDefault {
|
if !v.isDefault {
|
||||||
slog.Debug("default would overwrite a variable, ignoring", "name", name, "old_value", v.value, "new_value", value)
|
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{
|
v := &VariablePromise{
|
||||||
isDefault: true,
|
isDefault: true,
|
||||||
name: name,
|
name: name,
|
||||||
value: value,
|
value: interfaceToTemplateValue(value),
|
||||||
}
|
}
|
||||||
variables[name] = v
|
variables[name] = v
|
||||||
return 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 {
|
if v, ok := variables[name]; ok && !v.isDefault {
|
||||||
slog.Error("variable is being overwritten", "name", name, "old_value", v, "new_value", value)
|
slog.Error("variable is being overwritten", "name", name, "old_value", v, "new_value", value)
|
||||||
}
|
}
|
||||||
v := &VariablePromise{
|
v := &VariablePromise{
|
||||||
isDefault: false,
|
isDefault: false,
|
||||||
name: name,
|
name: name,
|
||||||
value: value,
|
value: interfaceToTemplateValue(value),
|
||||||
}
|
}
|
||||||
variables[name] = v
|
variables[name] = v
|
||||||
return v
|
return v
|
||||||
|
|
Loading…
Add table
Reference in a new issue