Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat 0103 #137

Merged
merged 54 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
100d2ea
Merge pull request #113 from indunet/develop
Deng-Ran Oct 8, 2022
dc5c05f
feat: add compiler
Deng-Ran Oct 10, 2022
a02db28
test: add lambda test
Deng-Ran Oct 12, 2022
15a0724
feat: lambda compiler and its tests
Deng-Ran Oct 13, 2022
a9610ef
feat: update lambda
Deng-Ran Oct 18, 2022
69412d7
doc: update readme
Deng-Ran Oct 18, 2022
a6c8ec3
fix: remove val in FormulaBuilder
Deng-Ran Oct 18, 2022
0510d53
fix: remove val in FormulaBuilder
Deng-Ran Oct 18, 2022
64a3493
build: add maven scala plugin
Deng-Ran Oct 18, 2022
cf8dd82
Merge pull request #115 from indunet/lambda
Deng-Ran Oct 18, 2022
47e1b3d
doc: update readme
Deng-Ran Oct 18, 2022
da3b000
Merge pull request #116 from indunet/lambda
Deng-Ran Oct 18, 2022
d5f255a
test: refactor fastproto benchmark
Deng-Ran Oct 20, 2022
4f34a52
test: move benchmark
Deng-Ran Oct 20, 2022
ad3dbf0
Merge pull request #117 from indunet/benchmark
Deng-Ran Oct 20, 2022
b550257
doc: add readme to benchmark
Deng-Ran Oct 20, 2022
07844cf
Merge pull request #118 from indunet/benchmark
Deng-Ran Oct 20, 2022
fddcf21
feat: add auto type
Deng-Ran Oct 21, 2022
4e85bdd
feat: update ProtocolType
Deng-Ran Oct 23, 2022
63c27a6
feat: add mapper.
Deng-Ran Oct 24, 2022
825b879
doc: add doc of auto type
Deng-Ran Oct 24, 2022
90f8451
Merge pull request #119 from indunet/autotype
Deng-Ran Oct 25, 2022
f62aa16
test: update unit test of auto type
Deng-Ran Oct 27, 2022
54e1484
feat: update api
Deng-Ran Oct 27, 2022
c727d82
Merge pull request #120 from indunet/codec
Deng-Ran Oct 27, 2022
643f56a
fix: mapper of auto type
Deng-Ran Oct 27, 2022
1cf632e
Merge pull request #121 from indunet/fix-autotype
Deng-Ran Oct 27, 2022
49c58d8
doc: add ignore chapter
Deng-Ran Nov 1, 2022
99d2831
Merge pull request #122 from indunet/doc
Deng-Ran Nov 1, 2022
fd39acc
feat: add EndianPolicy to array type
Deng-Ran Nov 1, 2022
09318bf
Merge pull request #123 from indunet/array-endian
Deng-Ran Nov 1, 2022
8425d04
doc: update readme
Deng-Ran Nov 2, 2022
2d06ade
Merge pull request #124 from indunet/doc
Deng-Ran Nov 2, 2022
e3c7ab6
doc: update manual
Deng-Ran Dec 20, 2022
bcb652a
Merge pull request #125 from indunet/doc
Deng-Ran Dec 20, 2022
3ee73b1
feat: working without annotations
Deng-Ran Dec 21, 2022
91cd369
Merge pull request #126 from indunet/feat-1221
Deng-Ran Dec 21, 2022
9ac7299
feat: add bool array type
Deng-Ran Dec 22, 2022
cbcc7df
Merge pull request #131 from indunet/feat-1222
Deng-Ran Dec 22, 2022
07b8f15
feat: update Decoder api
Deng-Ran Dec 23, 2022
d82e177
Merge pull request #132 from indunet/feat-1223
Deng-Ran Dec 23, 2022
c43e8aa
feat: update Decoder api
Deng-Ran Dec 23, 2022
0a24242
Merge pull request #133 from indunet/feat-1223
Deng-Ran Dec 23, 2022
f80210c
feat: add dynamic byte buffer
Deng-Ran Dec 25, 2022
5efcdbf
Merge pull request #134 from indunet/feat-1225
Deng-Ran Dec 25, 2022
4cd2373
feat: add AsciiType
Deng-Ran Dec 25, 2022
582168d
Merge pull request #135 from indunet/feat-1225
Deng-Ran Dec 25, 2022
ff95328
feat: add char array type
Deng-Ran Dec 29, 2022
169c786
Merge pull request #136 from indunet/feat-1229
Deng-Ran Jan 1, 2023
e5d6144
feat: add char array type
Deng-Ran Jan 3, 2023
eda01e4
doc: add annotation doc
Deng-Ran Jan 3, 2023
731fb75
feat: update dynamic byte array
Deng-Ran Jan 3, 2023
9e176a3
feat: update api
Deng-Ran Jan 4, 2023
bfa9bdf
feat: add formula to Decoder
Deng-Ran Jan 5, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: update ProtocolType
  • Loading branch information
