Skip to content

Commit eef5c56

Browse files
committed
feat[codec-core]: DataField.ValueLengthType
1 parent e383848 commit eef5c56

60 files changed

Lines changed: 464 additions & 501 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ext/jt/jt-808-server-spring-boot-starter-reactive/src/test/java/io/github/hylexus/xtream/codec/ext/jt808/builtin/messages/mixed/MixedEntity02Test.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.github.hylexus.xtream.codec.ext.jt808.builtin.messages.mixed;
1818

1919
import io.github.hylexus.xtream.codec.common.utils.FormatUtils;
20+
import io.github.hylexus.xtream.codec.core.annotation.ext.LengthFieldType;
2021
import io.github.hylexus.xtream.codec.core.tracker.CodecTracker;
2122
import io.github.hylexus.xtream.codec.core.type.simple.DataField;
2223
import io.github.hylexus.xtream.codec.ext.jt808.builtin.messages.BaseCodecTest;
@@ -44,7 +45,7 @@ void test() {
4445
dataList.add(u32("状态", 222L));
4546
final DataField.Dict<DataField.U8> extraItems = dict(
4647
DataField.U8.class,
47-
DataField.ValueLengthType.u8,
48+
LengthFieldType.u8,
4849
Map.of(
4950
// 里程,DWORD,1/10km,对应车上里程表读数
5051
u8((short) 0x01), u32(123L),
@@ -61,7 +62,7 @@ void test() {
6162
.setDirection(u16(1))
6263
.setTime(time)
6364
.setExtraItems(extraItems)
64-
.setExtraItems2(tlv(u8((short) 0x01), DataField.ValueLengthType.u8, u32(123L)));
65+
.setExtraItems2(tlv(u8((short) 0x01), LengthFieldType.u8, u32(123L)));
6566
dataList.add(mixedEntity02);
6667

6768
final ByteBuf encodeBuffer = responseEncoder.encode(dataList, new Jt808MessageDescriber(0x0200, Jt808ProtocolVersion.VERSION_2013, terminalId2013));

ext/jt/jt-808-server-spring-boot-starter-reactive/src/test/java/io/github/hylexus/xtream/codec/ext/jt808/builtin/messages/mixed/MixedFieldTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.github.hylexus.xtream.codec.ext.jt808.builtin.messages.mixed;
1818

1919
import io.github.hylexus.xtream.codec.common.utils.FormatUtils;
20+
import io.github.hylexus.xtream.codec.core.annotation.ext.LengthFieldType;
2021
import io.github.hylexus.xtream.codec.core.tracker.CodecTracker;
2122
import io.github.hylexus.xtream.codec.core.type.simple.DataField;
2223
import io.github.hylexus.xtream.codec.ext.jt808.builtin.messages.BaseCodecTest;
@@ -32,7 +33,6 @@
3233
import java.util.Map;
3334

3435
import static io.github.hylexus.xtream.codec.core.type.simple.DataField.U8;
35-
import static io.github.hylexus.xtream.codec.core.type.simple.DataField.ValueLengthType;
3636
import static io.github.hylexus.xtream.codec.core.type.simple.DataFields.*;
3737

3838
public class MixedFieldTest extends BaseCodecTest {
@@ -74,7 +74,7 @@ void test() {
7474
// 时间
7575
bcd8421String("251108223456"),
7676
// 位置附加项
77-
dict(U8.class, ValueLengthType.u8, extraItems)
77+
dict(U8.class, LengthFieldType.u8, extraItems)
7878
);
7979

8080
final ByteBuf encode = responseEncoder.encode(dataFieldList, new Jt808MessageDescriber(0x0200, Jt808ProtocolVersion.VERSION_2013, terminalId2013));

xtream-codec-core/src/main/java/io/github/hylexus/xtream/codec/core/DataFieldEncoder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public void encodeWithTracker(FieldCodec.SerializeContext context, @Nullable Dat
201201
private void doEncodeFieldWithTracker(FieldCodec.SerializeContext context, ByteBuf output, DataField dataField) {
202202
final CodecTracker codecTracker = Objects.requireNonNull(context.codecTracker());
203203
final int indexBeforeWrite = output.writerIndex();
204+
final String name = codecTracker.getFieldName(dataField.name());
204205
switch (dataField) {
205206
case DataField.I8 i8 -> output.writeByte(i8.value());
206207
case DataField.U8 u8 -> output.writeByte(u8.value());
@@ -221,14 +222,14 @@ private void doEncodeFieldWithTracker(FieldCodec.SerializeContext context, ByteB
221222
case DataField.Struct struct -> {
222223
final List<DataField> value = struct.value();
223224
final DefaultSerializeContext newContext = new DefaultSerializeContext(context, value);
224-
final NestedFieldSpan nestedFieldSpan = codecTracker.startNewNestedFieldSpan(dataField.name(), "", dataField.type(), this.getClass().getSimpleName());
225+
final NestedFieldSpan nestedFieldSpan = codecTracker.startNewNestedFieldSpan(name, "", dataField.type(), this.getClass().getSimpleName());
225226
this.encodeWithTracker(newContext, value, output);
226227
nestedFieldSpan.setHexString(FormatUtils.toHexString(output, indexBeforeWrite, output.writerIndex() - indexBeforeWrite));
227228
codecTracker.finishCurrentSpan();
228229
}
229230
case DataField.Sequence sequence -> {
230231
final List<DataField> value = sequence.value();
231-
final CollectionFieldSpan collectionFieldSpan = codecTracker.startNewCollectionFieldSpanForSimpleField(dataField.name());
232+
final CollectionFieldSpan collectionFieldSpan = codecTracker.startNewCollectionFieldSpanForSimpleField(name);
232233
final int parentIndexBeforeWrite = output.writerIndex();
233234
final DefaultSerializeContext newContext = new DefaultSerializeContext(context, value);
234235
this.encodeWithTracker(newContext, value, output);
@@ -259,7 +260,7 @@ private void doEncodeFieldWithTracker(FieldCodec.SerializeContext context, ByteB
259260
// 3. valueLength
260261
final int valueLength = temp.writerIndex();
261262
codecTracker.updateTrackerHints(MapEntryItemSpan.Type.VALUE_LENGTH);
262-
simpleMap.valueLengthType().writeToWithTracker(output, valueLength, codecTracker);
263+
simpleMap.valueLengthType().writeToWithTracker(output, valueLength, codecTracker, "valueLength");
263264
output.writeBytes(temp);
264265
mapEntrySpan.setHexString(FormatUtils.toHexString(output, writerIndex, output.writerIndex() - writerIndex));
265266
codecTracker.finishCurrentSpan();
@@ -275,7 +276,7 @@ private void doEncodeFieldWithTracker(FieldCodec.SerializeContext context, ByteB
275276
}
276277
case DataField.TlvDataField tlvDataField -> {
277278
final DefaultSerializeContext newContext = new DefaultSerializeContext(context, tlvDataField);
278-
final NestedFieldSpan nestedFieldSpan = codecTracker.startNewNestedFieldSpan(dataField.name(), "", dataField.type(), this.getClass().getSimpleName());
279+
final NestedFieldSpan nestedFieldSpan = codecTracker.startNewNestedFieldSpan(name, "", dataField.type(), this.getClass().getSimpleName());
279280
// 1. tag
280281
final DataField.DictKey tag = tlvDataField.tag();
281282
this.doEncodeFieldWithTracker(newContext, output, tag);
@@ -288,7 +289,7 @@ private void doEncodeFieldWithTracker(FieldCodec.SerializeContext context, ByteB
288289

289290
// 3. length
290291
final int valueLength = temp.writerIndex();
291-
tlvDataField.length().writeToWithTracker(output, valueLength, codecTracker);
292+
tlvDataField.length().writeToWithTracker(output, valueLength, codecTracker, "valueLength");
292293
output.writeBytes(temp);
293294

294295
nestedFieldSpan.setHexString(FormatUtils.toHexString(output, indexBeforeWrite, output.writerIndex() - indexBeforeWrite));
@@ -304,7 +305,7 @@ private void doEncodeFieldWithTracker(FieldCodec.SerializeContext context, ByteB
304305
&& !(dataField instanceof DataField.Sequence)
305306
&& !(dataField instanceof DataField.SimpleTlvDataField<?>)) {
306307
final String hexString = FormatUtils.toHexString(output, indexBeforeWrite, output.writerIndex() - indexBeforeWrite);
307-
codecTracker.addFieldSpan(codecTracker.getCurrentSpan(), dataField.name(), dataField.value(), hexString, this.getClass().getSimpleName(), dataField.getClass().getSimpleName());
308+
codecTracker.addFieldSpan(codecTracker.getCurrentSpan(), name, dataField.value(), hexString, this.getClass().getSimpleName(), dataField.getClass().getSimpleName());
308309
}
309310
}
310311

xtream-codec-core/src/main/java/io/github/hylexus/xtream/codec/core/ExtendMetaRegistry.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package io.github.hylexus.xtream.codec.core;
1818

19-
import io.github.hylexus.xtream.codec.core.annotation.pair.XtreamPairFieldSequence;
20-
import io.github.hylexus.xtream.codec.core.annotation.tlv.XtreamTLVFieldSequence;
2119
import io.github.hylexus.xtream.codec.core.impl.domain.XtreamPairFieldSequenceMeta;
2220
import io.github.hylexus.xtream.codec.core.impl.domain.XtreamTLVFieldSequenceMeta;
2321
import org.jspecify.annotations.NullMarked;
@@ -27,8 +25,8 @@
2725
@NullMarked
2826
public interface ExtendMetaRegistry {
2927

30-
XtreamTLVFieldSequenceMeta getXtreamTlvFieldSequenceMeta(int targetVersion, Field field, XtreamTLVFieldSequence xtreamTlvFieldSequence);
28+
XtreamTLVFieldSequenceMeta getXtreamTlvFieldSequenceMeta(int targetVersion, Field field);
3129

32-
XtreamPairFieldSequenceMeta getXtreamPairFieldSequenceMeta(int targetVersion, Field field, XtreamPairFieldSequence xtreamPairFieldSequence);
30+
XtreamPairFieldSequenceMeta getXtreamPairFieldSequenceMeta(int targetVersion, Field field);
3331

3432
}

xtream-codec-core/src/main/java/io/github/hylexus/xtream/codec/core/annotation/ext/ValueMatcher.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
* @see ValueMatcherMeta
2727
*/
2828
public @interface ValueMatcher {
29+
30+
/**
31+
* 反序列化 {@link java.util.List}&lt;{@link io.github.hylexus.xtream.codec.core.type.Pair}&gt; 类型时必须指定值长度
32+
*/
33+
int length() default -1;
34+
2935
int[] version() default {XtreamField.ALL_VERSION};
3036

3137
byte[] matchI8() default {};

xtream-codec-core/src/main/java/io/github/hylexus/xtream/codec/core/annotation/ext/ValueMatcherWithLength.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

xtream-codec-core/src/main/java/io/github/hylexus/xtream/codec/core/annotation/pair/XtreamPairFieldSequence.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.github.hylexus.xtream.codec.core.annotation.XtreamField;
2222
import io.github.hylexus.xtream.codec.core.annotation.ext.Key;
2323
import io.github.hylexus.xtream.codec.core.annotation.ext.ValueDecoderCommonParam;
24-
import io.github.hylexus.xtream.codec.core.annotation.ext.ValueMatcherWithLength;
24+
import io.github.hylexus.xtream.codec.core.annotation.ext.ValueMatcher;
2525
import io.github.hylexus.xtream.codec.core.impl.codec.pair.PairCodecs;
2626
import org.jetbrains.annotations.ApiStatus;
2727
import org.springframework.core.annotation.AliasFor;
@@ -49,7 +49,7 @@
4949
@interface Value {
5050
ValueDecoderCommonParam[] commonParams() default {};
5151

52-
ValueMatcherWithLength[] matchers();
52+
ValueMatcher[] matchers();
5353
}
5454

5555
// 以下都是 XtreamField 的属性

0 commit comments

Comments
 (0)