-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigFactory.cfc
198 lines (163 loc) · 6.06 KB
/
ConfigFactory.cfc
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @accessors true
*/
component {
property javaLoader;
public ConfigFactory function init() {
variables.configs = [];
variables.merges = [];
variables.javaLoader = new vendor.javaloader.JavaLoader([
getDirectoryFromPath(getCurrentTemplatePath()) & '/vendor/typesafe-config-1.2.1.jar'
]);
return this;
}
public ConfigFactory function clear() {
variables.configs = [];
return this;
}
public ConfigFactory function setMerges(required array merges) {
variables.merges = merges;
return this;
}
public ConfigFactory function addFile(required string file) {
arrayAppend(variables.configs, { type = 'file', value = file });
return this;
}
public ConfigFactory function addFiles(required array files) {
for( var i=1; i <= arrayLen(files); i++ ) {
addFile(files[i]);
}
return this;
}
public ConfigFactory function addString(required string str) {
arrayAppend(variables.configs, { type = 'string', value = str });
return this;
}
public ConfigFactory function addStrings(required array strs) {
for( var i=1; i <= arrayLen(files); i++ ) {
addString(strs[i]);
}
return this;
}
public ConfigFactory function addUrl(required string str) {
arrayAppend(variables.configs, { type = 'url', value = str });
return this;
}
public ConfigFactory function addUrls(required array urls) {
for( var i=1; i <= arrayLen(urls); i++ ) {
addUrl(urls[i]);
}
return this;
}
public ConfigFactory function addStruct(required struct data) {
arrayAppend(variables.configs, { type = 'struct', value = data });
return this;
}
public lib.Config function resolve() {
var configFactory = getConfigFactory();
var configContainers = [];
for( var i=1; i <= arrayLen(variables.configs); i++ ) {
var _config = variables.configs[i];
switch( _config.type ) {
case "file":
arrayAppend(configContainers, configFactory.parseFile(
createObject('java', 'java.io.File').init(_config.value)
));
break;
case "string":
arrayAppend(configContainers, configFactory.parseReader(
createObject('java', 'java.io.StringReader').init(_config.value)
));
break;
case "url":
arrayAppend(configContainers, configFactory.parseURL(
createObject('java', 'java.net.URL').init(_config.value)
));
break;
case "struct":
arrayAppend(configContainers, configFactory.parseMap(sanitizeKeys(_config.value)));
break;
}
}
var finalConfig = "";
for( var i=arrayLen(configContainers); i >= 1; i-- ) {
var _config = configContainers[i];
if( !isObject(finalConfig) ) {
finalConfig = _config;
} else {
finalConfig = finalConfig.withFallback(_config);
}
}
// No config contains found, just result a empty config
if (!isObject(finalConfig)) {
return new lib.Config({});
}
finalConfig = finalConfig.resolve();
if( !arrayLen(variables.merges) ) {
return new lib.Config(finalConfig.root());
}
var initialConfig = finalConfig;
var finalConfig = {};
for( var i=1; i <= arrayLen(variables.merges); i++ ) {
var merge = merges[i];
var node = initialConfig.withOnlyPath(merge).root()[merge];
if( isStruct(node) ) structMerge(finalConfig, node);
}
return new lib.Config(finalConfig);
}
private struct function structMerge(required struct struct1, required struct struct2) {
var structs = [];
var i_arg = "";
for( i_arg in arguments ) {
if( isStruct(arguments[i_arg]) ) arrayAppend(structs, arguments[i_arg]);
}
if( !arrayLen(structs) ) return {};
for( var i_struct=2; i_struct <= arrayLen(structs); i_struct++ ) {
struct2 = structs[i_struct];
var keyList = listToArray(structKeyList(struct1) & "," & structKeyList(struct2));
for( var i=1; i <= arrayLen(keyList); i++ ) {
var key = keyList[i];
if( structKeyExists(struct1, key) && structKeyExists(struct2, key)
&& isStruct(struct1[key]) && isStruct(struct2[key]) ) {
struct1[key] = structMerge({}, struct1[key], struct2[key]);
} else {
if( structKeyExists(struct2, key) ) {
struct1[key] = struct2[key];
}
}
}
}
return struct1;
}
private any function getConfigFactory() {
if( !structKeyExists(variables, 'configFactory') ) {
variables.configFactory = javaLoader.create('com.typesafe.config.ConfigFactory');
}
return variables.configFactory;
}
private any function sanitizeKeys(required any _values) {
if (isStruct(_values)) {
var values = {};
for (var key in _values) {
if (findNoCase(':', key)) {
continue;
}
var value = _values[key];
if (isSimpleValue(value)) {
values[key] = value;
} else if (isArray(value) || isStruct(value)) {
values[key] = sanitizeKeys(value);
}
}
return values;
}
if (isArray(_values)) {
var values = [];
for (var i = 1; i < arrayLen(_values); i++) {
values[i] = sanitizeKeys(_values[i]);
}
return values;
}
return _values;
}
}