Skip to content

Add FilterStructToMap for correct JSON-safe field filtering #673

@hanXen

Description

@hanXen

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:

  1. Filter fields based on include/exclude lists.
  2. Use the json tag as the key for the resulting map.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions