@@ -38,23 +38,23 @@ class GeneToDto(
3838
3939 /* *
4040 * @param leafGene to obtain the refType if the component is defined with a name
41- * @param actionName to provide a fallback on the DTO named with the action if the component is defined inline
41+ * @param fallback to provide a fallback on the DTO named with the action if the component is defined inline
4242 *
4343 * @return the DTO name that will be used to instantiate the first variable
4444 */
45- fun getRootDtoName (leafGene : Gene , actionName : String ): String {
45+ fun getDtoName (leafGene : Gene , fallback : String ): String {
4646 return when (leafGene) {
47- is ObjectGene -> leafGene.refType? : TestWriterUtils .safeVariableName(actionName )
47+ is ObjectGene -> leafGene.refType? : StringUtils .capitalization( TestWriterUtils .safeVariableName(fallback) )
4848 is ArrayGene <* > -> {
4949 val template = leafGene.template
5050 if (template is ObjectGene ) {
51- template.refType? : TestWriterUtils .safeVariableName(actionName )
51+ template.refType? : StringUtils .capitalization( TestWriterUtils .safeVariableName(fallback) )
5252 } else {
5353 // TODO handle arrays of basic data types
54- return getListType(actionName , template)
54+ return getListType(fallback , template)
5555 }
5656 }
57- else -> throw IllegalStateException (" Gene $leafGene is not supported for DTO payloads for action: $actionName " )
57+ else -> throw IllegalStateException (" Gene $leafGene is not supported for DTO payloads for action: $fallback " )
5858 }
5959 }
6060
@@ -68,7 +68,7 @@ class GeneToDto(
6868 fun getDtoCall (gene : Gene , dtoName : String , counter : Int ): DtoCall {
6969 return when (gene) {
7070 is ObjectGene -> getObjectDtoCall(gene, dtoName, counter)
71- is ArrayGene <* > -> getArrayDtoCall(gene, dtoName, counter)
71+ is ArrayGene <* > -> getArrayDtoCall(gene, dtoName, counter, null )
7272 else -> throw RuntimeException (" BUG: Gene $gene (with type ${this ::class .java.simpleName} ) should not be creating DTOs" )
7373 }
7474 }
@@ -88,36 +88,36 @@ class GeneToDto(
8888 val attributeName = it.name
8989 when (leafGene) {
9090 is ObjectGene -> {
91- val childDtoCall = getDtoCall(leafGene, StringUtils .capitalization( attributeName), counter)
91+ val childDtoCall = getDtoCall(leafGene, getDtoName(leafGene, attributeName), counter)
9292
9393 result.addAll(childDtoCall.objectCalls)
9494 result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, childDtoCall.varName))
9595 }
9696 is ArrayGene <* > -> {
97- val childDtoCall = getDtoCall (leafGene, StringUtils .capitalization( attributeName), counter)
97+ val childDtoCall = getArrayDtoCall (leafGene, getDtoName(leafGene, attributeName), counter, attributeName )
9898
9999 result.addAll(childDtoCall.objectCalls)
100100 result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, childDtoCall.varName))
101101 }
102102 else -> {
103- result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, it .getValueAsPrintableString(targetFormat = null )))
103+ result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, " ${leafGene .getValueAsPrintableString(targetFormat = null )}${getValueSuffix(leafGene)} " ))
104104 }
105105 }
106106 }
107107
108108 return DtoCall (dtoVarName, result)
109109 }
110110
111- private fun getArrayDtoCall (gene : ArrayGene <* >, dtoName : String , counter : Int ): DtoCall {
111+ private fun getArrayDtoCall (gene : ArrayGene <* >, dtoName : String , counter : Int , targetAttribute : String? ): DtoCall {
112112 val result = mutableListOf<String >()
113113 val template = gene.template
114114
115115 val listType = getListType(dtoName,template)
116- val listVarName = " list_${listType } _${counter} "
116+ val listVarName = " list_${targetAttribute ? : dtoName } _${counter} "
117117 result.add(dtoOutput.getNewListStatement(listType, listVarName))
118118
119119 if (template is ObjectGene ) {
120- val childDtoName = StringUtils .capitalization( template.refType? : gene.name)
120+ val childDtoName = template.refType? : StringUtils .capitalization( gene.name)
121121 var listCounter = 1
122122 gene.getViewOfElements().forEach {
123123 val childDtoCall = getDtoCall(it,childDtoName, listCounter++ )
@@ -126,7 +126,8 @@ class GeneToDto(
126126 }
127127 } else {
128128 gene.getViewOfElements().forEach {
129- result.add(dtoOutput.getAddElementToListStatement(listVarName, it.getValueAsPrintableString(targetFormat = null )))
129+ val leafGene = it.getLeafGene()
130+ result.add(dtoOutput.getAddElementToListStatement(listVarName, " ${leafGene.getValueAsPrintableString(targetFormat = null )}${getValueSuffix(leafGene)} " ))
130131 }
131132 }
132133
@@ -152,4 +153,14 @@ class GeneToDto(
152153 else -> throw Exception (" Not supported gene at the moment: ${gene?.javaClass?.simpleName} for field $fieldName " )
153154 }
154155 }
156+
157+ // According to documentation, a trailing constant is only needed for Long, Hexadecimal and Float
158+ // https://kotlinlang.org/docs/numbers.html#literal-constants-for-numbers
159+ private fun getValueSuffix (gene : Gene ): String {
160+ return when (gene) {
161+ is LongGene -> " L"
162+ is FloatGene -> " f"
163+ else -> " "
164+ }
165+ }
155166}
0 commit comments