6
6
"io/ioutil"
7
7
"path"
8
8
"strings"
9
+ "os"
10
+ "reflect"
9
11
)
10
12
11
13
// Gonfig implementation
@@ -14,6 +16,22 @@ type JsonGonfig struct {
14
16
obj map [string ]interface {}
15
17
}
16
18
19
+ // FromJsonFile opens the file supplied and calls
20
+ // FromJson function
21
+ func FromJsonFile (filename string ) (Gonfig , error ) {
22
+ f , err := os .Open (filename )
23
+ if err != nil {
24
+ return nil , err
25
+ }
26
+ defer f .Close ()
27
+ config , err := FromJson (f )
28
+ if err != nil {
29
+ return nil , err
30
+ }
31
+
32
+ return config , nil
33
+ }
34
+
17
35
// FromJson reads the contents from the supplied reader.
18
36
// The content is parsed as json into a map[string]interface{}.
19
37
// It returns a JsonGonfig struct pointer and any error encountered
@@ -44,6 +62,26 @@ func (jgonfig *JsonGonfig) GetString(key string, defaultValue interface{}) (stri
44
62
}
45
63
}
46
64
65
+
66
+ func (jgonfig * JsonGonfig ) GetArray (key string , target []struct {}) ([]struct {}, error ) {
67
+
68
+
69
+ configValue , err := jgonfig .Get (key , "" )
70
+ if err != nil {
71
+ return []struct {}{}, err
72
+ }
73
+ if stringValue , ok := configValue .(string ); ok {
74
+
75
+ keysBody := []byte (stringValue )
76
+ json .Unmarshal (keysBody , & target )
77
+
78
+ return target , nil
79
+ } else {
80
+ return []struct {}{}, & UnexpectedValueTypeError {key : key , value : configValue , message : "value is not a string" }
81
+ }
82
+ }
83
+
84
+
47
85
// GetInt uses Get to fetch the value behind the supplied key.
48
86
// It returns a int with either the retreived value or the default value and any error encountered.
49
87
// If value is not a int it returns a UnexpectedValueTypeError
0 commit comments