shell completion --format: use anonymous struct field once

We should not include the anonymous twice in the suggestions.

one example is `podman network ls --format {{.` it will also show
`{{.Network` but since Network is the actual struct all fields are
already shown so there is no need for it to be suggested.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2022-04-28 15:06:00 +02:00
parent 8be9781586
commit cb9a45630f
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
2 changed files with 5 additions and 6 deletions

View File

@ -1056,13 +1056,12 @@ func getStructFields(f reflect.Value, prefix string) []string {
if kind == reflect.Struct {
suffix = "."
}
if strings.HasPrefix(fname, prefix) {
// add field name with suffix
suggestions = append(suggestions, fname+suffix)
}
// if field is anonymous add the child fields as well
if field.Anonymous {
suggestions = append(suggestions, getStructFields(f.Field(j), prefix)...)
} else if strings.HasPrefix(fname, prefix) {
// add field name with suffix
suggestions = append(suggestions, fname+suffix)
}
}

View File

@ -79,12 +79,12 @@ func TestAutocompleteFormat(t *testing.T) {
{
"fist level struct field name",
"{{.",
[]string{"{{.Name}}", "{{.Age}}", "{{.Car.", "{{.Car2.", "{{.Anonymous.", "{{.Hello}}"},
[]string{"{{.Name}}", "{{.Age}}", "{{.Car.", "{{.Car2.", "{{.Hello}}"},
},
{
"fist level struct field name",
"{{ .",
[]string{"{{ .Name}}", "{{ .Age}}", "{{ .Car.", "{{ .Car2.", "{{ .Anonymous.", "{{ .Hello}}"},
[]string{"{{ .Name}}", "{{ .Age}}", "{{ .Car.", "{{ .Car2.", "{{ .Hello}}"},
},
{
"fist level struct field name",