Skip to content

Commit

Permalink
fix(plc4j/spi): implement const field
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl authored and chrisdutz committed Nov 17, 2021
1 parent dd3cad5 commit 4c1a1eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class FieldReaderAssert<T> implements FieldReader<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(FieldReaderAssert.class);
Expand All @@ -34,10 +36,11 @@ public T readField(String logicalName, DataReader<T> dataReader, WithReaderArgs.
throw new IllegalStateException("not possible with assert field");
}

public T readAssertField(String logicalName, DataReader<T> dataReader, boolean condition, WithReaderArgs... readerArgs) throws ParseException {
if (!condition) {
throw new ParseAssertException("Doesn't meet expectations");
public T readAssertField(String logicalName, DataReader<T> dataReader, T expectedValue, WithReaderArgs... readerArgs) throws ParseException {
T readValue = dataReader.read(logicalName, readerArgs);
if (Objects.equals(readValue, expectedValue)) {
throw new ParseAssertException("Actual value " + readValue + " doesn't match expected " + expectedValue);
}
return dataReader.read(logicalName, readerArgs);
return readValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@
package org.apache.plc4x.java.spi.codegen.fields;

import org.apache.plc4x.java.spi.codegen.io.DataReader;
import org.apache.plc4x.java.spi.generation.ParseAssertException;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.WithReaderArgs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class FieldReaderConst<T> implements FieldReader<T> {

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

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

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

}

0 comments on commit 4c1a1eb

Please sign in to comment.