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:
terminarandClaude Opus 5.5 committed 2026-10-31 23:40:45 +01:00
1 parent 83f4228e66
commit 131d82642c
6 files changed
+244 -13

No files matched your search

+71
View File
@@ -427,3 +427,74 @@ func TestFilenameAndFind(t *testing.T) {
t.Fatalf("Find: %v, %v", l.Path, err)
}
}
type secretRef struct {
Env string `yaml:"env,omitempty"`
File string `yaml:"file,omitempty"`
Value string `yaml:"value,omitempty"`
}
func (secretRef) ConfigOneOf() {}
type oneOfSchema struct {
Auth struct {
Type string `yaml:"type"`
Token secretRef `yaml:"token"`
Email *secretRef `yaml:"email"`
} `yaml:"auth"`
}
const oneOfTemplate = `auth:
type: bearer
# exactly one of env, file or value
token:
env: TOKEN
email:
file: /run/secrets/email
`
func TestUndocumentedKeysOneOf(t *testing.T) {
got, err := UndocumentedKeys(oneOfTemplate, oneOfSchema{})
if err != nil {
t.Fatal(err)
}
if len(got) != 0 {
t.Errorf("UndocumentedKeys(one alternative set) = %v, want none", got)
}
// No alternative in the template: every one is undocumented.
got, _ = UndocumentedKeys("auth:\n type: bearer\n token: {}\n email:\n env: E\n", oneOfSchema{})
if want := "auth.token.env auth.token.file auth.token.value"; strings.Join(got, " ") != want {
t.Errorf("UndocumentedKeys(empty token) = %v, want %s", got, want)
}
// A struct without the marker still needs every field.
type plain struct {
Token struct {
Env string `yaml:"env"`
File string `yaml:"file"`
} `yaml:"token"`
}
got, _ = UndocumentedKeys("token:\n env: X\n", plain{})
if want := "token.file"; strings.Join(got, " ") != want {
t.Errorf("UndocumentedKeys(plain) = %v, want %s", got, want)
}
}
func TestMissingOneOfOffersNoOtherAlternative(t *testing.T) {
file := "auth:\n type: bearer\n token:\n file: /run/secrets/token\n email:\n value: me@example.org\n"
got, err := Missing([]byte(file), []byte(oneOfTemplate), oneOfSchema{})
if err != nil {
t.Fatal(err)
}
if len(got) != 0 {
t.Errorf("Missing() = %v, want nothing (each secret has its alternative)", got)
}
// Without the schema the template's alternative is offered as before.
got, _ = Missing([]byte(file), []byte(oneOfTemplate), nil)
var keys []string
for _, a := range got {
keys = append(keys, a.Key)
}
if want := "auth.token.env auth.email.file"; strings.Join(keys, " ") != want {
t.Errorf("Missing(no schema) = %v, want %s", keys, want)
}
}
+36 -5
View File
@@ -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)