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

Export error variables that are returned from exported methods; fix d… #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

slessard
Copy link

Export error variables that are returned from exported methods
fix documentation for Get method

@fatih
Copy link
Owner

fatih commented Mar 18, 2021

Hi @slessard

If we expose these variables, they will become part of the library. I'm not sure I want to extend the API surface. How do you use it? I'm not sure exporting these variables is useful, but maybe I'm missing something. Thank you

@slessard
Copy link
Author

Exporting the error variables would allow me to check the type of error in a manner that is friendly to recent versions of Go using the errors.Is function. Here's an example:

for ;; {
	tags, err := structtag.Parse(string(field.Tag))
	if errors.Is(err, structtag.ErrTagKeySyntax) ||
		errors.Is(err, structtag.ErrTagSyntax) ||
		errors.Is(err, structtag.ErrTagValueSyntax) {
		// These minor errors I can live with
		continue
	} else {
		// Houston, we have a problem.
		return err
	}
	authorizedRolesAsCSVString, err = tags.Get(rolesTagKey)
	if errors.Is(err, structtag.ErrTagNotExist) {
		// This kind of error I can handle. There just aren't any roles
		continue
	} else {
		// Getting tags failed in some unexpected way. Time to bail out
		return err
	}
	// Process tags here
}

Without exported error variables I don't know of a good way to test the error return. This forces me to make assumptions about the structtags functions and the errors they return.

@BrianLeishman
Copy link

I see the biggest pro argument as being able to tell whether or not a tag exists, just like @slessard has written an example for. It would be nice to skip an operation if the tag simply doesn't exist

@zzjin
Copy link

zzjin commented Mar 15, 2023

Any update here for 2 years wait?

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.

5 participants