Skip to content

feat(xstructs): custom tags #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 22, 2025
Merged

feat(xstructs): custom tags #14

merged 6 commits into from
Apr 22, 2025

Conversation

ChBLA
Copy link
Contributor

@ChBLA ChBLA commented Apr 15, 2025

Adding support for custom tag ordering, allowing consumers to decide which tags should be considered and in which order.
Previously, consumers using e.g.

type A struct {
    B string `json:"-" yaml:"b"`
}

would see B omitted, as it prioritised JSON tags over YAML tags.

Now consumers can use:

xstructs.ToMap(s, xstructs.WithTags([]string{"yaml", "json"})

To prioritise YAML tags over JSON tags.

ChBLA added 3 commits April 15, 2025 13:39

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@ChBLA ChBLA marked this pull request as ready for review April 15, 2025 12:07
@ChBLA ChBLA requested a review from a team as a code owner April 15, 2025 12:07
@ChBLA ChBLA changed the title feat(xstructs): initial custom tag order feat(xstructs): custom tags Apr 15, 2025
@ChBLA ChBLA requested a review from alex5517 April 15, 2025 12:08
@alex5517
Copy link
Contributor

@ChBLA,

Does providing a list of tags: xstructs.ToMap(s, xstructs.WithTags("yaml", "json")) make a difference if we only consider this first one?

@alex5517
Copy link
Contributor

@ChBLA,

What behavior would be desired if tags were json:"values" yaml:",inline,omitempty" currently default would be to return:

Name: "values"
Options: []string{}

This might be the desired behavior 🤔

@ChBLA
Copy link
Contributor Author

ChBLA commented Apr 16, 2025

@ChBLA,

Does providing a list of tags: xstructs.ToMap(s, xstructs.WithTags("yaml", "json")) make a difference if we only consider this first one?

Yes, if, for example, you define the tags as xstructs.WithTags("yaml", "json"), but some struct fields (or all) do not contain yaml, it will use the json tags instead.

@ChBLA
Copy link
Contributor Author

ChBLA commented Apr 16, 2025

@ChBLA,

What behavior would be desired if tags were json:"values" yaml:",inline,omitempty" currently default would be to return:

Name: "values"
Options: []string{}

This might be the desired behavior 🤔

This is the behaviour using the default tag ordering (of json before yaml). If, instead, the desired result is to return:

Name: ""
Options: []string{"inline", "omitempty"}

Use xstructs.WithTags("yaml", "json")

ChBLA added 2 commits April 16, 2025 07:53
@ChBLA ChBLA requested a review from alex5517 April 16, 2025 05:57
@ChBLA ChBLA requested a review from KimNorgaard April 22, 2025 09:56
@ChBLA ChBLA merged commit 4893d4c into main Apr 22, 2025
1 check passed
@ChBLA ChBLA deleted the feat/xstructs-custom-tag-order branch April 22, 2025 10:36
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.

None yet

3 participants