Skip to content

Commit

Permalink
Add support for new blob type
Browse files Browse the repository at this point in the history
  • Loading branch information
Cirras committed May 2, 2023
1 parent 01abc95 commit f328641
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Support for new `blob` type, which maps to `byte[]`.

### Changed

- Rename `ItemType.MONEY` to `CURRENCY`.
Expand Down
9 changes: 9 additions & 0 deletions eolib/src/main/java/dev/cirras/data/EoReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public int getByte() {
return Byte.toUnsignedInt(readByte());
}

/**
* Reads an array of raw bytes from the input data.
*
* @return an array of raw bytes
*/
public byte[] getBytes(int length) {
return readBytes(length);
}

/**
* Reads an encoded 1-byte integer from the input data.
*
Expand Down
8 changes: 8 additions & 0 deletions eolib/src/test/java/dev/cirras/data/EoReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ void testOverReadByte() {
assertThat(reader.getByte()).inHexadecimal().isEqualTo(0x00);
}

@Test
void testGetBytes() {
EoReader reader = createReader(0x01, 0x02, 0x03, 0x04, 0x05);
assertThat(reader.getBytes(3)).inHexadecimal().containsExactly(0x01, 0x02, 0x03);
assertThat(reader.getBytes(10)).inHexadecimal().containsExactly(0x04, 0x05);
assertThat(reader.getBytes(1)).inHexadecimal().isEmpty();
}

@Test
void testGetChar() {
EoReader reader = createReader(0x01, 0x02, 0x80, 0x81, 0xFD, 0xFE, 0xFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import dev.cirras.generate.type.BasicType;
import dev.cirras.generate.type.BlobType;
import dev.cirras.generate.type.BoolType;
import dev.cirras.generate.type.CustomType;
import dev.cirras.generate.type.EnumType;
Expand Down Expand Up @@ -506,6 +507,8 @@ private CodeBlock getWriteStatement() {
getWriteStatementForBasicType((BasicType) type, lengthExpression, padded),
valueExpression)
.build();
} else if (type instanceof BlobType) {
return CodeBlock.builder().addStatement("writer.addBytes($L)", valueExpression).build();
} else if (type instanceof StructType) {
return CodeBlock.builder()
.addStatement(
Expand Down Expand Up @@ -684,6 +687,8 @@ private CodeBlock getReadStatement() {
} else {
statement.add(readBasicType);
}
} else if (type instanceof BlobType) {
statement.add("reader.getBytes(reader.getRemaining())");
} else if (type instanceof StructType) {
statement.add(
"$T.deserialize(reader)",
Expand Down Expand Up @@ -773,6 +778,8 @@ private TypeName getJavaTypeName() {
result = ClassName.get(String.class);
} else if (type instanceof BoolType) {
result = ClassName.get(Boolean.class);
} else if (type instanceof BlobType) {
result = TypeName.get(byte[].class);
} else if (type instanceof CustomType) {
result = ClassName.get(((CustomType) type).getPackageName(), type.getName());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.cirras.generate.type;

import java.util.Optional;

public class BlobType implements Type {
@Override
public String getName() {
return "blob";
}

@Override
public Optional<Integer> getFixedSize() {
return Optional.empty();
}

@Override
public boolean isBounded() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ private Type createType(String name, Length length) {
case "encoded_string":
result = new StringType(name, length);
break;
case "blob":
result = new BlobType();
break;
default:
result = createCustomType(name, underlyingType);
break;
Expand Down

0 comments on commit f328641

Please sign in to comment.