parser: Drop empty sub maps from hugo config output#14861
Conversation
When OmitEmpty is true, the marshaller recursively pruned zero-valued leaves from the unmarshalled map, but left empty parent maps in place. This surfaced in 'hugo config' as empty TOML tables such as [permalinks.target.sites] and its nested children, even when every field underneath was zero. After recursing into a child map, delete the parent's key if the child is now empty. Slices of maps are unaffected because removing slice entries would shift indices. Fixes gohugoio#14855 Signed-off-by: ChrisJr404 <chris@hacknow.com>
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. |
|
|
|
Can you push the "sign the CLA" button above? If you already did,. you need to check if your email address for the commit matches one of the addresses connected to your GitHub account. |
Took a stab at this. Happy to drop the PR if you've already got something underway.
ReplacingJSONMarshaller.MarshalJSONwalks the unmarshalled map and deletes zero-valued leaves whenOmitEmptyis true, but it skips the truthiness check for any value that is itself a map. So once recursion empties a child map, the now-empty parent map sticks around in the output. With the issue's input, that surfaces as:sitesmatrix.Sites.IsZerois defined and would return true here, but at this point in the pipeline everything is already amap[string]any, so the struct-levelIsZerois gone.The change after recursing into a child map, drop the key if the child is now empty. Slices of maps are deliberately left alone since removing slice entries would shift indices and the existing behavior never did that.
Verified locally with the issue's
hugo.toml. Output forpermalinks,privacy, etc. is now collapsed to just the populated fields, in--format toml,--format yaml, and--format json.--printZerois unchanged (that path doesn't enter theOmitEmptyblock).Added a regression test in
parser/lowercase_camel_json_test.gocovering:target.sites.{matrix,complements}shape with empty slice fieldsFixes #14855