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

feat: add TextFlag #2055

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

feat: add TextFlag #2055

wants to merge 2 commits into from

Conversation

somebadcode
Copy link

@somebadcode somebadcode commented Feb 10, 2025

  • Added TextFlag which supports setting values for types that satisfies both encoding.TextMarshaller and encoding.TextUnmarshaller which is very handy when you want to set log levels or string-like types that satisfies the interfaces.

Fixes: #2051

What type of PR is this?

  • feature

What this PR does / why we need it:

Making it easier to set types that satisfies encoding.TextMarshaller and encoding.TextUnmarshaller such as log/slog.LogLevelVar, using flags as see in the standard library's flag.

Which issue(s) this PR fixes:

Fixes #2051

Release Notes

Added `TextFlag` to support setting values for types that satisfy both  `encoding.TextMarshaller` and `encoding.TextUnmarshaller`.

* Added `TextFlag` which supports setting values for types that satisfies
  the `encoding.TextMarshaller` and `encoding.TextUnmarshaller` which is
  very handy when you want to set log levels or string-like types that
  satifies the interfaces.

Fixes: urfave#2051
Signed-off-by: Tobias Dahlberg <[email protected]>
@somebadcode somebadcode requested a review from a team as a code owner February 10, 2025 20:01
Signed-off-by: Eng Zer Jun <[email protected]>
Copy link
Member

@Juneezee Juneezee left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! 😊

Just a few comments regarding the tests. If possible, please add a few more test cases to cover edge cases and improve coverage.

return err
}

if slices.Compare(text, []byte("INFO")) != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if slices.Compare(text, []byte("INFO")) != 0 {
if !slices.Equal(text, []byte("INFO")) {

Comment on lines +88 to +104
if err := tt.flag.Apply(set); err != nil {
t.Fatalf("Apply(%v) failed: %v", tt.args, err)
}

err := set.Parse(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)

return
} else if (err != nil) == tt.wantErr {
// Expected error.
return
}

if got := tt.flag.GetValue(); got != tt.want {
t.Errorf("Value = %v, want %v", got, tt.want)
}
Copy link
Member

Choose a reason for hiding this comment

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

Use the testify package for assertions

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.

Text flags
2 participants