Deng-Ran committed Oct 23, 2022
commit 4e85bdd3dd771c5b76cf0be740a7fe13205674e7
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import org.indunet.fastproto.annotation.*;
import org.indunet.fastproto.util.CodecUtils;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -77,7 +75,7 @@ public class Sample {
public Sample() {
val random = new Random();

this.bool1 = random.nextInt() % 2 == 0 ? true : false;
this.bool1 = random.nextBoolean();
this.byte8 = (byte) random.nextInt(Byte.MAX_VALUE);
this.short16 = (short) random.nextInt(Short.MAX_VALUE);
this.int32 = random.nextInt();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/indunet/fastproto/ProtocolType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.indunet.fastproto;

import lombok.val;
import org.indunet.fastproto.annotation.*;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -45,6 +46,22 @@ public interface ProtocolType {
Class<? extends Annotation> UINT64 = UInt64Type.class;
Class<? extends Annotation> ENUM = EnumType.class;

static <T> T proxy(AutoType autoType, Class<T> dataTypeAnnotationClass) {
return (T) Proxy.newProxyInstance(ProtocolType.class.getClassLoader(), new Class<?>[] {dataTypeAnnotationClass}, (proxy, method, args) -> {
val mth = Arrays.stream(autoType.getClass().getMethods())
.filter(m -> m.getName().equals(method.getName()))
.findAny()
.get();

if (Arrays.asList("offset", "byteOffset", "bitOffset", "length")
.contains(mth.getName())) {
return ((int[]) mth.invoke(autoType, args))[0];
} else {
return mth.invoke(autoType, args);
}
});
}

static ProtocolType proxy(Annotation typeAnnotation) {
return (ProtocolType) Proxy.newProxyInstance(ProtocolType.class.getClassLoader(), new Class<?>[]{ProtocolType.class, typeAnnotation.annotationType()}, (proxy, method, args) -> {
switch (method.getName()) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/indunet/fastproto/annotation/AutoType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@
package org.indunet.fastproto.annotation;

import org.indunet.fastproto.EndianPolicy;
import org.indunet.fastproto.graph.resolve.validate.DecodingFormulaValidator;
import org.indunet.fastproto.graph.resolve.validate.EncodingFormulaValidator;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Auto type.
*
* @author Deng Ran
* @since 3.7.1
*/
@DataType
@Validator({DecodingFormulaValidator.class, EncodingFormulaValidator.class})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoType {
int[] offset() default {};

Expand All @@ -37,4 +48,6 @@
EndianPolicy[] endianPolicy() default {};

String charset() default "UTF-8";

String name() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.indunet.fastproto.annotation;

import org.indunet.fastproto.EndianPolicy;
import org.indunet.fastproto.graph.resolve.validate.DecodingFormulaValidator;
import org.indunet.fastproto.graph.resolve.validate.EncodingFormulaValidator;

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/indunet/fastproto/codec/CodecMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.indunet.fastproto.exception.CodecError;
import org.indunet.fastproto.exception.CodecException;
import org.indunet.fastproto.exception.DecodingException;
import org.indunet.fastproto.exception.ResolveException;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -221,6 +222,16 @@ public static <T, R> Function<T, R> getFormula(Class<? extends Function> clazz)
});
}

public static Class getDataTypeAnnotationClass(Class fieldType) {
return codecMap.entrySet().stream()
.filter(e -> e.getValue().keySet().stream()
.anyMatch(p -> p.test(fieldType)))
.map(Map.Entry::getKey)
.findAny()
.orElseThrow(() -> new ResolveException(
String.format("%s is not supported", fieldType.getName())));
}

public static Function<byte[], ?> getDecoder(CodecContext context, Class<? extends Function> clazz) {
if (clazz != null) {
val type = Arrays.stream(clazz.getGenericInterfaces())
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import lombok.val;
import org.indunet.fastproto.ProtocolType;
import org.indunet.fastproto.annotation.AutoType;
import org.indunet.fastproto.annotation.DataType;
import org.indunet.fastproto.codec.CodecMapper;
import org.indunet.fastproto.exception.ResolveException;
import org.indunet.fastproto.graph.Reference;

import java.lang.annotation.Annotation;
import java.util.Arrays;

/**
Expand All @@ -40,10 +43,19 @@ public void process(Reference reference) {
.findAny()
.orElseThrow(ResolveException::new);

reference.setDataTypeAnnotation(typeAnnotation);
if (typeAnnotation instanceof AutoType) {
Class<? extends Annotation> type = CodecMapper.getDataTypeAnnotationClass((Class) field.getGenericType());
Annotation proxy = (Annotation) ProtocolType.proxy((AutoType) typeAnnotation, type);

reference.setDataTypeAnnotation(proxy);
reference.setProtocolType(ProtocolType.proxy(proxy));
} else {
reference.setDataTypeAnnotation(typeAnnotation);
reference.setProtocolType(ProtocolType.proxy(typeAnnotation));
}

reference.setReferenceType(Reference.ReferenceType.FIELD);

reference.setProtocolType(ProtocolType.proxy(typeAnnotation));

this.forward(reference);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.indunet.fastproto.pipeline.encode;

import lombok.val;
import org.indunet.fastproto.exception.AddressingException;
import org.indunet.fastproto.exception.CodecError;
import org.indunet.fastproto.exception.ResolveException;
import org.indunet.fastproto.graph.Reference;
import org.indunet.fastproto.pipeline.FlowCode;
import org.indunet.fastproto.pipeline.Pipeline;
Expand All @@ -41,15 +41,15 @@ public void process(PipelineContext context) {
val type = r.getProtocolType();

if (type.offset() < 0 || type.length() < 0) {
throw new AddressingException(CodecError.UNABLE_INFER_LENGTH);
throw new ResolveException(CodecError.UNABLE_INFER_LENGTH);
} else {
return type.offset() + type.size() + type.length();
}
}).max()
.orElse(0);

if (max == 0) {
throw new AddressingException(CodecError.UNABLE_INFER_LENGTH);
throw new ResolveException(CodecError.UNABLE_INFER_LENGTH);
} else {
context.setDatagram(new byte[max]);
}
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/org/indunet/fastproto/ProtocolTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import lombok.SneakyThrows;
import lombok.val;
import org.indunet.fastproto.annotation.BoolType;
import org.indunet.fastproto.annotation.UInt8Type;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Deng Ran
Expand All @@ -30,7 +31,7 @@
public class ProtocolTypeTest {
@Test
@SneakyThrows
public void testProxy() {
public void testProxy1() {
val field = TestObj.class.getDeclaredField("value");
val typeAnnotation = field.getAnnotation(UInt8Type.class);
val proxy = ProtocolType.proxy(typeAnnotation);
Expand All @@ -39,6 +40,14 @@ public void testProxy() {
assertEquals(UInt8Type.SIZE, proxy.size());
}

@Test
public void testProxy2() {
val proxy = ProtocolType.proxy(null, BoolType.class);

assertTrue(proxy instanceof BoolType);
assertNotNull(ProtocolType.proxy(proxy));
}

public static class TestObj {
@UInt8Type(offset = 0)
int value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package org.indunet.fastproto.api.auto;

import lombok.val;
import org.indunet.fastproto.annotation.AutoType;
import org.indunet.fastproto.util.CodecUtils;

import java.util.Random;

/**
* Auto object.
*
* @author Deng Ran
* @since 3.7.1
*/
public class AutoObject {
public class AutoTypeObject {
@AutoType(byteOffset = 0, bitOffset = 1)
Boolean bool1;

Expand All @@ -40,11 +44,25 @@ public class AutoObject {
@AutoType(offset = 10)
Long long64;

public AutoObject() {
public AutoTypeObject() {
val random = new Random();

this.bool1 = random.nextBoolean();
this.byte8 = (byte) random.nextInt(128);
this.short16 = (short) random.nextInt(Short.MAX_VALUE);
this.int32 = random.nextInt();
this.long64 = random.nextLong();
}

public byte[] toBytes() {
return null;
val bytes = new byte[20];

CodecUtils.boolType(bytes, 0, 1, this.bool1);
CodecUtils.byteType(bytes, 2, this.byte8);
CodecUtils.shortType(bytes, 4, this.short16);
CodecUtils.int32Type(bytes, 6, this.int32);
CodecUtils.int64Type(bytes, 10, this.long64);

return bytes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,33 @@

package org.indunet.fastproto.api.auto;

import lombok.val;
import org.indunet.fastproto.FastProto;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Unit test of auto type.
*
* @author Deng Ran
* @since 3.7.1
*/
public class AutoTest {
public class AutoTypeTest {
@Test
public void testParse() {
val expected = new AutoTypeObject();
val bytes = expected.toBytes();

assertEquals(expected, FastProto.parse(bytes, AutoTypeObject.class));
}

@Test
public void testToBytes() {
val object = new AutoTypeObject();
val expected = object.toBytes();

assertArrayEquals(expected, FastProto.toBytes(object, expected.length));
}
}