Skip to content

Commit

Permalink
refactor: Moved the externalTypes into the options.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdutz committed Oct 16, 2024
1 parent 5e12a8e commit cc20d07
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public abstract class FreemarkerLanguageOutput implements LanguageOutput {
@Override
public void generate(File outputDir, String version, String languageName, String protocolName, String outputFlavor,
Map<String, TypeDefinition> types,
Map<String, String> externalTypes,
Map<String, String> options)
Map<String, Object> options)
throws GenerationException {

// Configure the Freemarker template engine
Expand Down Expand Up @@ -75,7 +74,7 @@ public void generate(File outputDir, String version, String languageName, String
typeContext.put("languageName", languageName);
typeContext.put("protocolName", protocolName);
typeContext.put("outputFlavor", outputFlavor);
typeContext.put("helper", getHelper(null, protocolName, outputFlavor, types, externalTypes, options));
typeContext.put("helper", getHelper(null, protocolName, outputFlavor, types, options));
typeContext.put("tracer", Tracer.start("global"));
typeContext.putAll(options);

Expand All @@ -97,7 +96,7 @@ public void generate(File outputDir, String version, String languageName, String
typeContext.put("outputFlavor", outputFlavor);
typeContext.put("typeName", typeEntry.getKey());
typeContext.put("type", typeEntry.getValue());
typeContext.put("helper", getHelper(typeEntry.getValue(), protocolName, outputFlavor, types, externalTypes, options));
typeContext.put("helper", getHelper(typeEntry.getValue(), protocolName, outputFlavor, types, options));
typeContext.put("tracer", Tracer.start("types"));

// Depending on the type, get the corresponding list of templates.
Expand Down Expand Up @@ -129,7 +128,7 @@ public void generate(File outputDir, String version, String languageName, String
typeContext.put("languageName", languageName);
typeContext.put("protocolName", protocolName);
typeContext.put("outputFlavor", outputFlavor);
typeContext.put("helper", getHelper(null, protocolName, outputFlavor, types, externalTypes, options));
typeContext.put("helper", getHelper(null, protocolName, outputFlavor, types, options));
typeContext.putAll(options);

for (Template template : miscTemplateList) {
Expand Down Expand Up @@ -213,7 +212,7 @@ protected List<Template> getMiscTemplates(Configuration freemarkerConfiguration)
return Collections.emptyList();
}

protected abstract FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> externalTypes, Map<String, String> options);
protected abstract FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName,
Map<String, TypeDefinition> types, Map<String, Object> options);

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ protected List<Template> getDataIoTemplates(Configuration freemarkerConfiguratio
@Override
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName,
Map<String, TypeDefinition> types,
Map<String, String> externalTypes,
Map<String, String> options) {
Map<String, Object> options) {
return new CLanguageTemplateHelper(thisType, protocolName, flavorName, types);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ protected List<Template> getDataIoTemplates(Configuration freemarkerConfiguratio
}

@Override
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types, Map<String, String> externalTypes, Map<String, String> options) {
return new CsLanguageTemplateHelper(thisType, protocolName, flavorName, types, externalTypes, options);
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types, Map<String, Object> options) {
return new CsLanguageTemplateHelper(thisType, protocolName, flavorName, types, options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ Licensed to the Apache Software Foundation (ASF) under one
@SuppressWarnings({"unused", "WeakerAccess"})
public class CsLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelper {

private final Map<String, String> options;
private final Map<String, Object> options;

public CsLanguageTemplateHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> externalTypes, Map<String, String> options) {
Map<String, Object> options) {
super(thisType, protocolName, flavorName, types);
this.options = options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public String getName() {

@Override
public Set<String> supportedOptions() {
return Collections.emptySet();
return new HashSet<>(List.of(
// Map containing the type-names for external types.
"externalTypes"));
}

@Override
Expand Down Expand Up @@ -76,8 +78,8 @@ protected List<Template> getMiscTemplates(Configuration freemarkerConfiguration)

@Override
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> externalTypes, Map<String, String> options) {
return new GoLanguageTemplateHelper(thisType, protocolName, flavorName, types, externalTypes, options);
Map<String, Object> options) {
return new GoLanguageTemplateHelper(thisType, protocolName, flavorName, types, options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public class GoLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelp

private static final Logger LOGGER = LoggerFactory.getLogger(GoLanguageTemplateHelper.class);

private final Map<String, String> options;
private final Map<String, String> externalTypes;
private final Map<String, Object> options;

// TODO: we could condense it to one import set as these can be emitted per template and are not hardcoded anymore

Expand All @@ -55,10 +54,9 @@ public class GoLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelp
public final SortedSet<String> requiredImportsForDataIo = new TreeSet<>();

public GoLanguageTemplateHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> externalTypes, Map<String, String> options) {
Map<String, Object> options) {
super(thisType, protocolName, flavorName, types);
this.options = options;
this.externalTypes = externalTypes;
}

public String fileName(String protocolName, String languageName, String languageFlavorName) {
Expand Down Expand Up @@ -113,15 +111,21 @@ public boolean isComplex(Field field) {

@Override
public String getLanguageTypeNameForTypeReference(TypeReference typeReference) {
if(externalTypes != null) {
if(options.containsKey("externalTypes")) {
Object externalTypes = options.get("externalTypes");
if(!(externalTypes instanceof Map)) {
throw new IllegalArgumentException("The option 'externalTypes' is not a Map");
}
Map<String, Object> externalTypesMap = (Map<String, Object>) externalTypes;

String typeName = null;
if(typeReference.isComplexTypeReference()) {
typeName = typeReference.asComplexTypeReference().orElseThrow().getName();
} else if(typeReference.isEnumTypeReference()) {
typeName = typeReference.asEnumTypeReference().orElseThrow().getName();
}
if((typeName != null) && externalTypes.containsKey(typeName)) {
String replacement = externalTypes.get(typeName);
if((typeName != null) && externalTypesMap.containsKey(typeName)) {
String replacement = externalTypesMap.get(typeName).toString();
String namespaceAlias;
if(replacement.contains(" ")) {
namespaceAlias = replacement.split(" ")[0];
Expand Down Expand Up @@ -1927,10 +1931,4 @@ public boolean isGeneratePropertiesForReservedFields() {
return options.getOrDefault("generate-properties-for-reserved-fields", "false").equals("true");
}

public String getExternalTypeImports() {
StringBuilder imports = new StringBuilder();
externalTypes.forEach((mspecTypeName, javaTypeName) -> imports.append("import ").append(javaTypeName).append(";\n"));
return imports.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Set<String> supportedOptions() {
// Generates additional properties to save values of reserved fields for the case that the value differs from the expected value.
"generate-properties-for-reserved-fields",
// Map containing the type-names for external types.
"external"));
"externalTypes"));
}

@Override
Expand Down Expand Up @@ -86,9 +86,9 @@ protected List<Template> getDataIoTemplates(Configuration freemarkerConfiguratio
}

@Override
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> externalTypes, Map<String, String> options) {
return new JavaLanguageTemplateHelper(thisType, protocolName, flavorName, types, externalTypes, options);
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName,
Map<String, TypeDefinition> types, Map<String, Object> options) {
return new JavaLanguageTemplateHelper(thisType, protocolName, flavorName, types, options);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@
@SuppressWarnings({"unused", "WeakerAccess"})
public class JavaLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelper {

private final Map<String, String> options;
private final Map<String, String> externalTypes;
private final Map<String, Object> options;

public JavaLanguageTemplateHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> externalTypes, Map<String, String> options) {
Map<String, Object> options) {
super(thisType, protocolName, flavorName, types);
this.externalTypes = externalTypes;
this.options = options;
}

Expand Down Expand Up @@ -1396,7 +1394,20 @@ public boolean isGeneratePropertiesForReservedFields() {

public String getExternalTypeImports() {
StringBuilder imports = new StringBuilder();
externalTypes.forEach((mspecTypeName, javaTypeName) -> imports.append("import ").append(javaTypeName).append(";\n"));
if(options.containsKey("externalTypes")) {
Object externalTypes = options.get("externalTypes");
if(externalTypes instanceof Map) {
Map<String, Object> externalTypesMap = (Map<String, Object>) externalTypes;
for (String mspecTypeName : externalTypesMap.keySet()) {
Object obj = externalTypesMap.get(mspecTypeName);
if(obj instanceof String) {
imports.append("import ").append(obj).append(";\n");
} else {
throw new IllegalArgumentException("Type definition for " + mspecTypeName + " is invalid");
}
}
}
}
return imports.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected List<Template> getDataIoTemplates(Configuration freemarkerConfiguratio
*/
@Override
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> externalTypes, Map<String, String> options) {
Map<String, Object> options) {
return new PythonLanguageTemplateHelper(thisType, protocolName, flavorName, types);
}

Expand Down
8 changes: 5 additions & 3 deletions plc4go/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,11 @@
<languageName>go</languageName>
<outputFlavor>read-write</outputFlavor>
<outputDir>${project.basedir}/protocols</outputDir>
<externalTypes>
<PlcValueType>api "github.com/apache/plc4x/plc4go/pkg/api/values"</PlcValueType>
</externalTypes>
<options>
<externalTypes>
<PlcValueType>api "github.com/apache/plc4x/plc4go/pkg/api/values"</PlcValueType>
</externalTypes>
</options>
</configuration>
</execution>
<execution>
Expand Down
8 changes: 5 additions & 3 deletions plc4j/drivers/ads/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@
<languageName>java</languageName>
<outputFlavor>read-write</outputFlavor>
<outputDir>src/main/generated</outputDir>
<externalTypes>
<PlcValueType>org.apache.plc4x.java.api.types.PlcValueType</PlcValueType>
</externalTypes>
<options>
<externalTypes>
<PlcValueType>org.apache.plc4x.java.api.types.PlcValueType</PlcValueType>
</externalTypes>
</options>
</configuration>
</execution>
<execution>
Expand Down

0 comments on commit cc20d07

Please sign in to comment.