configcmd.OneOf for alternative fields; completion.Files for paths
UndocumentedKeys reported the unused alternatives of a struct like SecretRef (env/file/value) as undocumented; a struct implementing the marker configcmd.OneOf now counts as documented with one alternative, and upgrade offers none of the others to a file that chose one. completion.Tree gets the key completion.Files: that level completes file and directory paths besides command names (bash, zsh, fish, PowerShell), for programs started as "<program> [path]" like yamc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
83f4228e66
commit
131d82642c
6 files changed
+244
-13
No files matched your search
+36
-5
@@ -82,6 +82,9 @@ func docMapping(doc *yaml.Node) *yaml.Node {
|
||||
// of file).
|
||||
func compare(path []string, t reflect.Type, fmap, tmap *yaml.Node, childIndent, from, to, afterLine int, lines []string) []Addition {
|
||||
var out []Addition
|
||||
// A file that chose one alternative of a OneOf struct gets none of
|
||||
// the template's others offered.
|
||||
chosen := isOneOf(t) && len(fmap.Content) > 0
|
||||
for i := 0; i+1 < len(tmap.Content); i += 2 {
|
||||
tkey, tval := tmap.Content[i], tmap.Content[i+1]
|
||||
sub := append(append([]string(nil), path...), tkey.Value)
|
||||
@@ -105,7 +108,7 @@ func compare(path []string, t reflect.Type, fmap, tmap *yaml.Node, childIndent,
|
||||
}
|
||||
continue
|
||||
}
|
||||
if mentioned(lines, from, to, childIndent, tkey.Value) {
|
||||
if chosen || mentioned(lines, from, to, childIndent, tkey.Value) {
|
||||
continue
|
||||
}
|
||||
out = append(out, Addition{
|
||||
@@ -226,12 +229,36 @@ func Apply(raw []byte, adds []Addition) []byte {
|
||||
return []byte(out)
|
||||
}
|
||||
|
||||
// OneOf marks a configuration struct whose fields are alternatives, of
|
||||
// which a file sets exactly one -- a secret taken from env, file or
|
||||
// value, say. The method itself does nothing; implementing it is the
|
||||
// marker:
|
||||
//
|
||||
// func (SecretRef) ConfigOneOf() {}
|
||||
//
|
||||
// UndocumentedKeys then accepts a template that sets one alternative
|
||||
// (and explains the others in comments), and Missing offers none of the
|
||||
// others to a file that already chose one.
|
||||
type OneOf interface{ ConfigOneOf() }
|
||||
|
||||
var oneOfType = reflect.TypeOf((*OneOf)(nil)).Elem()
|
||||
|
||||
// isOneOf reports whether t (or a pointer to it) implements OneOf.
|
||||
func isOneOf(t reflect.Type) bool {
|
||||
if t == nil {
|
||||
return false
|
||||
}
|
||||
t = deref(t)
|
||||
return t.Implements(oneOfType) || reflect.PointerTo(t).Implements(oneOfType)
|
||||
}
|
||||
|
||||
// UndocumentedKeys returns the dotted YAML paths of schema (a struct
|
||||
// value, e.g. config.Config{}) that the template does not contain --
|
||||
// neither as a key nor, for a list of structs, in the list's first
|
||||
// item. A program's tests call it with its full template so that a new
|
||||
// option cannot be added without documenting it, which is what keeps
|
||||
// "config upgrade" useful.
|
||||
// item. A struct implementing OneOf counts as documented when the
|
||||
// template sets any one of its fields. A program's tests call it with
|
||||
// its full template so that a new option cannot be added without
|
||||
// documenting it, which is what keeps "config upgrade" useful.
|
||||
func UndocumentedKeys(template string, schema any) ([]string, error) {
|
||||
var doc yaml.Node
|
||||
if err := yaml.Unmarshal([]byte(template), &doc); err != nil {
|
||||
@@ -251,6 +278,8 @@ func walkSchema(t reflect.Type, m *yaml.Node, path []string, out *[]string) {
|
||||
if t.Kind() != reflect.Struct {
|
||||
return
|
||||
}
|
||||
// One alternative set: the others are not missing.
|
||||
chosen := isOneOf(t) && len(m.Content) > 0
|
||||
for i := range t.NumField() {
|
||||
f := t.Field(i)
|
||||
name, opts, _ := strings.Cut(f.Tag.Get("yaml"), ",")
|
||||
@@ -268,7 +297,9 @@ func walkSchema(t reflect.Type, m *yaml.Node, path []string, out *[]string) {
|
||||
sub := append(append([]string(nil), path...), name)
|
||||
_, val := lookup(m, name)
|
||||
if val == nil {
|
||||
*out = append(*out, strings.Join(sub, "."))
|
||||
if !chosen {
|
||||
*out = append(*out, strings.Join(sub, "."))
|
||||
}
|
||||
continue
|
||||
}
|
||||
ft := deref(f.Type)
|
||||
|
||||
Reference in new issue
Block a user