-
Notifications
You must be signed in to change notification settings - Fork 3
/
parsers.go
172 lines (134 loc) · 4.03 KB
/
parsers.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
package parsers
import (
"encoding/base64"
"encoding/csv"
"encoding/hex"
"encoding/json"
"encoding/xml"
"io"
"golang.org/x/exp/maps"
"golang.org/x/tools/txtar"
"gopkg.in/yaml.v2"
"go.autokitteh.dev/autokitteh/internal/kittehs"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
)
var (
Parsers = map[string]func(io.Reader) (sdktypes.Value, error){
"txt": ParseText,
"json": ParseJSON,
"yaml": ParseYAML,
"kitteh.yaml": ParseRawYAML,
"kitteh.json": ParseRawJSON,
"csv": ParseCSV,
"b64": ParseBase64,
"hex": ParseHex,
"xml": ParseXML,
"txtar": ParseTxTar,
}
Extensions = maps.Keys(Parsers)
ExtensionsWithDotPrefix = kittehs.Transform(Extensions, func(s string) string { return "." + s })
)
func ParseJSON(r io.Reader) (sdktypes.Value, error) {
// TODO: allow to read lists and sets?
var m map[string]any
if err := json.NewDecoder(r).Decode(&m); err != nil {
// TODO: ProgramError.
return sdktypes.InvalidValue, err
}
return sdktypes.DefaultValueWrapper.Wrap(m)
}
func ParseRawJSON(r io.Reader) (sdktypes.Value, error) {
var v sdktypes.Value
// TODO: there are some stuff that must not be allowed to be parseled, such as Functions.
if err := json.NewDecoder(r).Decode(&v); err != nil {
// TODO: ProgramError.
return sdktypes.InvalidValue, err
}
return v, nil
}
func ParseYAML(r io.Reader) (sdktypes.Value, error) {
// TODO: allow to read lists and sets?
var m map[string]any
if err := yaml.NewDecoder(r).Decode(&m); err != nil {
// TODO: ProgramError.
return sdktypes.InvalidValue, err
}
return sdktypes.DefaultValueWrapper.Wrap(m)
}
func ParseRawYAML(r io.Reader) (sdktypes.Value, error) {
var v sdktypes.Value
// TODO: there are some stuff that must not be allowed to be parseled, such as Functions.
if err := yaml.NewDecoder(r).Decode(&v); err != nil {
// TODO: ProgramError.
return sdktypes.InvalidValue, err
}
return v, nil
}
func ParseXML(r io.Reader) (sdktypes.Value, error) {
// TODO: allow to read lists and sets?
var m map[string]any
if err := xml.NewDecoder(r).Decode(&m); err != nil {
// TODO: ProgramError.
return sdktypes.InvalidValue, err
}
return sdktypes.DefaultValueWrapper.Wrap(m)
}
func ParseText(r io.Reader) (sdktypes.Value, error) {
data, err := io.ReadAll(r)
if err != nil {
return sdktypes.InvalidValue, err
}
return sdktypes.NewDictValueFromStringMap(map[string]sdktypes.Value{
"text": sdktypes.NewStringValue(string(data)),
}), nil
}
func ParseCSV(r io.Reader) (sdktypes.Value, error) {
rs, err := csv.NewReader(r).ReadAll()
if err != nil {
return sdktypes.InvalidValue, err
}
v := kittehs.Must1(sdktypes.DefaultValueWrapper.Wrap(rs))
return sdktypes.NewDictValueFromStringMap(map[string]sdktypes.Value{
"data": v,
}), nil
}
func ParseHex(r io.Reader) (sdktypes.Value, error) {
encoded, err := io.ReadAll(r)
if err != nil {
return sdktypes.InvalidValue, err
}
decoded, err := hex.DecodeString(string(encoded))
if err != nil {
return sdktypes.InvalidValue, err
}
return sdktypes.NewDictValueFromStringMap(map[string]sdktypes.Value{
"data": sdktypes.NewBytesValue(decoded),
}), nil
}
func ParseBase64(r io.Reader) (sdktypes.Value, error) {
encoded, err := io.ReadAll(r)
if err != nil {
return sdktypes.InvalidValue, err
}
decoded, err := base64.StdEncoding.DecodeString(string(encoded))
if err != nil {
return sdktypes.InvalidValue, err
}
return sdktypes.NewDictValueFromStringMap(map[string]sdktypes.Value{
"data": sdktypes.NewBytesValue(decoded),
}), nil
}
func ParseTxTar(r io.Reader) (sdktypes.Value, error) {
data, err := io.ReadAll(r)
if err != nil {
return sdktypes.InvalidValue, err
}
archive := txtar.Parse(data)
files := kittehs.ListToMap(archive.Files, func(f txtar.File) (string, sdktypes.Value) {
return f.Name, sdktypes.NewStringValue(string(f.Data))
})
return sdktypes.NewDictValueFromStringMap(map[string]sdktypes.Value{
"comment": sdktypes.NewStringValue(string(archive.Comment)),
"files": sdktypes.NewDictValueFromStringMap(files),
}), nil
}