Skip to content
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

Add user-defined tag naming #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

johnnybubonic
Copy link

Frequently, many other libraries use a default tag (off the top of my head, https://github.com/jessevdk/go-flags does but I'm sure there are others.)

These changes allow the caller to define a custom tag name, either globally or locally to a function call, to avoid runtime conflicts with these other libraries.

These changes are 100% backwards-compatible, no changes to exported API have been made and all existing consuming code will function as it did before.

These changes allow the caller to define a custom tag name, either
globally or locally to a function call.
Copy link

codecov bot commented Nov 20, 2024

Codecov Report

Attention: Patch coverage is 55.55556% with 8 lines in your changes missing coverage. Please review.

Project coverage is 86.14%. Comparing base (abebf4b) to head (36bbebb).

Files with missing lines Patch % Lines
defaults.go 55.55% 7 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #53      +/-   ##
==========================================
- Coverage   95.97%   86.14%   -9.83%     
==========================================
  Files           2        2              
  Lines         149      166      +17     
==========================================
  Hits          143      143              
- Misses          3       19      +16     
- Partials        3        4       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@nf-brentsaner
Copy link

Alternatively, you could also just call SetWithTag(obj, TagName) inside Set() but I wasn't sure on your preference here.

I can write additional tests if you let me know if the current implementation is ideal or if you want it wrapped like that.

README.md Outdated Show resolved Hide resolved
defaults.go Outdated Show resolved Hide resolved
@johnnybubonic
Copy link
Author

I also realized the Set() calls in the struct recursion during reflection would also need to be updated

@johnnybubonic
Copy link
Author

johnnybubonic commented Dec 5, 2024

FWIW, looks like the most recent circleci test failed was due to linting; it doesn't like that the block-commented example output isn't indented an additional level.

(Edit: whoops, didn't mean to close)

Name string `default:"John Smith"`
Age int `default:"27"`
Gender Gender `default:"m"`
Name string `default:"John Smith" default_alt:"Jane Doe"`
Copy link
Owner

@creasty creasty Dec 6, 2024

Choose a reason for hiding this comment

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

Would you please revert these changes?
"User-Definable tag name" should not be this library's primary use case.

The same goes for example/main.go — Although it might be useful to have a separate file.

// SetWithTag initializes members in a struct referenced by a pointer using an explicit tag name.
// Maps and slices are initialized by `make` and other primitive types are set with default values.
// `ptr` should be a struct pointer
func SetWithTag(ptr interface{}, runtimeTag string) error {
Copy link
Owner

Choose a reason for hiding this comment

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

nit: runtimeTag sounds a bit off to me. Reflection at its essence is about runtime.
tagName should suffice.

Copy link
Owner

Choose a reason for hiding this comment

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

We'd also like to add minimum specs for testing user-specified tag names.
Do note that we don't want to extend the existing struct; create a new struct to test with.

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.

4 participants