@@ -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+
2937func 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 }
0 commit comments