Skip to content

Commit da45726

Browse files
fix(audit-trail): fix event list fields names and validation (#5436)
1 parent 761f9a5 commit da45726

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

core/human/marshal.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ type Marshaler interface {
2626
MarshalHuman() (string, error)
2727
}
2828

29+
// Validates fields containing <index> placeholders
30+
func validateIndexedField(t reflect.Type, fieldName string) error {
31+
testFieldName := strings.ReplaceAll(fieldName, "<index>", "0")
32+
_, err := gofields.GetType(t, testFieldName)
33+
34+
return err
35+
}
36+
2937
func Marshal(data any, opt *MarshalOpt) (string, error) {
3038
if opt == nil {
3139
opt = &MarshalOpt{}
@@ -298,11 +306,20 @@ func marshalSlice(slice reflect.Value, opt *MarshalOpt) (string, error) {
298306

299307
// Validate that all field exist
300308
for _, f := range opt.Fields {
301-
_, err := gofields.GetType(itemType, f.FieldName)
302-
if err != nil {
303-
return "", &UnknownFieldError{
304-
FieldName: f.FieldName,
305-
ValidFields: gofields.ListFields(itemType),
309+
if strings.Contains(f.FieldName, "<index>") {
310+
if err := validateIndexedField(itemType, f.FieldName); err != nil {
311+
return "", &UnknownFieldError{
312+
FieldName: f.FieldName,
313+
ValidFields: gofields.ListFields(itemType),
314+
}
315+
}
316+
} else {
317+
_, err := gofields.GetType(itemType, f.FieldName)
318+
if err != nil {
319+
return "", &UnknownFieldError{
320+
FieldName: f.FieldName,
321+
ValidFields: gofields.ListFields(itemType),
322+
}
306323
}
307324
}
308325
}

internal/namespaces/audit_trail/v1alpha1/custom_event.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func eventListBuilder(c *core.Command) *core.Command {
1616
},
1717
{
1818
Label: "Name",
19-
FieldName: "Resource.Name",
19+
FieldName: "Resources.<index>.Name",
2020
},
2121
{
2222
Label: "StatusCode",
@@ -64,19 +64,11 @@ func eventListBuilder(c *core.Command) *core.Command {
6464
},
6565
{
6666
Label: "Type",
67-
FieldName: "Resource.Type",
67+
FieldName: "Resources.<index>.Type",
6868
},
6969
{
7070
Label: "ResourceID",
71-
FieldName: "Resource.ID",
72-
},
73-
{
74-
Label: "Resource Created At",
75-
FieldName: "Resource.CreatedAt",
76-
},
77-
{
78-
Label: "Resource Updated At",
79-
FieldName: "Resource.UpdatedAt",
71+
FieldName: "Resources.<index>.ID",
8072
},
8173
},
8274
}

0 commit comments

Comments
 (0)