-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
43 lines (37 loc) · 1.07 KB
/
parser.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
package schmp
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v3"
)
type parser func(options ProcessingOptions) ([]map[string]interface{}, error)
func getParser(mode string) (parser, error) {
switch mode {
case "json":
return buildParserUsingUnmarshaller(json.Unmarshal), nil
case "yaml":
return buildParserUsingUnmarshaller(yaml.Unmarshal), nil
case "toml":
return buildParserUsingUnmarshaller(toml.Unmarshal), nil
}
return nil, fmt.Errorf(`mode "%s" is not supported`, mode)
}
func buildParserUsingUnmarshaller(unmarshal func([]byte, interface{}) error) func(options ProcessingOptions) ([]map[string]interface{}, error) {
return func(options ProcessingOptions) ([]map[string]interface{}, error) {
results := make([]map[string]interface{}, len(options.Sources))
for i, v := range options.Sources {
b, err := ioutil.ReadAll(v)
if err != nil {
return nil, err
}
results[i] = make(map[string]interface{})
err = unmarshal(b, &results[i])
if err != nil {
return nil, err
}
}
return results, nil
}
}