@@ -14,44 +14,48 @@ Array [
14
14
]
15
15
` ;
16
16
17
- exports[` Golang File Generation creates a simple Go file 1 ` ] = `
17
+ exports[` Golang File Generation creates a Go file without singleton 1 ` ] = `
18
18
Array [
19
19
" // @generated by app-config" ,
20
20
" " ,
21
21
" package main" ,
22
22
" " ,
23
23
" import (" ,
24
24
" \\ " encoding / json \\" " ,
25
+ " \\ " errors \\" " ,
26
+ " \\ " fmt \\" " ,
25
27
" \\ " log \\" " ,
26
28
" \\ " os \\" " ,
27
29
" " ,
28
30
" \\ " github .com / xeipuuv / gojsonschema \\" " ,
29
31
" )" ,
30
32
" " ,
31
- " var config Configuration" ,
33
+ " func LoadConfig() (Configuration, error) {" ,
34
+ " var loadedConfig Configuration" ,
35
+ " var loadedSchema map[string]interface{}" ,
36
+ " var err error" ,
32
37
" " ,
33
- " func init() {" ,
34
38
" configText := os.Getenv(\\ " APP_CONFIG \\" )" ,
35
39
" schemaText := os.Getenv(\\ " APP_CONFIG_SCHEMA \\" )" ,
36
40
" " ,
37
41
" if configText == \\ " \\" {" ,
38
- " log.Panic (\\ " The APP_CONFIG environment variable was not set \\" )" ,
42
+ " return loadedConfig, errors.New (\\ " The APP_CONFIG environment variable was not set \\" )" ,
39
43
" }" ,
40
44
" " ,
41
45
" if schemaText == \\ " \\" {" ,
42
- " log.Panic (\\ " The APP_CONFIG_SCHEMA environment variable was not set \\" )" ,
46
+ " return loadedConfig, errors.New (\\ " The APP_CONFIG_SCHEMA environment variable was not set \\" )" ,
43
47
" }" ,
44
48
" " ,
45
- " loadedSchema, err := UnmarshalConfig ([]byte(schemaText))" ,
49
+ " err = json.Unmarshal ([]byte(schemaText), &loadedSchema )" ,
46
50
" " ,
47
51
" if err != nil {" ,
48
- " log.Panic (\\ " Could not parse APP_CONFIG_SCHEMA environment variable : \\" , err)" ,
52
+ " return loadedConfig, fmt.Errorf (\\ " Could not parse APP_CONFIG_SCHEMA environment variable : % s \\" , err.Error() )" ,
49
53
" }" ,
50
54
" " ,
51
- " loadedConfig, err := UnmarshalConfig ([]byte(configText))" ,
55
+ " err = json.Unmarshal ([]byte(configText), &loadedConfig )" ,
52
56
" " ,
53
57
" if err != nil {" ,
54
- " log.Panic (\\ " Could not parse APP_CONFIG environment variable : \\" , err)" ,
58
+ " return loadedConfig, fmt.Errorf (\\ " Could not parse APP_CONFIG environment variable : % s \\" , err.Error() )" ,
55
59
" }" ,
56
60
" " ,
57
61
" schemaLoader := gojsonschema.NewGoLoader(loadedSchema)" ,
@@ -60,15 +64,66 @@ Array [
60
64
" result, err := gojsonschema.Validate(schemaLoader, documentLoader)" ,
61
65
" " ,
62
66
" if err != nil {" ,
63
- " log.Panic (\\ " Could not validate app - config : \\" , err.Error())" ,
67
+ " return loadedConfig, fmt.Errorf (\\ " Could not validate App Config : % s \\" , err.Error())" ,
64
68
" }" ,
65
69
" " ,
66
70
" if !result.Valid() {" ,
71
+ " errors := \\ " \\" " ,
72
+ " " ,
67
73
" for _, desc := range result.Errors() {" ,
68
- " log.Printf(\\ " - % s \\\\n \\" , desc)" ,
74
+ " if errors == \\ " \\" {" ,
75
+ " errors = fmt.Sprintf(\\ " % v \\" , desc)" ,
76
+ " } else {" ,
77
+ " errors = fmt.Sprintf(\\ " % s , % v \\" , errors, desc)" ,
78
+ " }" ,
69
79
" }" ,
70
80
" " ,
71
- " log.Panic(\\ " The app - config value was not valid .\\" )" ,
81
+ " return loadedConfig, fmt.Errorf(\\ " The App Config value invalid : % s \\" , errors)" ,
82
+ " }" ,
83
+ " " ,
84
+ " return loadedConfig, nil" ,
85
+ " }" ,
86
+ " " ,
87
+ " func UnmarshalConfiguration(data []byte) (Configuration, error) {" ,
88
+ " var r Configuration" ,
89
+ " err := json.Unmarshal(data, &r)" ,
90
+ " return r, err" ,
91
+ " }" ,
92
+ " " ,
93
+ " func (r *Configuration) Marshal() ([]byte, error) {" ,
94
+ " return json.Marshal(r)" ,
95
+ " }" ,
96
+ " " ,
97
+ " type Configuration struct {" ,
98
+ " Foo *string \` json:\\ " foo ,omitempty \\" \` " ,
99
+ " }" ,
100
+ " " ,
101
+ ]
102
+ ` ;
103
+
104
+ exports[` Golang File Generation creates a simple Go file 1 ` ] = `
105
+ Array [
106
+ " // @generated by app-config" ,
107
+ " " ,
108
+ " package main" ,
109
+ " " ,
110
+ " import (" ,
111
+ " \\ " encoding / json \\" " ,
112
+ " \\ " errors \\" " ,
113
+ " \\ " fmt \\" " ,
114
+ " \\ " log \\" " ,
115
+ " \\ " os \\" " ,
116
+ " " ,
117
+ " \\ " github .com / xeipuuv / gojsonschema \\" " ,
118
+ " )" ,
119
+ " " ,
120
+ " var config Configuration" ,
121
+ " " ,
122
+ " func init() {" ,
123
+ " loadedConfig, err := LoadConfig()" ,
124
+ " " ,
125
+ " if err != nil {" ,
126
+ " log.Panic(err.Error())" ,
72
127
" }" ,
73
128
" " ,
74
129
" config = loadedConfig" ,
@@ -77,6 +132,59 @@ Array [
77
132
" func GetConfig() Configuration {" ,
78
133
" return config" ,
79
134
" }" ,
135
+ " func LoadConfig() (Configuration, error) {" ,
136
+ " var loadedConfig Configuration" ,
137
+ " var loadedSchema map[string]interface{}" ,
138
+ " var err error" ,
139
+ " " ,
140
+ " configText := os.Getenv(\\ " APP_CONFIG \\" )" ,
141
+ " schemaText := os.Getenv(\\ " APP_CONFIG_SCHEMA \\" )" ,
142
+ " " ,
143
+ " if configText == \\ " \\" {" ,
144
+ " return loadedConfig, errors.New(\\ " The APP_CONFIG environment variable was not set \\" )" ,
145
+ " }" ,
146
+ " " ,
147
+ " if schemaText == \\ " \\" {" ,
148
+ " return loadedConfig, errors.New(\\ " The APP_CONFIG_SCHEMA environment variable was not set \\" )" ,
149
+ " }" ,
150
+ " " ,
151
+ " err = json.Unmarshal([]byte(schemaText), &loadedSchema)" ,
152
+ " " ,
153
+ " if err != nil {" ,
154
+ " return loadedConfig, fmt.Errorf(\\ " Could not parse APP_CONFIG_SCHEMA environment variable : % s \\" , err.Error())" ,
155
+ " }" ,
156
+ " " ,
157
+ " err = json.Unmarshal([]byte(configText), &loadedConfig)" ,
158
+ " " ,
159
+ " if err != nil {" ,
160
+ " return loadedConfig, fmt.Errorf(\\ " Could not parse APP_CONFIG environment variable : % s \\" , err.Error())" ,
161
+ " }" ,
162
+ " " ,
163
+ " schemaLoader := gojsonschema.NewGoLoader(loadedSchema)" ,
164
+ " documentLoader := gojsonschema.NewGoLoader(loadedConfig)" ,
165
+ " " ,
166
+ " result, err := gojsonschema.Validate(schemaLoader, documentLoader)" ,
167
+ " " ,
168
+ " if err != nil {" ,
169
+ " return loadedConfig, fmt.Errorf(\\ " Could not validate App Config : % s \\" , err.Error())" ,
170
+ " }" ,
171
+ " " ,
172
+ " if !result.Valid() {" ,
173
+ " errors := \\ " \\" " ,
174
+ " " ,
175
+ " for _, desc := range result.Errors() {" ,
176
+ " if errors == \\ " \\" {" ,
177
+ " errors = fmt.Sprintf(\\ " % v \\" , desc)" ,
178
+ " } else {" ,
179
+ " errors = fmt.Sprintf(\\ " % s , % v \\" , errors, desc)" ,
180
+ " }" ,
181
+ " }" ,
182
+ " " ,
183
+ " return loadedConfig, fmt.Errorf(\\ " The App Config value invalid : % s \\" , errors)" ,
184
+ " }" ,
185
+ " " ,
186
+ " return loadedConfig, nil" ,
187
+ " }" ,
80
188
" " ,
81
189
" func UnmarshalConfiguration(data []byte) (Configuration, error) {" ,
82
190
" var r Configuration" ,
0 commit comments