Skip to content

Commit

Permalink
Merge pull request #6 from morikuni/json-marshaler
Browse files Browse the repository at this point in the history
Implement json.Marshaler
  • Loading branch information
morikuni authored Jun 6, 2023
2 parents a12853a + 42357e1 commit 1f8d06c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions mlang.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mlang

import (
"encoding/json"
"fmt"
"io"

Expand All @@ -14,6 +15,17 @@ import (
// Basically, golang.org/x/text/language.Tag is used, but you can use any your original types as well.
type Language = any

type randomLanguageType struct{}

var (
randomLanguage = randomLanguageType{}
defaultLanguage Language = randomLanguage
)

func SetDefaultLanguage(lang Language) {
defaultLanguage = lang
}

type Message interface {
failure.Field
isMessage()
Expand All @@ -29,6 +41,8 @@ type Dict[M string | Template] map[Language]M
var (
_ failure.ErrorFormatter = Dict[string]{}
_ slog.LogValuer = Dict[string]{}
_ json.Marshaler = Dict[string]{}
_ fmt.Stringer = Dict[string]{}
)

func (d Dict[M]) isMessage() {}
Expand Down Expand Up @@ -72,15 +86,9 @@ func (d Dict[M]) LogValue() slog.Value {
return slog.StringValue(d.String())
}

type randomLanguageType struct{}

var (
randomLanguage = randomLanguageType{}
defaultLanguage Language = randomLanguage
)

func SetDefaultLanguage(lang Language) {
defaultLanguage = lang
// MarshalJSON implements json.Marshaler.
func (d Dict[M]) MarshalJSON() ([]byte, error) {
return []byte(d.String()), nil
}

func (d Dict[M]) String() string {
Expand Down

0 comments on commit 1f8d06c

Please sign in to comment.