7
7
import java .util .HashMap ;
8
8
import java .util .List ;
9
9
import java .util .Map ;
10
+ import java .util .Optional ;
10
11
import java .util .stream .Stream ;
11
12
12
13
import org .eclipse .microprofile .config .Config ;
24
25
public abstract class JVMCodeGenerator implements CodeGenProvider {
25
26
26
27
public static final String PACKAGE_PREFIX = "ftl." ;
28
+ public static final String TYPE_MAPPER = "TypeAliasMapper" ;
27
29
28
30
@ Override
29
31
public String providerId () {
@@ -42,6 +44,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
42
44
}
43
45
List <Module > modules = new ArrayList <>();
44
46
Map <DeclRef , Type > typeAliasMap = new HashMap <>();
47
+ Map <DeclRef , String > nativeTypeAliasMap = new HashMap <>();
45
48
try (Stream <Path > pathStream = Files .list (context .inputDir ())) {
46
49
for (var file : pathStream .toList ()) {
47
50
String fileName = file .getFileName ().toString ();
@@ -50,9 +53,30 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
50
53
}
51
54
var module = Module .parseFrom (Files .readAllBytes (file ));
52
55
for (var decl : module .getDeclsList ()) {
56
+ String packageName = PACKAGE_PREFIX + module .getName ();
53
57
if (decl .hasTypeAlias ()) {
54
58
var data = decl .getTypeAlias ();
55
- typeAliasMap .put (new DeclRef (module .getName (), data .getName ()), data .getType ());
59
+ boolean handled = false ;
60
+ for (var md : data .getMetadataList ()) {
61
+ if (md .hasTypeMap ()) {
62
+ String runtime = md .getTypeMap ().getRuntime ();
63
+ if (runtime .equals ("kotlin" ) || runtime .equals ("java" )) {
64
+ nativeTypeAliasMap .put (new DeclRef (module .getName (), data .getName ()),
65
+ md .getTypeMap ().getNativeName ());
66
+ generateTypeAliasMapper (module .getName (), data .getName (), packageName ,
67
+ Optional .of (md .getTypeMap ().getNativeName ()),
68
+ context .outDir ());
69
+ handled = true ;
70
+ break ;
71
+ }
72
+ }
73
+ }
74
+ if (!handled ) {
75
+ generateTypeAliasMapper (module .getName (), data .getName (), packageName , Optional .empty (),
76
+ context .outDir ());
77
+ typeAliasMap .put (new DeclRef (module .getName (), data .getName ()), data .getType ());
78
+ }
79
+
56
80
}
57
81
}
58
82
modules .add (module );
@@ -69,26 +93,27 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
69
93
if (!verb .getExport ()) {
70
94
continue ;
71
95
}
72
- generateVerb (module , verb , packageName , typeAliasMap , context .outDir ());
96
+ generateVerb (module , verb , packageName , typeAliasMap , nativeTypeAliasMap , context .outDir ());
73
97
} else if (decl .hasData ()) {
74
98
var data = decl .getData ();
75
99
if (!data .getExport ()) {
76
100
continue ;
77
101
}
78
- generateDataObject (module , data , packageName , typeAliasMap , context .outDir ());
102
+ generateDataObject (module , data , packageName , typeAliasMap , nativeTypeAliasMap , context .outDir ());
79
103
80
104
} else if (decl .hasEnum ()) {
81
105
var data = decl .getEnum ();
82
106
if (!data .getExport ()) {
83
107
continue ;
84
108
}
85
- generateEnum (module , data , packageName , typeAliasMap , context .outDir ());
109
+ generateEnum (module , data , packageName , typeAliasMap , nativeTypeAliasMap , context .outDir ());
86
110
} else if (decl .hasTopic ()) {
87
111
var data = decl .getTopic ();
88
112
if (!data .getExport ()) {
89
113
continue ;
90
114
}
91
- generateTopicSubscription (module , data , packageName , typeAliasMap , context .outDir ());
115
+ generateTopicSubscription (module , data , packageName , typeAliasMap , nativeTypeAliasMap ,
116
+ context .outDir ());
92
117
}
93
118
}
94
119
}
@@ -99,17 +124,20 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
99
124
return true ;
100
125
}
101
126
127
+ protected abstract void generateTypeAliasMapper (String module , String name , String packageName ,
128
+ Optional <String > nativeTypeAlias , Path outputDir ) throws IOException ;
129
+
102
130
protected abstract void generateTopicSubscription (Module module , Topic data , String packageName ,
103
- Map <DeclRef , Type > typeAliasMap , Path outputDir ) throws IOException ;
131
+ Map <DeclRef , Type > typeAliasMap , Map < DeclRef , String > nativeTypeAliasMap , Path outputDir ) throws IOException ;
104
132
105
133
protected abstract void generateEnum (Module module , Enum data , String packageName , Map <DeclRef , Type > typeAliasMap ,
106
- Path outputDir ) throws IOException ;
134
+ Map < DeclRef , String > nativeTypeAliasMap , Path outputDir ) throws IOException ;
107
135
108
136
protected abstract void generateDataObject (Module module , Data data , String packageName , Map <DeclRef , Type > typeAliasMap ,
109
- Path outputDir ) throws IOException ;
137
+ Map < DeclRef , String > nativeTypeAliasMap , Path outputDir ) throws IOException ;
110
138
111
139
protected abstract void generateVerb (Module module , Verb verb , String packageName , Map <DeclRef , Type > typeAliasMap ,
112
- Path outputDir ) throws IOException ;
140
+ Map < DeclRef , String > nativeTypeAliasMap , Path outputDir ) throws IOException ;
113
141
114
142
@ Override
115
143
public boolean shouldRun (Path sourceDir , Config config ) {
0 commit comments