-
Notifications
You must be signed in to change notification settings - Fork 6
/
unmarshal_config.go
60 lines (44 loc) · 1.48 KB
/
unmarshal_config.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
package amanar
import (
"encoding/json"
"fmt"
"github.com/icza/dyno"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
)
func unmarshalConfiguration(bytes []byte) (*Amanar, error) {
c, err := UnmarshalYamlAmanarConfiguration(bytes)
if err != nil {
return nil, fmt.Errorf("could not unmarshal amanar configuration: %w", err)
}
return &c, err
}
func convertToJson(bytes []byte) ([]byte, error) {
c, err := DynamicUnmarshalYamlAmanarConfiguration(bytes)
if err != nil {
return nil, err
}
marshalableMap := dyno.ConvertMapI2MapS(c)
return json.Marshal(marshalableMap)
}
//go:generate go-bindata -pkg amanar amanar_config_schema.json
func LoadConfiguration(configFilepath string) (*Amanar, error, []gojsonschema.ResultError) {
bytes, err := ioutil.ReadFile(configFilepath)
if err != nil {
return nil, fmt.Errorf("could not read amanar configuration file: %w", err), nil
}
configuration, err := convertToJson(bytes)
if err != nil {
return nil, fmt.Errorf("could not load amanar configuration to JSON for validation: %w", err), nil
}
validator := NewJsonBytesSchemaValidator(configuration)
err, validationErrors := validator.Validate()
if err != nil {
return nil, fmt.Errorf("[JSON CONFIG SCHEMA VALIDATION] could not run validation for JSON: %w", err), nil
}
parsedConfiguration, err := unmarshalConfiguration(bytes)
if err != nil {
return nil, fmt.Errorf("could not load amanar configuration to struct: %w", err), nil
}
return parsedConfiguration, err, validationErrors
}