8000 Fix `incorrectTypeForFlagError` for unknowns by danhunsaker · Pull Request #1708 · urfave/cli · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@danhunsaker
Copy link

The reflect package doesn't always know what to call a type in (reflect.Type).Name(), but almost always gets it right in (reflect.Type).String(). Fall back to that so we get actionable error messages.

What type of PR is this?

  • bug

What this PR does / why we need it:

When configs parsed into MapInputSource are looked up, they report type mismatch errors, as expected. However, if the type is mismatched for any reason, the error message isn't particularly useful - it reports an "actual type" of ''. This is a known limitation of the way (reflect.Type).Name() works (it only provides names for package-defined types, not built-in composites or pointers or a handful of other things). Falling back to (reflect.Type).String() in these cases provides enough data for a developer to fix the actual issue (in this case, using the correct type in their InputSourceContext implementation), which is the entire point of returning a type name in that specific error message.

v3 doesn't have support for InputSourceContexts (or any equivalent I could find), so this fix currently only targets v2.

Testing

A test was added to ensure this change works as expected.

Release Notes

NONE

@danhunsaker danhunsaker requested a review from a team as a code owner March 19, 2023 19:12
@skelouse
Copy link
Contributor
skelouse commented Mar 19, 2023

Why don't we just do:

fmt.Errorf("Mismatched type for flag '%s'. Expected '%s' but actual is '%T'", name, expectedTypeName, value)
func TestXxx(t *testing.T) {
	var value *string

	func(_value interface{}) {
		fmt.Printf("Got: %T", _value)
	}(value)
}

// Output: Got: *string

@danhunsaker
Copy link
Author

That's a good idea. Wonder if %T existed when this was originally written...

@skelouse
Copy link
Contributor

That's a good idea. Wonder if %T existed when this was originally written...

Looks like it has been around since go1, https://pkg.go.dev/fmt@go1

The `reflect` package doesn't always know what to call a type in
`(reflect.Type).Name()`, but `fmt.Errorf("%T")` always gets it right.
Use that so we get actionable error messages.
@meatballhat meatballhat added kind/bug describes or fixes a bug area/v2 relates to / is being considered for v2 labels Mar 26, 2023
@meatballhat meatballhat added this to the Release 2.x milestone Mar 26, 2023
@meatballhat meatballhat changed the title v2 - Fix incorrectTypeForFlagError for unknowns Fix incorrectTypeForFlagError for unknowns Mar 26, 2023
Copy link
Member
@meatballhat meatballhat left a comment

Choose a reason for hiding this comment

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

Thank you!

@meatballhat meatballhat merged commit c69f466 into urfave:v2-maint May 1, 2023
mauromorales referenced this pull request in kairos-io/provider-kairos May 3, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/urfave/cli/v2](https://togithub.com/urfave/cli) | require
| patch | `v2.25.1` -> `v2.25.3` |

---

### Release Notes

<details>
<summary>urfave/cli</summary>

### [`v2.25.3`](https://togithub.com/urfave/cli/releases/tag/v2.25.3)

[Compare
Source](https://togithub.com/urfave/cli/compare/v2.25.2...v2.25.3)

#### What's Changed

- Fix `incorrectTypeForFlagError` for unknowns by
[@&#8203;danhunsaker](https://togithub.com/danhunsaker) in
[https://github.com/urfave/cli/pull/1708](https://togithub.com/urfave/cli/pull/1708)

#### New Contributors

- [@&#8203;danhunsaker](https://togithub.com/danhunsaker) made their
first contribution in
[https://github.com/urfave/cli/pull/1708](https://togithub.com/urfave/cli/pull/1708)

**Full Changelog**:
urfave/cli@v2.25.2...v2.25.3

### [`v2.25.2`](https://togithub.com/urfave/cli/releases/tag/v2.25.2)

[Compare
Source](https://togithub.com/urfave/cli/compare/v2.25.1...v2.25.2)

#### What's Changed

- Fix missing required flag error uses flag name and not alias by
[@&#8203;nirhaas](https://togithub.com/nirhaas) in
[https://github.com/urfave/cli/pull/1709](https://togithub.com/urfave/cli/pull/1709)
- Remove redundant variable declarations by
[@&#8203;huiyifyj](https://togithub.com/huiyifyj) in
[https://github.com/urfave/cli/pull/1714](https://togithub.com/urfave/cli/pull/1714)
- Fix:(issue 1689) Match markdown output with help output by
[@&#8203;dearchap](https://togithub.com/dearchap) in
[https://github.com/urfave/cli/pull/1723](https://togithub.com/urfave/cli/pull/1723)

#### New Contributors

- [@&#8203;nirhaas](https://togithub.com/nirhaas) made their first
contribution in
[https://github.com/urfave/cli/pull/1709](https://togithub.com/urfave/cli/pull/1709)
- [@&#8203;huiyifyj](https://togithub.com/huiyifyj) made their first
contribution in
[https://github.com/urfave/cli/pull/1714](https://togithub.com/urfave/cli/pull/1714)

**Full Changelog**:
urfave/cli@v2.25.1...v2.25.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/kairos-io/provider-kairos).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42Ni4xIiwidXBkYXRlZEluVmVyIjoiMzUuNjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0