Skip to content

Commit

Permalink
feat(codegen): Fixed go code generation after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdutz committed Feb 24, 2022
1 parent 7c59bc3 commit 749e034
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public String getLanguageTypeNameForTypeReference(TypeReference typeReference) {
// TODO: shouldn't this be an error case
return "";
}
if (typeReference instanceof ArrayTypeReference) {
final ArrayTypeReference arrayTypeReference = (ArrayTypeReference) typeReference;
return "[]" + getLanguageTypeNameForTypeReference(arrayTypeReference.getElementTypeReference());
}
if (!(typeReference instanceof SimpleTypeReference)) {
return ((NonSimpleTypeReference) typeReference).getName();
}
Expand Down Expand Up @@ -244,7 +248,7 @@ public String getNullValueForTypeReference(TypeReference typeReference) {
case VSTRING:
return "\"\"";
}
} else if (typeReference instanceof ComplexTypeReference && isEnumTypeReference(typeReference)) {
} else if (typeReference.isEnumTypeReference()) {
return "0";
}
return "nil";
Expand Down Expand Up @@ -273,7 +277,7 @@ public int getNumBits(SimpleTypeReference simpleTypeReference) {
}

public boolean needsPointerAccess(PropertyField field) {
return "optional".equals(field.getTypeName()) || (field.getType().isComplexTypeReference() && !isEnumField(field));
return "optional".equals(field.getTypeName()) || (field.getType().isNonSimpleTypeReference() && !isEnumField(field));
}

