Skip to content

Conversation

@hanXen
Copy link
Contributor

@hanXen hanXen commented Aug 27, 2025

Resolves #673

Summary

This PR introduces FilterStructToMap, a utility function in the structs package that converts a struct into a map[string]any while respecting json tags, omitempty, and user-defined field filters.

This is a direct solution to the problem identified in naabu#1545, where the existing FilterStruct utility caused issues with JSON marshaling due to its zero-value filtering approach.

Implementation Details

  • FilterStructToMap[T any](...) (map[string]any, error):

    • Takes a struct and include/exclude field lists as input.
    • Iterates through struct fields using reflect.
    • Uses the json tag as the key for the output map.
    • Skips zero-valued fields when omitempty is present.
    • Respects the include/exclude lists for filtering.
    • Returns a clean map for JSON marshaling.
  • walkFilteredFields (Internal Helper):

    • To avoid code duplication, a new internal helper function, walkFilteredFields, has been created.
    • This function encapsulates the common logic of iterating through fields while checking against the include/exclude maps.
    • Refactored FilterStruct to reuse the same logic, reducing duplication.

This utility offers a reliable, reusable solution for JSON-safe struct filtering and serialization.

Copy link
Member

@Mzack9999 Mzack9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Mzack9999 Mzack9999 merged commit 0c88295 into projectdiscovery:main Aug 27, 2025
7 checks passed
@hanXen hanXen deleted the feat/filter-struct-to-map branch September 16, 2025 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add FilterStructToMap for correct JSON-safe field filtering

2 participants