|
1 | 1 | const fs = require('fs') |
| 2 | +const path = require('path') |
2 | 3 |
|
3 | 4 | /** Class representing the sling model generator */ |
4 | 5 | class Generator { |
@@ -90,7 +91,12 @@ function genNames(properties) { |
90 | 91 | } |
91 | 92 | if(props['x-form-type'] === 'collection') { |
92 | 93 | if (Object.keys(props['properties']).length > 1 || props['x-form-multifield'] === "true" || props['x-form-multifield'] === true) { |
93 | | - inject += '\tprivate List<IComponent> ' |
| 94 | + var collectionType = props['x-collection-type'] |
| 95 | + if (collectionType) { |
| 96 | + inject += `\tprivate List<${collectionType}Model> ` |
| 97 | + } else { |
| 98 | + inject += '\tprivate List<IComponent> ' |
| 99 | + } |
94 | 100 | } else { |
95 | 101 | inject += '\tprivate String[] ' |
96 | 102 | } |
@@ -137,7 +143,12 @@ function genGetters(properties, customGetters) { |
137 | 143 | inject += `\t/* ${JSON.stringify(props)} */\n` |
138 | 144 | if(props['x-form-type'] === 'collection') { |
139 | 145 | if (Object.keys(props['properties']).length > 1 || props['x-form-multifield'] === "true" || props['x-form-multifield'] === true) { |
140 | | - inject += '\tpublic List<IComponent> get' |
| 146 | + var collectionType = props['x-collection-type'] |
| 147 | + if (collectionType) { |
| 148 | + inject += `\tpublic List<${collectionType}Model> get` |
| 149 | + } else { |
| 150 | + inject += '\tpublic List<IComponent> get' |
| 151 | + } |
141 | 152 | } else { |
142 | 153 | inject += '\tpublic String[] get' |
143 | 154 | } |
@@ -170,16 +181,40 @@ function genGettersAnnotates(ownerName, annotate) { |
170 | 181 | return result |
171 | 182 | } |
172 | 183 |
|
| 184 | +function genTypes(properties, generator) { |
| 185 | + for(var propName in properties) { |
| 186 | + if (propName !== 'children') { |
| 187 | + var props = properties[propName] |
| 188 | + if(props['x-source'] === 'inject') { |
| 189 | + if(props['x-form-type'] === 'collection') { |
| 190 | + if (Object.keys(props['properties']).length > 1 || props['x-form-multifield'] === "true" || props['x-form-multifield'] === true) { |
| 191 | + var collectionType = props['x-collection-type'] |
| 192 | + if (collectionType) { |
| 193 | + gen(new Generator(generator.src, path.join(path.dirname(generator.dst), `${collectionType}Model.java`), { |
| 194 | + ...generator.data, |
| 195 | + modelName: collectionType, |
| 196 | + definitions: { |
| 197 | + [collectionType]: props |
| 198 | + } |
| 199 | + })) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | +} |
| 208 | + |
173 | 209 | /** |
174 | 210 | * Loads in the sling model template and fills in the data, inject, and getters content to make the components model |
175 | 211 | * @param {string} generator - the generator object |
176 | 212 | */ |
177 | 213 | function gen(generator) { |
178 | | - |
179 | 214 | // load the template |
180 | 215 | var srcData = fs.readFileSync(generator.src).toString() |
181 | 216 |
|
182 | | - // now substitute all the values we know into the template |
| 217 | + // substitute all the values we know into the template |
183 | 218 | srcData = makeInitial(srcData, generator) |
184 | 219 |
|
185 | 220 | // if the generated file does not exist yet, create it with the template we just created |
@@ -242,6 +277,8 @@ function gen(generator) { |
242 | 277 | } catch(error) { |
243 | 278 | fs.writeFileSync(generator.dst, dstCurrent) |
244 | 279 | } |
| 280 | + |
| 281 | + genTypes(properties, generator) |
245 | 282 | } |
246 | 283 |
|
247 | 284 | module.exports = { |
|
0 commit comments