Skip to content

Commit

Permalink
Merge pull request #4 from morikuni/error-formatter
Browse files Browse the repository at this point in the history
Implement failure.ErrorFormatter
  • Loading branch information
morikuni authored Jun 6, 2023
2 parents fedb810 + 271b482 commit 693f7b9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jobs:
test:
name: test
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v3
- uses: reviewdog/action-golangci-lint@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func IHavePen(count int) mlang.Message {
}
}

func ExampleSet() {
func main() {
fmt.Println(InvalidUserName.MustGet(language.English))
// Invalid user name
fmt.Println(InvalidUserName.MustGet(language.Japanese))
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func IHavePen(count int) mlang.Message {
}
}

func ExampleSet() {
func ExampleDict() {
fmt.Println(InvalidUserName.MustGet(language.English))
fmt.Println(InvalidUserName.MustGet(language.Japanese))

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ go 1.20

require golang.org/x/text v0.9.0

require github.com/morikuni/failure/v2 v2.0.0-20230512003104-6bb497ead261 // indirect
require github.com/morikuni/failure/v2 v2.0.0-20230512003104-6bb497ead261
8 changes: 8 additions & 0 deletions mlang.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mlang

import (
"fmt"
"io"

"github.com/morikuni/failure/v2"

Expand Down Expand Up @@ -51,6 +52,13 @@ func (d Dict[M]) SetErrorField(field failure.FieldSetter) {
field.Set(internal.FailureKey, Message(d))
}

var _ failure.ErrorFormatter = Dict[string]{}

// FormatError implements failure.ErrorFormatter.
func (d Dict[M]) FormatError(w io.Writer) {
_, _ = io.WriteString(w, d.MustGet(-1)) // put -1 as a dummy language. Randomly picked language is used.
}

func eval(msg any, lang Language) (string, bool) {
switch m := msg.(type) {
case Template:
Expand Down

0 comments on commit 693f7b9

Please sign in to comment.