Skip to content

refactor: use NewLabeledResource constructor instead of inline struct literals#8344

Merged
lpcox merged 4 commits into
mainfrom
lpcox-fix-labeledresource-ctor-8319
Jun 30, 2026
Merged

refactor: use NewLabeledResource constructor instead of inline struct literals#8344
lpcox merged 4 commits into
mainfrom
lpcox-fix-labeledresource-ctor-8319

Conversation

@lpcox

@lpcox lpcox commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Replace all inline &difc.LabeledResource{} constructions with calls to the canonical difc.NewLabeledResource() constructor, reducing code duplication and ensuring consistent initialization.

Changes

File Change
internal/guard/noop.go Replace 5-line inline struct with difc.NewLabeledResource("noop resource (no restrictions)")
internal/guard/wasm_parse.go (line 403) Replace &difc.LabeledResource{} with difc.NewLabeledResource("") before fillLabeledResourceFromMap
internal/guard/wasm_parse.go (line 442) Same pattern as above for collection item labels
internal/guard/write_sink.go Replace inline struct with difc.NewLabeledResource(...) then override Secrecy with custom tags

Rationale

difc.NewLabeledResource() already initializes Description, Secrecy, Integrity, and Structure with proper defaults. Bypassing it with inline struct literals duplicates that logic and risks inconsistency if the constructor defaults change.

For write_sink.go, the constructor is used for default initialization, then Secrecy is overridden with NewSecrecyLabelWithTags since that case requires custom accept tags.

For wasm_parse.go, the constructor defaults are immediately overwritten by fillLabeledResourceFromMap, but using the constructor ensures all fields start with valid defaults rather than zero values.

Fixes #8319

… literals

Replace all inline `&difc.LabeledResource{}` constructions with calls to
`difc.NewLabeledResource()` to use the canonical constructor consistently:

- internal/guard/noop.go: Replace inline struct with NewLabeledResource
- internal/guard/wasm_parse.go (2 sites): Replace empty struct literals
- internal/guard/write_sink.go: Replace inline struct, override Secrecy after

Fixes #8319

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 30, 2026 13:26
GitHub Advanced Security started work on behalf of lpcox June 30, 2026 13:27 View session
GitHub Advanced Security finished work on behalf of lpcox June 30, 2026 13:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors DIFC guard code to consistently initialize difc.LabeledResource via the canonical difc.NewLabeledResource() constructor instead of inline struct literals, reducing duplicated initialization logic and centralizing defaults.

Changes:

  • Refactor NoopGuard to construct its labeled resource via difc.NewLabeledResource(...).
  • Refactor WASM guard response parsing to start from a constructor-created LabeledResource before populating fields from decoded JSON.
  • Refactor WriteSinkGuard to use difc.NewLabeledResource(...) and then override Secrecy with configured accept tags.
Show a summary per file
File Description
internal/guard/noop.go Switches noop resource construction to difc.NewLabeledResource(...).
internal/guard/wasm_parse.go Uses difc.NewLabeledResource("") as the base object before filling labels from WASM response maps.
internal/guard/write_sink.go Uses difc.NewLabeledResource(...) then overrides Secrecy with NewSecrecyLabelWithTags(...).

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 3
  • Review effort level: Low

Comment thread internal/guard/noop.go Outdated
Comment thread internal/guard/wasm_parse.go Outdated
Comment thread internal/guard/wasm_parse.go Outdated
lpcox and others added 3 commits June 30, 2026 06:32
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 30, 2026 13:32 View session
GitHub Advanced Security started work on behalf of lpcox June 30, 2026 13:32 View session
GitHub Advanced Security started work on behalf of lpcox June 30, 2026 13:32 View session
GitHub Advanced Security finished work on behalf of lpcox June 30, 2026 13:33
GitHub Advanced Security finished work on behalf of lpcox June 30, 2026 13:33
GitHub Advanced Security finished work on behalf of lpcox June 30, 2026 13:33
@lpcox lpcox merged commit a47eb6d into main Jun 30, 2026
24 checks passed
@lpcox lpcox deleted the lpcox-fix-labeledresource-ctor-8319 branch June 30, 2026 13:37
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.

[duplicate-code] Duplicate Code Pattern: Inline LabeledResource{} Construction Bypasses Existing Constructor

2 participants