Skip to content

Commit

Permalink
Merge pull request #3 from morikuni/failure-support
Browse files Browse the repository at this point in the history
Support using with failure v2
  • Loading branch information
morikuni authored Jun 4, 2023
2 parents 06b3b6e + 2fe9b0f commit e9b0f7c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/morikuni/go-mlang
go 1.20

require golang.org/x/text v0.9.0

require github.com/morikuni/failure/v2 v2.0.0-20230512003104-6bb497ead261 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/morikuni/failure/v2 v2.0.0-20230512003104-6bb497ead261 h1:Rqq1Kh/HMFt8YZFeW5jopA+l2wjV2+tMcouxbwuMxqs=
github.com/morikuni/failure/v2 v2.0.0-20230512003104-6bb497ead261/go.mod h1:tHod902kOvu2+09OAbzPMrE4B8fIc+M/2kl/UI3mDQI=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
8 changes: 8 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package internal

type failureKey bool

const (
_key failureKey = true
FailureKey = _key
)
9 changes: 9 additions & 0 deletions mlang.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package mlang

import (
"fmt"

"github.com/morikuni/failure/v2"

"github.com/morikuni/go-mlang/internal"
)

// Language can be any comparable types.
// Basically, golang.org/x/text/language.Tag is used, but you can use any your original types as well.
type Language = any

type Message interface {
failure.Field
isMessage()
Get(lang Language) (string, bool)
MustGet(lang Language) string
Expand Down Expand Up @@ -42,6 +47,10 @@ func (d Dict[M]) MustGet(lang Language) string {
panic("empty set")
}

func (d Dict[M]) SetErrorField(field failure.FieldSetter) {
field.Set(internal.FailureKey, Message(d))
}

func eval(msg any, lang Language) (string, bool) {
switch m := msg.(type) {
case Template:
Expand Down
17 changes: 17 additions & 0 deletions mlangfailure/mlangfailure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mlangfailure

import (
"github.com/morikuni/failure/v2"

"github.com/morikuni/go-mlang"
"github.com/morikuni/go-mlang/internal"
)

const (
Key = internal.FailureKey
)

func MessageOf(err error) mlang.Message {
m, _ := failure.ValueAs[mlang.Message](err, Key)
return m
}
20 changes: 20 additions & 0 deletions mlangfailure/mlangfailure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mlangfailure

import (
"testing"

"github.com/morikuni/failure/v2"

"github.com/morikuni/go-mlang"
)

func TestMessageOf(t *testing.T) {
err := failure.Unexpected("unexpected", mlang.Dict[string]{1: "one"})
m := MessageOf(err)
if m == nil {
t.Fatal("m is nil")
}
if m.MustGet(1) != "one" {
t.Errorf("got %v, want %v", m.MustGet(1), "one")
}
}

0 comments on commit e9b0f7c

Please sign in to comment.