Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Dec 18, 2024
1 parent 82c84da commit f43ac6f
Show file tree
Hide file tree
Showing 18 changed files with 392 additions and 72 deletions.
13 changes: 7 additions & 6 deletions examples/maven-spring-avro/src/main/avro/test_avro_001.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"type": "string",
"doc": "Unique identifier of the order"
},
{
"name": "qr_code",
"type": "bytes",
"doc": "QR code"
},
{
"name": "created_at",
"type": [
Expand Down Expand Up @@ -76,12 +81,8 @@
},
{
"name": "amount",
"type": {
"type": "bytes",
"logicalType": "decimal",
"precision": 12,
"scale": 2
}
"type": "float",
"doc": "Amount"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package community.flock.wirespec.examples.maven.avro;

import com.eventloopsoftware.*;
import com.eventloopsoftware.TestAvroEnumIdentifier;
import com.eventloopsoftware.TestAvroOrder;
import com.eventloopsoftware.TestAvroOrderLines;
import com.eventloopsoftware.TestAvroRecord;
import com.eventloopsoftware.TestAvroRefNumber;
import com.eventloopsoftware.kafka.model.TestAvroMetadata;
import io.confluent.kafka.serializers.KafkaAvroDeserializer;
import io.confluent.kafka.serializers.KafkaAvroDeserializerConfig;
import io.confluent.kafka.serializers.KafkaAvroSerializer;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
Expand All @@ -28,17 +17,15 @@
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.org.checkerframework.checker.units.qual.A;
import org.testcontainers.utility.DockerImageName;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

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

Expand Down Expand Up @@ -75,6 +62,7 @@ var record = new TestAvroRecord(
new TestAvroMetadata("321", 1L),
new TestAvroOrder(
"123",
"QR Code".getBytes(),
Optional.of(1L),
List.of(
new TestAvroRefNumber("ref1", TestAvroEnumIdentifier.REF_1),
Expand All @@ -83,21 +71,22 @@ var record = new TestAvroRecord(

),
List.of(
new TestAvroOrderLines("11", "100"),
new TestAvroOrderLines("22", "200"),
new TestAvroOrderLines("33", "300")
new TestAvroOrderLines("11", 100.0F),
new TestAvroOrderLines("22", 200.0F),
new TestAvroOrderLines("33", 300.0F)
),
3L
)
);

service.listen("group1", message -> {
assertEquals(record, message);
assertRecordEquals(record, message);
latch.countDown();
});

service.listen("group2", message -> {
assertEquals(record, message);
assertRecordEquals(record, message);

latch.countDown();
});

Expand All @@ -107,4 +96,13 @@ var record = new TestAvroRecord(
assertTrue(messageConsumed);

}

void assertRecordEquals(TestAvroRecord expected, TestAvroRecord actual){
assertEquals(expected.metadata(), actual.metadata());
assertEquals(expected.order().number(), actual.order().number());
assertEquals(expected.order().a_number(), actual.order().a_number());
assertEquals(expected.order().created_at(), actual.order().created_at());
assertEquals(expected.order().lines(), actual.order().lines());
assertArrayEquals(expected.order().qr_code(), actual.order().qr_code());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,26 @@ import arrow.core.Either
import arrow.core.raise.either
import community.flock.wirespec.compiler.core.exceptions.WirespecException
import community.flock.wirespec.compiler.core.exceptions.WirespecException.CompilerException.ParserException.WrongTokenException
import community.flock.wirespec.compiler.core.tokenize.types.*
import community.flock.wirespec.compiler.core.tokenize.types.Arrow
import community.flock.wirespec.compiler.core.tokenize.types.Brackets
import community.flock.wirespec.compiler.core.tokenize.types.Colon
import community.flock.wirespec.compiler.core.tokenize.types.CustomType
import community.flock.wirespec.compiler.core.tokenize.types.CustomValue
import community.flock.wirespec.compiler.core.tokenize.types.ForwardSlash
import community.flock.wirespec.compiler.core.tokenize.types.Hash
import community.flock.wirespec.compiler.core.tokenize.types.LeftCurly
import community.flock.wirespec.compiler.core.tokenize.types.Method
import community.flock.wirespec.compiler.core.tokenize.types.Path
import community.flock.wirespec.compiler.core.tokenize.types.QuestionMark
import community.flock.wirespec.compiler.core.tokenize.types.RightCurly
import community.flock.wirespec.compiler.core.tokenize.types.StatusCode
import community.flock.wirespec.compiler.core.tokenize.types.WirespecType
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsString
import community.flock.wirespec.compiler.core.tokenize.types.WsUnit
import community.flock.wirespec.compiler.core.tokenize.types.WsBytes
import community.flock.wirespec.compiler.utils.Logger

class EndpointParser(logger: Logger) : AbstractParser(logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ sealed interface Reference : Value<String> {

data class Primitive(
val type: Type,
val origin: String? = null,
override val isIterable: Boolean = false,
override val isDictionary: Boolean = false
) : Reference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ import arrow.core.Either
import arrow.core.raise.either
import community.flock.wirespec.compiler.core.exceptions.WirespecException
import community.flock.wirespec.compiler.core.exceptions.WirespecException.CompilerException.ParserException.WrongTokenException
import community.flock.wirespec.compiler.core.tokenize.types.*
import community.flock.wirespec.compiler.core.tokenize.types.Brackets
import community.flock.wirespec.compiler.core.tokenize.types.Colon
import community.flock.wirespec.compiler.core.tokenize.types.Comma
import community.flock.wirespec.compiler.core.tokenize.types.CustomType
import community.flock.wirespec.compiler.core.tokenize.types.CustomValue
import community.flock.wirespec.compiler.core.tokenize.types.EndOfProgram
import community.flock.wirespec.compiler.core.tokenize.types.Equals
import community.flock.wirespec.compiler.core.tokenize.types.ForwardSlash
import community.flock.wirespec.compiler.core.tokenize.types.LeftCurly
import community.flock.wirespec.compiler.core.tokenize.types.Pipe
import community.flock.wirespec.compiler.core.tokenize.types.QuestionMark
import community.flock.wirespec.compiler.core.tokenize.types.RightCurly
import community.flock.wirespec.compiler.core.tokenize.types.TypeDefinitionStart
import community.flock.wirespec.compiler.core.tokenize.types.WirespecDefinition
import community.flock.wirespec.compiler.core.tokenize.types.WirespecType
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsString
import community.flock.wirespec.compiler.core.tokenize.types.WsUnit
import community.flock.wirespec.compiler.core.tokenize.types.WsBytes
import community.flock.wirespec.compiler.utils.Logger

class TypeParser(logger: Logger) : AbstractParser(logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import community.flock.wirespec.compiler.core.parse.DefinitionIdentifier
import community.flock.wirespec.compiler.core.parse.Enum
import community.flock.wirespec.compiler.core.parse.Field
import community.flock.wirespec.compiler.core.parse.FieldIdentifier
import community.flock.wirespec.compiler.core.parse.Identifier
import community.flock.wirespec.compiler.core.parse.Reference
import community.flock.wirespec.compiler.core.parse.Type

Expand All @@ -16,11 +15,11 @@ object AvroConverter {
fun AvroModel.TypeList.isNullable() = contains(nullType)
fun AvroModel.SimpleType.toPrimitive() = when (this.value) {
"boolean" -> Reference.Primitive.Type.Boolean
"int" -> Reference.Primitive.Type.Integer
"long" -> Reference.Primitive.Type.Integer
"float" -> Reference.Primitive.Type.Number
"double" -> Reference.Primitive.Type.Number
"bytes" -> Reference.Primitive.Type.String
"int" -> Reference.Primitive.Type.Integer(Reference.Primitive.Type.Precision.P32)
"long" -> Reference.Primitive.Type.Integer(Reference.Primitive.Type.Precision.P64)
"float" -> Reference.Primitive.Type.Number(Reference.Primitive.Type.Precision.P32)
"double" -> Reference.Primitive.Type.Number(Reference.Primitive.Type.Precision.P64)
"bytes" -> Reference.Primitive.Type.Bytes
"string" -> Reference.Primitive.Type.String
else -> TODO("primitive not mapped ${this.value}")
}
Expand All @@ -34,7 +33,6 @@ object AvroConverter {

"boolean", "int", "long", "float", "double", "bytes", "string" -> Reference.Primitive(
type = toPrimitive(),
origin = value,
isIterable = isIterable,
isDictionary = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ object AvroEmitter {

fun Reference.emit(ast:AST, hasEmitted: MutableList<String>): AvroModel.Type = when (val ref = this) {
is Reference.Primitive -> {
val origin = ref.origin?.let { AvroModel.SimpleType(it) }
val type = when(ref.type){
Reference.Primitive.Type.String-> AvroModel.SimpleType("string")
Reference.Primitive.Type.Integer -> AvroModel.SimpleType("int")
Reference.Primitive.Type.Number -> AvroModel.SimpleType("float")
Reference.Primitive.Type.Boolean -> AvroModel.SimpleType("boolean")
when(val type = ref.type){
is Reference.Primitive.Type.String-> AvroModel.SimpleType("string")
is Reference.Primitive.Type.Integer -> when(type.precision){
Reference.Primitive.Type.Precision.P32 -> AvroModel.SimpleType("int")
Reference.Primitive.Type.Precision.P64 -> AvroModel.SimpleType("long")
}
is Reference.Primitive.Type.Number -> when(type.precision){
Reference.Primitive.Type.Precision.P32 -> AvroModel.SimpleType("float")
Reference.Primitive.Type.Precision.P64 -> AvroModel.SimpleType("double")
}
is Reference.Primitive.Type.Boolean -> AvroModel.SimpleType("boolean")
is Reference.Primitive.Type.Bytes -> AvroModel.SimpleType("bytes")
}
origin ?: type
}
is Reference.Custom -> {
when (val def = ast.findType(ref.value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.encodeCollection
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

object AvroModel {
@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package community.flock.wirespec.converter.avro
import community.flock.wirespec.compiler.core.parse.AST
import community.flock.wirespec.compiler.core.parse.Channel
import community.flock.wirespec.compiler.core.parse.DefinitionIdentifier
import community.flock.wirespec.compiler.core.parse.FieldIdentifier
import community.flock.wirespec.compiler.core.parse.Identifier
import community.flock.wirespec.compiler.core.parse.Reference
import community.flock.wirespec.converter.avro.AvroConverter.flatten
import kotlinx.serialization.json.Json
Expand Down
Loading

0 comments on commit f43ac6f

Please sign in to comment.