-
Notifications
You must be signed in to change notification settings - Fork 41
Description
Background
While fixing a bug in projectdiscovery/naabu
(see projectdiscovery/naabu#1545), we identified a limitation in the current structs.FilterStruct
utility.
It sets excluded fields to their Go zero-value. When marshaled to JSON, not all zero-values are omitted (e.g., time.Time
, fields without omitempty
), which leads to unexpected fields being included.
Proposal
To solve this problem generically, we propose adding a new utility function, FilterStructToMap
.
This function would have a similar signature to FilterStruct
but would return a map[string]any
instead of the original struct type T
. Its responsibilities would be:
- Filter fields based on
include/exclude
lists. - Use the
json
tag as the key for the resulting map. - Correctly handle the
omitempty
tag by not adding zero-valued fields to the map.
This provides a clean and reusable way to convert a struct into a filtered map, making it well-suited for preparing data for JSON marshaling. Beyond addressing the immediate need in naabu, it can also be applied across other ProjectDiscovery tools that face similar output-filtering challenges.