public String getSpecialReadBufferReadMethodCall(String logicalName, SimpleTypeReference simpleTypeReference, TypedField field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ import (
func ${type.name}Parse(readBuffer utils.ReadBuffer<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name} <#if parserArgument.type.isComplexTypeReference() && !helper.isEnumTypeReference(parserArgument.type)>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>, </#sep></#list></#if>) (api.PlcValue, error) {
readBuffer.PullContext("${type.name}")
switch {
<#list type.switchField.cases as case>
<#list type.switchField.orElseThrow().cases as case>
<@compress single_line=true>
<#if case.discriminatorValueTerms?has_content>
case
<#list case.discriminatorValueTerms as discriminatorValueTerm>
${helper.toParseExpression(null, null, type.switchField.discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} ==
<#if parserArguments[discriminatorValueTerm?index].type.isComplexTypeReference()>
${helper.toParseExpression(null, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} ==
<#if parserArguments[discriminatorValueTerm?index].type.isNonSimpleTypeReference()>
<#if helper.isEnumTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#assign enumType=helper.getLanguageTypeNameForTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#assign enumTypeWithoutTraces=tracer.removeTraces(enumType)>
Expand Down Expand Up @@ -109,29 +109,30 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer<#if parserArguments?has_conte
<#switch field.typeName>
<#case "array">
<#assign arrayField = field.asArrayField().orElseThrow()>
<#assign arrayElementType = arrayField.type.elementTypeReference>

// Array Field (${arrayField.name})
var ${arrayField.name} []api.PlcValue
for i := 0; i < int(${helper.toParseExpression(null, null, arrayField.loopExpression, parserArguments)}); i++ {
_item, _itemErr := <#if arrayField.type.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(arrayField.name, arrayField.type.asSimpleTypeReference().orElseThrow(), arrayField)}<#else>Complex type array in data-io parsing currently not implemented</#if>
_item, _itemErr := <#if arrayElementType.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(arrayField.name, arrayElementType.asSimpleTypeReference().orElseThrow(), arrayField)}<#else>Complex type array in data-io parsing currently not implemented</#if>
if _itemErr != nil {
return nil, errors.Wrap(_itemErr, "Error parsing 'value' field")<@emitImport import="github.com/pkg/errors" />
}
${arrayField.name} = append(${arrayField.name}, ${helper.getPlcValueTypeForTypeReference(arrayField.type)}(_item))
${arrayField.name} = append(${arrayField.name}, ${helper.getPlcValueTypeForTypeReference(arrayElementType)}(_item))
}
<#if arrayField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
<#case "manual">
<#assign arrayField = field.asManualField().orElseThrow()>
<#assign manualField = field.asManualField().orElseThrow()>

// Manual Field (${arrayField.name})
${arrayField.name}, _${arrayField.name}Err := ${helper.toParseExpression(arrayField, arrayField.type, arrayField.parseExpression, parserArguments)}
if _${arrayField.name}Err != nil {
return nil, errors.Wrap(_${arrayField.name}Err, "Error parsing '${arrayField.name}' field")<@emitImport import="github.com/pkg/errors" />
// Manual Field (${manualField.name})
${manualField.name}, _${manualField.name}Err := ${helper.toParseExpression(manualField, manualField.type, manualField.parseExpression, parserArguments)}
if _${manualField.name}Err != nil {
return nil, errors.Wrap(_${manualField.name}Err, "Error parsing '${manualField.name}' field")<@emitImport import="github.com/pkg/errors" />
}
<#if arrayField.name == "value">
<#if manualField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
Expand Down Expand Up @@ -219,14 +220,14 @@ func ${type.name}Serialize(writeBuffer utils.WriteBuffer, value api.PlcValue<#if
writeBuffer.PushContext("${type.name}")
switch {
<#assign defaultSet=false>
<#list type.switchField.cases as case>
<#list type.switchField.orElseThrow().cases as case>
<@compress single_line=true>
<#if case.discriminatorValueTerms?has_content>
case
<#list case.discriminatorValueTerms as discriminatorValueTerm>
${helper.toParseExpression(null, null, type.switchField.discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} ==
${helper.toParseExpression(null, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} ==
<#assign typeRef=parserArguments[discriminatorValueTerm?index].type>
<#if typeRef.isComplexTypeReference()>
<#if typeRef.isNonSimpleTypeReference()>
<#if helper.isEnumTypeReference(typeRef)>
<#assign enumType=helper.getLanguageTypeNameForTypeReference(typeRef)>
<#assign enumTypeWithoutTraces=tracer.removeTraces(enumType)>
Expand All @@ -251,13 +252,14 @@ func ${type.name}Serialize(writeBuffer utils.WriteBuffer, value api.PlcValue<#if
<#switch field.typeName>
<#case "array">
<#assign arrayField = field.asArrayField().orElseThrow()>
<#assign arrayElementType = arrayField.type.elementTypeReference>

// Array Field (${arrayField.name})
for i := uint32(0); i < uint32(${helper.toSerializationExpression(null, null, arrayField.loopExpression, parserArguments)}); i++ {
<#if case.name = "Struct">
${arrayField.name} := value.GetValue("${arrayField.name}")
</#if>
_itemErr := <#if arrayField.type.isSimpleTypeReference()>${helper.getWriteBufferWriteMethodCall("", arrayField.type.asSimpleTypeReference().orElseThrow(), arrayField.name + ".GetIndex(i).Get" + helper.getLanguageTypeNameForTypeReference(arrayField.type)?cap_first + "()", arrayField)}<#else>Complex type array in data-io serialization currently not implemented</#if>
_itemErr := <#if arrayElementType.isSimpleTypeReference()>${helper.getWriteBufferWriteMethodCall("", arrayElementType.asSimpleTypeReference().orElseThrow(), arrayField.name + ".GetIndex(i).Get" + helper.getLanguageTypeNameForTypeReference(arrayElementType)?cap_first + "()", arrayField)}<#else>Complex type array in data-io serialization currently not implemented</#if>
if _itemErr != nil {
return errors.Wrap(_itemErr, "Error serializing 'value' field")<@emitImport import="github.com/pkg/errors" />
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (e ${type.name}) ${constantName?cap_first}() ${helper.getLanguageTypeNameFo
switch e {
<#list helper.getUniqueEnumValues(type.enumValues) as enumValue>
case ${helper.escapeValue(type.type.orElseThrow(), enumValue.value)}: { /* '${enumValue.value}' */
<#if type.getConstantType(constantName).isComplexTypeReference()>
<#if type.getConstantType(constantName).isNonSimpleTypeReference()>
<#if helper.isEnumTypeReference(type.getConstantType(constantName))>
return <#if helper.escapeEnumValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow()) = "0">${helper.escapeEnumValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow())}<#else>${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName))}_${helper.escapeEnumValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow())}</#if>
<#else>
Expand Down
Loading

0 comments on commit 749e034

Please sign in to comment.