Skip to content

Commit 52cba66

Browse files
author
reggie_7
committed
Generate additional types for collections with x-collection-type property
1 parent 8728482 commit 52cba66

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

lib/generator.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs')
2+
const path = require('path')
23

34
/** Class representing the sling model generator */
45
class Generator {
@@ -90,7 +91,12 @@ function genNames(properties) {
9091
}
9192
if(props['x-form-type'] === 'collection') {
9293
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+
}
94100
} else {
95101
inject += '\tprivate String[] '
96102
}
@@ -137,7 +143,12 @@ function genGetters(properties, customGetters) {
137143
inject += `\t/* ${JSON.stringify(props)} */\n`
138144
if(props['x-form-type'] === 'collection') {
139145
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+
}
141152
} else {
142153
inject += '\tpublic String[] get'
143154
}
@@ -170,16 +181,40 @@ function genGettersAnnotates(ownerName, annotate) {
170181
return result
171182
}
172183

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+
173209
/**
174210
* Loads in the sling model template and fills in the data, inject, and getters content to make the components model
175211
* @param {string} generator - the generator object
176212
*/
177213
function gen(generator) {
178-
179214
// load the template
180215
var srcData = fs.readFileSync(generator.src).toString()
181216

182-
// now substitute all the values we know into the template
217+
// substitute all the values we know into the template
183218
srcData = makeInitial(srcData, generator)
184219

185220
// if the generated file does not exist yet, create it with the template we just created
@@ -242,6 +277,8 @@ function gen(generator) {
242277
} catch(error) {
243278
fs.writeFileSync(generator.dst, dstCurrent)
244279
}
280+
281+
genTypes(properties, generator)
245282
}
246283

247284
module.exports = {

0 commit comments

Comments
 (0)