Replace Dynamic with template.FuncMap

Must better and more resilient. Also allows the creation of template
domain languages by individual Cmd authors.

First `indent` builtin created.
pull/85/head v0.5.0
rwxrob 2 years ago
parent 966dc7ac29
commit 87300a5828
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -371,6 +371,13 @@ want the specific reasons.
When and if dependencies on them become an issue it can be addressed
then.
* **Not exporting Dynamic FuncMap builtins**
Since those builtins will land in `mark` subpackage eventually, don't
want to build any dependencies on them now that will break. The
builtins themselves can obviously be used immediately and has a much
smaller chance of changing in the future.
## Style Guidelines
* Everything through `go fmt` or equiv, no exceptions

@ -8,7 +8,7 @@ require (
github.com/rwxrob/scan v0.6.1
github.com/rwxrob/structs v0.5.1
github.com/rwxrob/term v0.2.6
github.com/rwxrob/to v0.4.5
github.com/rwxrob/to v0.4.6
)
require (

@ -10,6 +10,8 @@ github.com/rwxrob/term v0.2.6 h1:C8BqqHaEh8MGYp1cVrPRlDAYPEK3HfvhGjf7l5AvnV8=
github.com/rwxrob/term v0.2.6/go.mod h1:II0qQ7aHUdPniZCAPWOdYwugcZqdmRmEWIJQN7Z8NA0=
github.com/rwxrob/to v0.4.5 h1:ILFn40Zd8Tbz0+vmMcA9WyBkAZzeIltr39STLeoxP/o=
github.com/rwxrob/to v0.4.5/go.mod h1:lojk6scni4ZRYjnKJO/f2DVRTW0BB6l9LZQ/NvZZt4Y=
github.com/rwxrob/to v0.4.6 h1:VXYmc2wI3aGq3HzecgMWYLiFZRVy1PlZIVOGRt0S8V0=
github.com/rwxrob/to v0.4.6/go.mod h1:lojk6scni4ZRYjnKJO/f2DVRTW0BB6l9LZQ/NvZZt4Y=
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 h1:iU7T1X1J6yxDr0rda54sWGkHgOp5XJrqm79gcNlC2VM=
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=

@ -18,6 +18,7 @@ import (
"github.com/rwxrob/fn/maps"
"github.com/rwxrob/fn/redu"
"github.com/rwxrob/structs/qstack"
"github.com/rwxrob/to"
)
type Cmd struct {
@ -47,7 +48,8 @@ type Cmd struct {
MaxParm int `json:"-"` // maximum number of params required
ReqConf bool `json:"-"` // requires Z.Conf be assigned
ReqCache bool `json:"-"` // requires Z.Cache be assigned
Dynamic DynMap `json:"-"` // dynamic attributes
Dynamic template.FuncMap `json:"-"` // dynamic attributes
_aliases map[string]*Cmd // see cacheAliases called from Run->Seek->Resolve
_sections map[string]string // see cacheSections called from Run
@ -61,26 +63,6 @@ type Section struct {
Body string
}
// DynMap is a key value map of Dyn functions.
type DynMap map[string]DynFunc
// DynFunc is a dynamic attribute that evaluated and returned at runtime.
// Access them from Cmd.Dyn[<name>]. They are slower to access so don't
// abuse them.
type DynFunc func(x *Cmd) string
// Dyn returns the output of the DynFunc value for the give key from the
// Cmd.Dynamic DynMap if not nil. Returns empty string otherwise.
func (x *Cmd) Dyn(key string) string {
if x.Dynamic == nil {
return ""
}
if v, has := x.Dynamic[key]; has {
return v(x)
}
return ""
}
func (s Section) GetTitle() string { return s.Title }
func (s Section) GetBody() string { return s.Body }
@ -472,7 +454,8 @@ func (x *Cmd) Q(q string) string {
// as the data object source for the template. It is called by the Get*
// family of field accessors but can be called directly as well.
func (x *Cmd) Fill(tmpl string) string {
t, err := template.New("t").Parse(tmpl)
funcs := to.MergedMaps(markFuncMap, x.Dynamic)
t, err := template.New("t").Funcs(funcs).Parse(tmpl)
if err != nil {
log.Println(err)
}

@ -0,0 +1,19 @@
package Z
import (
"text/template"
"github.com/rwxrob/to"
)
// This file contains the BonzaiMark builtins that Cmd authors can use
// in their Description and other places where templated BonzaiMark is
// allowed.
var markFuncMap = template.FuncMap{
"indent": indent,
}
func indent(n int, in string) string {
return to.Indented(in, IndentBy+n)
}
Loading…
Cancel
Save