Skip to content

fix(plc4go/spi): preserve WithByteOrder in UpcastReaderArgs/UpcastWriterArgs#2494

Open
khalidDaoud wants to merge 1 commit intoapache:developfrom
twinfer:fix/go-spi-upcast-byte-order
Open

fix(plc4go/spi): preserve WithByteOrder in UpcastReaderArgs/UpcastWriterArgs#2494
khalidDaoud wants to merge 1 commit intoapache:developfrom
twinfer:fix/go-spi-upcast-byte-order

Conversation

@khalidDaoud
Copy link

Summary

  • UpcastReaderArgs/UpcastWriterArgs now preserve WithReaderWriterArgs concrete types instead of wrapping them in readerWriterArg

Problem

UpcastReaderArgs wraps every arg in readerWriterArg{arg, writerArg{}}. When the arg is already a WithReaderWriterArgs (e.g. codegen.WithByteOrder), this wrapping changes the concrete type from withOptionByteOrder to readerWriterArg.

FieldCommons.ExtractByteOrder does a type switch on withOptionByteOrder — which no longer matches the wrapped type. Result: ExtractByteOrder returns nil, and SwitchParseByteOrderIfNecessary skips the byte order switch entirely.

This means codegen.WithByteOrder(binary.LittleEndian) passed to ReadSimpleField/WriteSimpleField is silently ignored — fields are read/written with the buffer's default byte order.

Fix

Check if the arg already implements WithReaderWriterArgs before wrapping:

if rw, ok := arg.(WithReaderWriterArgs); ok {
    result[i] = rw
} else {
    result[i] = readerWriterArg{arg, writerArg{}}
}

Affected protocols

All protocols using byteOrder='LITTLE_ENDIAN' at the type level in mspec: ADS, AB-ETH, OPC UA, IEC 60870-5-104.

Test plan

…terArgs

UpcastReaderArgs and UpcastWriterArgs wrap WithReaderArgs/WithWriterArgs
into readerWriterArg structs. When the arg already implements
WithReaderWriterArgs (e.g. codegen.WithByteOrder), this wrapping
destroys the concrete type, causing ExtractByteOrder's type switch
to fail silently. The result is that codegen.WithByteOrder(LE) passed
to ReadSimpleField/WriteSimpleField has no effect — fields are read
and written with the buffer's default byte order instead.

Fix: check if the arg already implements WithReaderWriterArgs and
pass it through unwrapped.

Affects all protocols using type-level byteOrder in mspec:
ADS, AB-ETH, OPC UA, IEC 60870-5-104.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant