shell completion --format: only show exported fields

go templates only support exported fields, so the completion logic must
filter the private fields out.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2022-05-16 14:30:45 +02:00
parent 7093885df7
commit eeeb88a667
2 changed files with 12 additions and 0 deletions

View File

@ -1054,6 +1054,11 @@ func getStructFields(f reflect.Value, prefix string) []string {
// loop over all field names // loop over all field names
for j := 0; j < f.NumField(); j++ { for j := 0; j < f.NumField(); j++ {
field := f.Type().Field(j) field := f.Type().Field(j)
// check if struct field is not exported, templates only use exported fields
// PkgPath is always empty for exported fields
if field.PkgPath != "" {
continue
}
fname := field.Name fname := field.Name
suffix := "}}" suffix := "}}"
kind := field.Type.Kind() kind := field.Type.Kind()

View File

@ -31,6 +31,12 @@ func (c *Car) Color() string {
return "" return ""
} }
// This is for reflect testing required.
// nolint:unused
func (c Car) internal() int {
return 0
}
func TestAutocompleteFormat(t *testing.T) { func TestAutocompleteFormat(t *testing.T) {
testStruct := struct { testStruct := struct {
Name string Name string
@ -38,6 +44,7 @@ func TestAutocompleteFormat(t *testing.T) {
Car *Car Car *Car
Car2 *Car Car2 *Car
*Anonymous *Anonymous
private int
}{} }{}
testStruct.Car = &Car{} testStruct.Car = &Car{}