Skip to content

Commit 08937d1

Browse files
committed
Fix comma insertion processing nested Entities
1 parent 4c672bc commit 08937d1

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/sql/databricks/AspectModelDatabricksDenormalizedSqlVisitor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,12 @@ public String visitStructureElement( final StructureElement structureElement, fi
142142
final StringBuilder result = new StringBuilder();
143143
final Consumer<String> appendLine = line -> {
144144
if ( !line.isBlank() ) {
145-
if ( !result.isEmpty() ) {
145+
// Turning entire result into String for checking its contents can be ineffective,
146+
// so we pick up the minimum substring here though it's verbose a bit.
147+
if ( !result.isEmpty() && !result.substring( result.length() < 4 ? 0 : result.length() - 4 ).stripTrailing().endsWith( "," ) ) {
146148
result.append( ",\n" );
147149
}
148-
if ( !line.startsWith( " " ) ) {
150+
if ( !line.startsWith( " " ) && ( result.length() < 2 || !result.substring( result.length() - 2 ).equals( " " ) ) ) {
149151
result.append( " " );
150152
}
151153
result.append( line );
@@ -168,7 +170,10 @@ public String visitStructureElement( final StructureElement structureElement, fi
168170

169171
@Override
170172
public String visitAspect( final Aspect aspect, final Context context ) {
171-
final String columnDeclarations = visitStructureElement( aspect, context );
173+
String columnDeclarations = visitStructureElement( aspect, context );
174+
if ( columnDeclarations.endsWith( ",\n " ) ) {
175+
columnDeclarations = columnDeclarations.substring( 0, columnDeclarations.length() - 4 );
176+
}
172177
final String comment = config.includeTableComment()
173178
? Optional.ofNullable( aspect.getDescription( config.commentLanguage() ) ).map( description ->
174179
new DatabricksCommentDefinition( description ) + "\n" ).orElse( "" )
@@ -309,10 +314,6 @@ private String processComplexType( final ComplexType entity, final Context conte
309314
}
310315
} );
311316

312-
if ( !columns.isEmpty() && columns.toString().endsWith( ",\n " ) ) {
313-
columns.setLength( columns.length() - 4 ); // Remove last ",\n "
314-
}
315-
316317
return columns.toString();
317318
}
318319

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/sql/databricks/AspectModelDatabricksDenormalizedSqlVisitorTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,16 @@ CREATE TABLE IF NOT EXISTS aspect_with_property_with_payload_name (
533533
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithPropertyWithPayloadName');
534534
""" );
535535
}
536+
537+
@Test
538+
void testAspectWithUpwardTransitionInNestedEntity() {
539+
assertThat( sql( TestAspect.ASPECT_WITH_UPWARD_TRANSITION_IN_NESTED_ENTITY ) ).isEqualTo( """
540+
CREATE TABLE IF NOT EXISTS aspect_with_upward_transition_in_nested_entity (
541+
first_level_property__second_level_property1__third_level_property_id BIGINT NOT NULL,
542+
first_level_property__second_level_property1__third_level_property STRING NOT NULL,
543+
first_level_property__second_level_property2 STRING NOT NULL
544+
)
545+
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithUpwardTransitionInNestedEntity');
546+
""" );
547+
}
536548
}

core/esmf-test-aspect-models/src/main/java/org/eclipse/esmf/test/TestAspect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public enum TestAspect implements TestModel {
192192
ASPECT_WITH_TWO_STRUCTURED_VALUES_AND_TRAIT,
193193
ASPECT_WITH_UMLAUT_DESCRIPTION,
194194
ASPECT_WITH_UNIT,
195+
ASPECT_WITH_UPWARD_TRANSITION_IN_NESTED_ENTITY,
195196
ASPECT_WITH_USED_AND_UNUSED_CHARACTERISTIC,
196197
ASPECT_WITH_USED_AND_UNUSED_COLLECTION,
197198
ASPECT_WITH_USED_AND_UNUSED_CONSTRAINT,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2025 Contributors to the Eclipse Foundation
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
13+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.2.0#> .
14+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.2.0#> .
15+
16+
:AspectWithUpwardTransitionInNestedEntity a samm:Aspect ;
17+
samm:properties ( :firstLevelProperty ) ;
18+
samm:operations ( ) .
19+
20+
:firstLevelProperty a samm:Property ;
21+
samm:characteristic :FirstLevelCharacteristics .
22+
23+
:FirstLevelCharacteristics a samm-c:List ;
24+
samm:dataType :FirstLevelEntity .
25+
26+
:FirstLevelEntity a samm:Entity ;
27+
samm:properties ( :secondLevelProperty1 :secondLevelProperty2 ) .
28+
29+
:secondLevelProperty1 a samm:Property ;
30+
samm:characteristic :SecondLevelCharacteristics .
31+
32+
:secondLevelProperty2 a samm:Property ;
33+
samm:characteristic samm-c:Text .
34+
35+
:SecondLevelCharacteristics a samm:Characteristic ;
36+
samm:dataType :SecondLevelEntity .
37+
38+
:SecondLevelEntity a samm:Entity ;
39+
samm:properties ( :thirdLevelProperty ) .
40+
41+
:thirdLevelProperty a samm:Property ;
42+
samm:characteristic samm-c:Text .

0 commit comments

Comments
 (0)