-
Notifications
You must be signed in to change notification settings - Fork 4
/
pod.go
38 lines (31 loc) · 749 Bytes
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package langkit
import "encoding/json"
// Pod contains a string translated into multiple languages. It's ment to be serialized and stored on entities in databases, such as on "product description" or "title" columns.
type Pod map[string]string
func ParsePod(msg json.RawMessage) Pod {
p := &Pod{}
err := json.Unmarshal(msg, p)
if err != nil {
panic(err)
}
return *p
}
func CreatePod(def string) Pod {
return Pod{"default": def}
}
func (t Pod) Default() string {
return t.Get("default")
}
func (t Pod) Get(locale string) string {
if v, found := t["default"]; found {
return v
}
return "[no default]"
}
func (t Pod) JSON() json.RawMessage {
b, err := json.Marshal(t)
if err != nil {
panic(err)
}
return json.RawMessage(b)
}