Skip to content

Commit 58843fe

Browse files
committed
metric.go: implement TextMarshaler and TextUnmarshaler interface.
1 parent 561408f commit 58843fe

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

metric/metric.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package metric
22

3+
import (
4+
"errors"
5+
"strings"
6+
)
7+
38
// BOOL is an int32 for structure align.
49
type BOOL int32
510

@@ -15,6 +20,24 @@ func (b BOOL) String() string {
1520
return "false"
1621
}
1722

23+
// MarshalText is used to implement TextMarshaler interface.
24+
func (b BOOL) MarshalText() ([]byte, error) {
25+
return []byte(b.String()), nil
26+
}
27+
28+
// UnmarshalText is used to implement TextUnmarshaler interface.
29+
func (b *BOOL) UnmarshalText(data []byte) error {
30+
switch strings.ToLower(string(data)) {
31+
case "true":
32+
*b = BOOL(1)
33+
case "false":
34+
*b = BOOL(0)
35+
default:
36+
return errors.New("invalid BOOL value")
37+
}
38+
return nil
39+
}
40+
1841
// Metrics contains status about runtime submodules.
1942
type Metrics struct {
2043
Library LTStatus `toml:"library" json:"library"`

0 commit comments

Comments
 (0)