-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializers.go
More file actions
146 lines (116 loc) · 3.56 KB
/
serializers.go
File metadata and controls
146 lines (116 loc) · 3.56 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
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
package prefer
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"path"
"reflect"
"github.com/pelletier/go-toml/v2"
"github.com/yosuke-furukawa/json5/encoding/json5"
"gopkg.in/ini.v1"
"gopkg.in/yaml.v3"
)
// NOTE: It may make more sense to use a map to these instead of creating
// potentially unnecessray structs for implementing interfaces on.
type Serializer interface {
Serialize(interface{}) ([]byte, error)
Deserialize([]byte, interface{}) error
}
type YAMLSerializer struct{}
type XMLSerializer struct{}
type INISerializer struct{}
type JSONSerializer struct{}
type TOMLSerializer struct{}
type SerializerFactory func() Serializer
var defaultSerializers map[string]SerializerFactory
func NewSerializer(identifier string, content []byte) (serializer Serializer, err error) {
extension := path.Ext(identifier)
factory, ok := defaultSerializers[extension]
if !ok {
return nil, errors.New("No matching serializer for " + identifier)
}
return factory(), nil
}
func NewYAMLSerializer() Serializer {
return YAMLSerializer{}
}
func NewXMLSerializer() Serializer {
return XMLSerializer{}
}
func NewINISerializer() Serializer {
return INISerializer{}
}
func NewJSONSerializer() Serializer {
return JSONSerializer{}
}
func NewTOMLSerializer() Serializer {
return TOMLSerializer{}
}
func (this YAMLSerializer) Serialize(input interface{}) ([]byte, error) {
return yaml.Marshal(input)
}
func (this YAMLSerializer) Deserialize(input []byte, obj interface{}) error {
return yaml.Unmarshal(input, obj)
}
func (this XMLSerializer) Serialize(input interface{}) ([]byte, error) {
return xml.Marshal(input)
}
func (this XMLSerializer) Deserialize(input []byte, obj interface{}) error {
return xml.Unmarshal(input, &obj)
}
func (this INISerializer) Serialize(input interface{}) ([]byte, error) {
cfg := ini.Empty()
v := reflect.ValueOf(input)
t := reflect.TypeOf(input)
for i := 0; i < v.NumField(); i++ {
field := t.Field(i)
value := v.Field(i)
var strValue string
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
strValue = fmt.Sprintf("%d", value.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
strValue = fmt.Sprintf("%d", value.Uint())
case reflect.Float32, reflect.Float64:
strValue = fmt.Sprintf("%g", value.Float())
case reflect.Bool:
strValue = fmt.Sprintf("%t", value.Bool())
default:
strValue = value.String()
}
cfg.Section("").Key(field.Name).SetValue(strValue)
}
var buf bytes.Buffer
_, err := cfg.WriteTo(&buf)
return buf.Bytes(), err
}
func (this INISerializer) Deserialize(input []byte, obj interface{}) error {
cfg, err := ini.Load(input)
if err != nil {
return err
}
return cfg.MapTo(obj)
}
func (this JSONSerializer) Serialize(input interface{}) ([]byte, error) {
return json5.Marshal(input)
}
func (this JSONSerializer) Deserialize(input []byte, obj interface{}) error {
return json5.Unmarshal(input, obj)
}
func (this TOMLSerializer) Serialize(input interface{}) ([]byte, error) {
return toml.Marshal(input)
}
func (this TOMLSerializer) Deserialize(input []byte, obj interface{}) error {
return toml.Unmarshal(input, obj)
}
func init() {
defaultSerializers = make(map[string]SerializerFactory)
defaultSerializers[".json"] = NewJSONSerializer
defaultSerializers[".json5"] = NewJSONSerializer
defaultSerializers[".yml"] = NewYAMLSerializer
defaultSerializers[".yaml"] = NewYAMLSerializer
defaultSerializers[".xml"] = NewXMLSerializer
defaultSerializers[".ini"] = NewINISerializer
defaultSerializers[".toml"] = NewTOMLSerializer
}