Skip to content

Support struct tags for map keys - #311

Open
AgentGoose32 wants to merge 1 commit into
darccio:masterfrom
AgentGoose32:polar-255-map-key-tag
Open

Support struct tags for map keys#311
AgentGoose32 wants to merge 1 commit into
darccio:masterfrom
AgentGoose32:polar-255-map-key-tag

Conversation

@AgentGoose32

@AgentGoose32 AgentGoose32 commented Apr 28, 2026

Copy link
Copy Markdown

/claim #255

Summary

  • Adds WithMapKeyTag(tag) for struct-to-map mapping.
  • Uses the named struct tag value as the destination map key, including stripping comma options like json:"name,omitempty".
  • Falls back to the existing lower-camel field name when the tag is missing, empty, or -.
  • Adds regression coverage for tagged, untagged, and ignored-tag fields.

Validation

  • go test ./...
  • git diff --check

Summary by CodeRabbit

Release Notes

  • New Features
    • Added ability to use custom struct tags (e.g., JSON tags) as destination map keys when mapping structs to maps. Users can now specify their preferred tag for key derivation during struct-to-map conversion.

@coderabbitai

coderabbitai Bot commented Apr 28, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This pull request introduces support for using struct tags to determine destination map keys during struct-to-map conversion. It adds a new configuration option WithMapKeyTag() and updates the mapping logic to respect custom tag names when deriving map keys.

Changes

Cohort / File(s) Summary
Struct Tag Configuration
merge.go
Introduces mapKeyTag configuration field and exports WithMapKeyTag(tag) functional option to specify which struct tag should be used for deriving destination map keys.
Map Key Derivation Logic
map.go
Updates deepMap function to read the configured struct tag value, truncate at the first comma, and use it as the destination map key; falls back to default lowercased field name when tag is empty or "-".
Integration Test
issue255_test.go
Adds TestIssue255MapStructToMapWithTaggedKeys test verifying struct-to-map conversion respects JSON tags as map keys, including handling of tag options, json:"-" fields, and untagged fields.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 With whiskers twitched and hop so merry,
Tags guide our structs through the ferry,
From struct fields to maps they go,
Custom keys in a mapped tableau!
JSON tags now lead the way, hooray! 🗺️

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding support for using struct tags to determine map keys during struct-to-map mapping.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@AgentGoose32
AgentGoose32 force-pushed the polar-255-map-key-tag branch from 437eaf9 to 5c14736 Compare April 28, 2026 22:16

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
issue255_test.go (1)

9-14: Consider adding an explicit json:"" fallback case.

The implementation supports empty-tag fallback too; adding one field/assertion would lock that behavior down.

Proposed test extension
 type issue255Person struct {
 	FirstName string `json:"first_name"`
 	LastName  string `json:"last_name,omitempty"`
 	Password  string `json:"-"`
+	Nickname  string `json:""`
 	Age       int
 }
@@
 	person := issue255Person{
 		FirstName: "Ada",
 		LastName:  "Lovelace",
 		Password:  "secret",
+		Nickname:  "Countess",
 		Age:       36,
 	}
@@
 	if actual["password"] != "secret" {
 		t.Fatalf("expected '-' tag to fall back to default field name, got %#v", actual)
 	}
+	if actual["nickname"] != "Countess" {
+		t.Fatalf("expected empty tag name to fall back to default field name, got %#v", actual)
+	}
 	if actual["age"] != 36 {
 		t.Fatalf("expected untagged field to fall back to default field name, got %#v", actual)
 	}

Also applies to: 29-40

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@issue255_test.go` around lines 9 - 14, The test should include an explicit
field on the issue255Person struct that uses the empty-tag fallback (e.g., add a
new field with `json:""` tag) and an assertion verifying that encoding/decoding
uses the fallback behavior; update the test cases that exercise issue255Person
(the assertions around lines covering the encoding/decoding, referenced by the
struct and existing checks at lines ~29-40) to include expected JSON key/name
for that new field to lock in the empty-tag behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@issue255_test.go`:
- Around line 9-14: The test should include an explicit field on the
issue255Person struct that uses the empty-tag fallback (e.g., add a new field
with `json:""` tag) and an assertion verifying that encoding/decoding uses the
fallback behavior; update the test cases that exercise issue255Person (the
assertions around lines covering the encoding/decoding, referenced by the struct
and existing checks at lines ~29-40) to include expected JSON key/name for that
new field to lock in the empty-tag behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9b869b4c-0b98-4e45-b475-95850b428f65

📥 Commits

Reviewing files that changed from the base of the PR and between bd27904 and 5c14736.

📒 Files selected for processing (3)
  • issue255_test.go
  • map.go
  • merge.go

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.

1 participant