Repository navigation
Expand file tree
/
Copy pathlangref.go
More file actions
64 lines (52 loc) · 1.64 KB
/
Copy pathlangref.go
File metadata and controls
64 lines (52 loc) · 1.64 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package activitypub
import (
"github.com/go-ap/errors"
"golang.org/x/text/language"
)
// LangRef is the type for a language reference code, should be an ISO639-1 language specifier.
type LangRef language.Tag
// NilLangRef represents a convention for a nil language reference.
// It is used for LangRefValue objects without an explicit language key.
var NilLangRef = und
// DefaultLang represents the default language reference used when using the convenience content generation.
var DefaultLang = English
// Valid
func (l LangRef) Valid() bool {
return len(l.String()) > 0 && l != LangRef(language.Und)
}
// Equal checks if the language ref is equal to the "other"
func (l LangRef) Equal(other LangRef) bool {
return (l.Valid() == other.Valid()) || l == other
}
// MakeRef
func MakeRef(raw []byte) LangRef {
return LangRef(language.Make(string(raw)))
}
func (l *LangRef) UnmarshalBinary(data []byte) error {
return errors.NotImplementedf("Binary functionality not implemented")
}
func (l LangRef) MarshalBinary() ([]byte, error) {
return nil, errors.NotImplementedf("Binary functionality not implemented")
}
// UnmarshalJSON decodes an incoming JSON document into the receiver object.
func (l *LangRef) UnmarshalJSON(data []byte) error {
return l.UnmarshalText(data)
}
// UnmarshalText implements the TextEncoder interface
func (l *LangRef) UnmarshalText(data []byte) error {
*l = NilLangRef
if len(data) == 0 {
return nil
}
if len(data) > 2 {
if data[0] == '"' && data[len(data)-1] == '"' {
*l = MakeRef(data[1 : len(data)-1])
}
} else {
*l = MakeRef(data)
}
return nil
}
func (l LangRef) String() string {
return language.Tag(l).String()
}