Fix Cmd.Q, tighten Query interface reqs

This commit is contained in:
rwxrob 2022-04-09 04:46:39 -04:00
parent fa90cf0850
commit a3e22c48f8
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77
3 changed files with 11 additions and 5 deletions

View File

@ -24,6 +24,11 @@ package bonzai
// to OverWrite will be marshalled to YAML and completely replace the
// existing configuration in a way that must guarantee atomic,
// system-wide write safety. (Locking a file is insufficient alone.)
//
// Query implementations must trim any initial or trailing white space
// (usually just a single line return from yq, for example) in order to
// ensure that the resulting values for edge matches can be used without
// needing a trim.
type Configurer interface {
Init() error // must initialize a new configuration
Data() string // must return full YAML

View File

@ -408,11 +408,12 @@ func (x *Cmd) Path() []string {
return path.Items()
}
// PathString returns a dotted notation of the Path. This is useful for
// associating configuration and other data specifically with this
// PathString returns a dotted notation of the Path including an initial
// dot (for root). This is compatible yq query expressions and useful
// for associating configuration and other data specifically with this
// command.
func (x *Cmd) PathString() string {
return strings.Join(x.Path(), ".")
return "." + strings.Join(x.Path(), ".")
}
// Log is currently short for log.Printf() but may be supplemented in
@ -421,7 +422,7 @@ func (x *Cmd) Log(format string, a ...any) {
log.Printf(format, a...)
}
// Q is a shorter version of Z.Conf.Query(x.Path()+"."+q) for
// Q is a shorter version of Z.Conf.Query(x.PathString()+"."+q) for
// convenience. Logs the error and returns a blank string if Z.Conf is
// not defined (see ReqConf).
func (x *Cmd) Q(q string) string {

View File

@ -135,7 +135,7 @@ func ExampleCmd_PathString() {
z.Run()
// Output:
// some.thing.deep
// .some.thing.deep
}
func ExampleCmd_Names() {