Skip to content

Commit

Permalink
fix(plc4j/codgen): draft a const call
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl authored and chrisdutz committed Nov 17, 2021
1 parent e882921 commit ee1a6d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import static org.apache.plc4x.java.spi.generation.StaticHelper.*;
import ${helper.packageName(protocolName, languageName, outputFlavor)}.*;
<#if helper.getComplexTypeReferences()?has_content>import ${helper.packageName(protocolName, languageName, outputFlavor)}.io.*;</#if>
import ${helper.packageName(protocolName, languageName, outputFlavor)}.types.*;

import org.apache.plc4x.java.spi.codegen.fields;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.spi.generation.*;
import org.apache.plc4x.java.api.value.PlcValue;
Expand Down Expand Up @@ -318,12 +320,15 @@ public class ${type.name}IO implements <#if outputFlavor != "passive">MessageIO<
<#if helper.isSimpleTypeReference(constField.type)>
<#assign simpleTypeReference = constField.type.asSimpleTypeReference().orElseThrow()>
${helper.getLanguageTypeNameForField(field)} ${constField.name} = ${helper.getReadBufferReadMethodCall(constField.name, simpleTypeReference, "", constField)};

// TODO: value assingment gets useless once this is transformed to field read only as the check happens within anyway
FieldReaderConst.INSTANCE.readConstField(String logicalName, new DataReaderSimple${helper.getLanguageTypeNameForTypeReference(simpleTypeReference, false)}(readBuffer), ${type.name}.${constField.name?upper_case}))
<#else>
${helper.getLanguageTypeNameForField(field)} ${constField.name} = ${helper.getLanguageTypeNameForField(field)}.enumForValue(${helper.getReadBufferReadMethodCall(constField.name, helper.getEnumBaseTypeReference(constField.type), "", constField)});
</#if>
if(${constField.name} != ${type.name}.${constField.name?upper_case}) {
throw new ParseException("Expected constant value " + ${type.name}.${constField.name?upper_case} + " but got " + ${constField.name});
}
</#if>
<#break>
<#case "discriminator">
<#assign discriminatorField = field.asDiscriminatorField().orElseThrow()>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ public class FieldReaderConst<T> implements FieldReader<T> {

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

public static final FieldReaderConst<?> INSTANCE = new FieldReaderConst<>();

@Override
public T readField(String logicalName, DataReader<T> dataReader, WithReaderArgs... readerArgs) throws ParseException {
throw new IllegalStateException("not possible with const field");
}

public T readAssertField(String logicalName, DataReader<T> dataReader, T expectedValue, WithReaderArgs... readerArgs) throws ParseException {
public void readConstField(String logicalName, DataReader<T> dataReader, T expectedValue, WithReaderArgs... readerArgs) throws ParseException {
T constValue = dataReader.read(logicalName, readerArgs);
if (!Objects.equals(constValue, expectedValue)) {
throw new ParseException("Actual value " + constValue + " doesn't match expected " + expectedValue);
}
return constValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class FieldReaderImplicit<T> implements FieldReader<T> {

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

public static final FieldReaderImplicit<?> INSTANCE = new FieldReaderImplicit<>();

@Override
public T readField(String logicalName, DataReader<T> dataReader, WithReaderArgs... readerArgs) throws ParseException {
return switchByteOrderIfNecessary(() -> dataReader.read(logicalName, readerArgs), dataReader, extractByteOder(readerArgs).orElse(null));
Expand Down

0 comments on commit ee1a6d2

Please sign in to comment.