spf13 / spf13/cast

Wrap a common "cast error" for To___E functions

Open
#86 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
4k
Forks
337
Avg merge
15m
Merged PRs (30d)
1

Description

To make it easier to use Go1.13's errors.Is and errors.As, it'd be great if the To___E functions wrap a common "cast error" so that transitive callers to these functions can determine if an error comes from casting.

A slightly-silly example usecase:

func main() {
  val, err := maybeCast("not_an_int")
  if errors.Is(err, cast.Err) {
    fmt.Println("unable to cast!")
  }
  fmt.Println(val)
}

func maybeCast(myVal string) (int, error) {
  if someCondition() {
    return cast.ToIntE(myVal)
  }
  return fmt.Errorf("unrelated error")
}

If there's interest, I'd love to make a PR. :)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the To___E functions and reviewing how their errors are currently produced. Check Go 1.13 errors.Is and errors.As behavior, then define the shared cast-error behavior and verify that callers can identify casting failures without matching individual messages.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.