-
Notifications
You must be signed in to change notification settings - Fork 4
/
bitarray_encoding_example_test.go
104 lines (87 loc) · 1.9 KB
/
bitarray_encoding_example_test.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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray_test
import (
"encoding/json"
"fmt"
"github.com/tunabay/go-bitarray"
"gopkg.in/yaml.v3"
)
func ExampleBitArray_MarshalBinary() {
ba := bitarray.MustParse("1111-0101 11")
bin, _ := ba.MarshalBinary()
fmt.Printf("%x\n", bin)
// Output:
// f5c3
}
func ExampleBitArray_MarshalText() {
ba := bitarray.MustParse("1111-0101 11")
txt, _ := ba.MarshalText()
fmt.Printf("%s\n", txt)
// Output:
// 1111010111
}
func ExampleBitArray_MarshalJSON() {
data := &struct {
Foo *bitarray.BitArray `json:"foox"`
Bar *bitarray.BitArray
Baz *bitarray.BitArray
Qux *bitarray.BitArray
List []*bitarray.BitArray
}{
Foo: bitarray.MustParse("1111-0000 111"),
Bar: bitarray.MustParse("0000-0000"),
Baz: bitarray.MustParse(""),
List: []*bitarray.BitArray{
bitarray.MustParse("0001"),
bitarray.MustParse("1000"),
},
}
out, err := json.MarshalIndent(data, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
// Output:
// {
// "foox": "11110000111",
// "Bar": "00000000",
// "Baz": "",
// "Qux": null,
// "List": [
// "0001",
// "1000"
// ]
// }
}
func ExampleBitArray_MarshalYAML() {
data := &struct {
Foo *bitarray.BitArray `yaml:"foox"`
Bar *bitarray.BitArray
Baz *bitarray.BitArray
Qux *bitarray.BitArray
List []*bitarray.BitArray
}{
Foo: bitarray.MustParse("1111-0000 111"),
Bar: bitarray.MustParse("0000-0000"),
Baz: bitarray.MustParse(""),
List: []*bitarray.BitArray{
bitarray.MustParse("0001"),
bitarray.MustParse("1000"),
},
}
out, err := yaml.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(out))
// Output:
// foox: "11110000111"
// bar: "00000000"
// baz: ""
// qux: null
// list:
// - "0001"
// - "1000"
}