Skip to content

JSON inspect mode: honor -tags flag (currently always emits tag) #73

Description

@peczenyj

In -inspect -format=json, the tag field on each layout field is emitted unconditionally (with omitempty only as a guard against empty strings), regardless of whether -tags was passed. This is asymmetric with text inspect mode, where -tags gates tag visibility.

Reproduction

$ ./structalign -inspect -type=Tagged ./_example/
type Tagged struct { // ...
    Flag bool    // ...                    ← no tag shown
    ID string    // ...
}

$ ./structalign -inspect -type=Tagged -tags ./_example/
type Tagged struct { // ...
    Flag bool `json:"flag"`       // ...   ← tag shown
}

$ diff \
    <(./structalign -inspect -type=Tagged -format=json ./_example/) \
    <(./structalign -inspect -type=Tagged -format=json -tags ./_example/)
# (byte-identical — both contain `"tag": "json:\"flag\""`)

Cause

internal/ui/json.go:82-91 populates Tag: f.Tag directly from the common.LayoutField. The RenderJSON method doesn't take an opt.tags parameter, and the layout phase always populates Field.Tag (internal/layout/layout.go:177).

Fix

Plumb the -tags (i.e. KeepTags) choice into RenderJSON and clear the Tag on jsonLayoutField when !keepTags, mirroring the text path. Trade-off: tooling consumers lose tag data by default — but they can opt in with -tags (or STRUCTALIGN_TAGS=true, or tags = true in .structalignrc), which is consistent with text mode.

This also means the diff-mode JSON (where original/proposed already honor -tags via align.go:90-94) becomes the natural model: -tags is the single global "include tags in output" knob, regardless of mode or format.

Scope

  • Update RenderJSON signature to accept the keep-tags choice (or pass via Printer field — match existing patterns).
  • Update app.go:300-307 call sites.
  • Add a JSON inspect golden test case inspect_tags_tagged.json.golden with and without -tags.
  • Document in README's Configuration Reference table that -tags affects both text and JSON.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions