使用@Repeatable优化多版本注解方式
parent
d14d42c90c
commit
dd370d224f
|
@ -2,15 +2,13 @@ package io.github.yezhihao.protostar.annotation;
|
|||
|
||||
import io.github.yezhihao.protostar.DataType;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author yezhihao
|
||||
* home https://gitee.com/yezhihao/jt808-server
|
||||
*/
|
||||
@Repeatable(Fs.class)
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Field {
|
||||
|
|
|
@ -4,7 +4,6 @@ import io.github.yezhihao.protostar.DataType;
|
|||
import io.github.yezhihao.protostar.ProtostarUtil;
|
||||
import io.github.yezhihao.protostar.Schema;
|
||||
import io.github.yezhihao.protostar.annotation.Field;
|
||||
import io.github.yezhihao.protostar.annotation.Fs;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
@ -20,11 +19,12 @@ public class Test {
|
|||
Schema<Foo> schema_v1 = multiVersionSchema.get(1);
|
||||
|
||||
ByteBuf buffer = Unpooled.buffer(32);
|
||||
schema_v0.writeTo(buffer, foo());
|
||||
Foo foo = foo();
|
||||
schema_v0.writeTo(buffer, foo);
|
||||
String hex = ByteBufUtil.hexDump(buffer);
|
||||
System.out.println(hex);
|
||||
|
||||
Foo foo = schema_v0.readFrom(buffer);
|
||||
foo = schema_v0.readFrom(buffer);
|
||||
System.out.println(foo);
|
||||
System.out.println("=========================version: 0");
|
||||
|
||||
|
@ -52,8 +52,8 @@ public class Test {
|
|||
private int id;
|
||||
private LocalDateTime dateTime;
|
||||
|
||||
@Fs({@Field(index = 0, type = DataType.STRING, lengthSize = 1, desc = "名称", version = 0),
|
||||
@Field(index = 0, type = DataType.STRING, length = 10, desc = "名称", version = 1)})
|
||||
@Field(index = 0, type = DataType.STRING, lengthSize = 1, desc = "名称", version = 0)
|
||||
@Field(index = 0, type = DataType.STRING, length = 10, desc = "名称", version = 1)
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -62,8 +62,8 @@ public class Test {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
@Fs({@Field(index = 1, type = DataType.WORD, desc = "ID", version = 0),
|
||||
@Field(index = 1, type = DataType.DWORD, desc = "ID", version = 1)})
|
||||
@Field(index = 1, type = DataType.WORD, desc = "ID", version = 0)
|
||||
@Field(index = 1, type = DataType.DWORD, desc = "ID", version = 1)
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -72,6 +72,7 @@ public class Test {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
@Field(index = 3, type = DataType.BCD8421, desc = "日期", version = {0, 1})
|
||||
@Field(index = 3, type = DataType.BCD8421, desc = "日期", version = {0, 1})
|
||||
public LocalDateTime getDateTime() {
|
||||
return dateTime;
|
||||
|
|
|
@ -21,11 +21,12 @@ public class Test {
|
|||
Schema<Foo> schema = multiVersionSchema.get(0);
|
||||
|
||||
ByteBuf buffer = Unpooled.buffer(32);
|
||||
schema.writeTo(buffer, foo());
|
||||
Foo foo = foo();
|
||||
schema.writeTo(buffer, foo);
|
||||
String hex = ByteBufUtil.hexDump(buffer);
|
||||
System.out.println(hex);
|
||||
|
||||
Foo foo = schema.readFrom(buffer);
|
||||
foo = schema.readFrom(buffer);
|
||||
System.out.println(foo);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue