/
directory_filetypes.go
136 lines (114 loc) · 2.99 KB
/
directory_filetypes.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
/*
* Copyright Tamás Demeter-Haludka 2021
*
* This file is part of Constellation.
*
* Constellation is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Constellation is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Constellation. If not, see <https://www.gnu.org/licenses/>.
*/
package config
import (
"encoding/json"
"encoding/xml"
"io"
"io/ioutil"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v2"
)
type JSON struct {
Strict bool
Prefix string
Indent string
}
func (t *JSON) Extensions() []string {
return []string{"json"}
}
func (t *JSON) Unmarshal(stream io.Reader, v interface{}) error {
dec := json.NewDecoder(stream)
if t.Strict {
dec.DisallowUnknownFields()
}
return dec.Decode(v)
}
func (t *JSON) Marshal(stream io.Writer, v interface{}) error {
enc := json.NewEncoder(stream)
enc.SetIndent(t.Prefix, t.Indent)
return enc.Encode(v)
}
type YAML struct {
Strict bool
}
func (t *YAML) Extensions() []string {
return []string{"yml", "yaml"}
}
func (t *YAML) Unmarshal(stream io.Reader, v interface{}) error {
data, err := ioutil.ReadAll(stream)
if err != nil {
return err
}
if t.Strict {
return yaml.UnmarshalStrict(data, v)
} else {
return yaml.Unmarshal(data, v)
}
}
func (t *YAML) Marshal(stream io.Writer, v interface{}) error {
data, err := yaml.Marshal(v)
if err != nil {
return err
}
_, err = stream.Write(data)
return err
}
type TOML struct {
ArraysWithOneElementPerLine bool
QuoteMapKeys bool
}
func (t *TOML) Extensions() []string {
return []string{"toml"}
}
func (t *TOML) Unmarshal(stream io.Reader, v interface{}) error {
return toml.NewDecoder(stream).Decode(v)
}
func (t *TOML) Marshal(stream io.Writer, v interface{}) error {
enc := toml.NewEncoder(stream)
enc.ArraysWithOneElementPerLine(t.ArraysWithOneElementPerLine)
enc.QuoteMapKeys(t.QuoteMapKeys)
return enc.Encode(v)
}
type XML struct {
Strict bool
AutoClose []string
Entity map[string]string
CharsetReader func(charset string, input io.Reader) (io.Reader, error)
DefaultSpace string
Prefix string
Indent string
}
func (t *XML) Extensions() []string {
return []string{"xml"}
}
func (t *XML) Unmarshal(stream io.Reader, v interface{}) error {
dec := xml.NewDecoder(stream)
dec.Strict = t.Strict
dec.AutoClose = t.AutoClose
dec.Entity = t.Entity
dec.CharsetReader = t.CharsetReader
dec.DefaultSpace = t.DefaultSpace
return dec.Decode(v)
}
func (t *XML) Marshal(stream io.Writer, v interface{}) error {
enc := xml.NewEncoder(stream)
enc.Indent(t.Prefix, t.Indent)
return enc.Encode(v)
}