-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.go
198 lines (166 loc) · 4.22 KB
/
serializer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cdb
import (
"encoding/json"
"io"
"os"
"strings"
"github.com/pelletier/go-toml/v2"
"gopkg.in/yaml.v3"
)
// Serializer holds the options for encoding or decoding a book.
type Serializer struct {
Indent int
Format string
Editable bool
Book *Book
encoder BookEncoder
decoder BookDecoder
}
// SerializerOpt sets the options for a serializer.
type SerializerOpt func(*Serializer)
// Encoder is an interface for types that can Encode a book.
type Encoder interface {
Encode(v any) error
}
// EncoderInit takes the serializer options and returns a function that
// initializes an Encoder.
type EncoderInit func(*Serializer) BookEncoder
// BookEncoder takes a io.Writer and returns a type that can Encode a book.
type BookEncoder func(w io.Writer) Encoder
// Decoder is an interface for types that can decode a book.
type Decoder interface {
Decode(v any) error
}
// DecoderInit takes the serializer options and returns a function that
// initializes a Decoder.
type DecoderInit func(*Serializer) BookDecoder
// BookDecoder takes a io.Writer and returns a type that can Decode a book.
type BookDecoder func(r io.Reader) Decoder
// NewSerializer constructs a serializer for a book with default options.
func NewSerializer(b *Book, opts ...SerializerOpt) *Serializer {
s := &Serializer{
Book: b,
Indent: 2,
Format: ".txt",
}
for _, opt := range opts {
opt(s)
}
return s
}
// Decoder takes a DecoderInit and sets the BookDecoder.
func (s *Serializer) Decoder(init DecoderInit) *Serializer {
s.decoder = init(s)
return s
}
// Encoder takes a EncoderInit and sets the BookEncoder.
func (s *Serializer) Encoder(init EncoderInit) *Serializer {
s.encoder = init(s)
return s
}
// WithIndent sets the number of spaces for indenting.
func WithIndent(n int) SerializerOpt {
return func(s *Serializer) {
s.Indent = n
}
}
// EditableOnly sets the option for serializing only editable book fields.
func EditableOnly() SerializerOpt {
return func(s *Serializer) {
s.Editable = true
}
}
// WithFormat sets the encoding format extension.
func WithFormat(ext string) SerializerOpt {
return func(s *Serializer) {
s.Format = ext
}
}
// DecodeYAML configures a YAML BookDecoder.
func DecodeYAML(s *Serializer) BookDecoder {
s.Format = ".yaml"
return func(r io.Reader) Decoder {
dec := yaml.NewDecoder(r)
return dec
}
}
// DecodeJSON configures a JSON BookDecoder.
func DecodeJSON(s *Serializer) BookDecoder {
s.Format = ".json"
return func(r io.Reader) Decoder {
dec := json.NewDecoder(r)
return dec
}
}
// DecodeTOML configures a TOML BookDecoder.
func DecodeTOML(s *Serializer) BookDecoder {
s.Format = ".toml"
return func(r io.Reader) Decoder {
dec := toml.NewDecoder(r)
return dec
}
}
// ReadFile reads a file for decoding.
func (s *Serializer) ReadFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
return s.ReadFrom(file)
}
// ReadFrom reads from io.Reader for decoding.
func (s *Serializer) ReadFrom(r io.Reader) error {
d := s.decoder(r)
return d.Decode(s.Book)
}
// EncodeYAML configures a YAML BookEncoder.
func EncodeYAML(s *Serializer) BookEncoder {
s.Format = ".yaml"
return func(w io.Writer) Encoder {
enc := yaml.NewEncoder(w)
if s.Indent > 0 {
enc.SetIndent(s.Indent)
}
return enc
}
}
// EncodeJSON configures a JSON BookEncoder.
func EncodeJSON(s *Serializer) BookEncoder {
s.Format = ".json"
return func(w io.Writer) Encoder {
enc := json.NewEncoder(w)
if s.Indent > 0 {
enc.SetIndent("", strings.Repeat(" ", s.Indent))
}
return enc
}
}
// EncodeTOML configures a TOML BookEncoder.
func EncodeTOML(s *Serializer) BookEncoder {
s.Format = ".toml"
return func(w io.Writer) Encoder {
enc := toml.NewEncoder(w)
if s.Indent > 0 {
enc.SetIndentSymbol(strings.Repeat(" ", s.Indent))
}
return enc
}
}
// WriteFile writes an encoded book to disk.
func (s *Serializer) WriteFile(name string) error {
file, err := os.Create(name + s.Format)
if err != nil {
return err
}
defer file.Close()
return s.WriteTo(file)
}
// WriteTo writes an encoded book to io.Writer.
func (s *Serializer) WriteTo(w io.Writer) error {
enc := s.encoder(w)
if s.Editable {
return enc.Encode(s.Book.EditableFields)
}
return enc.Encode(s.Book)
}