Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Dec 17, 2024
1 parent 82c84da commit 8565dd9
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 24 deletions.
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 @@ -16,11 +16,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 +34,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 @@ -41,7 +41,7 @@ object OpenApiV2Emitter: Emitter(noLogger) {

override fun emit(type: Type, ast: AST) = notYetImplemented()

override fun emit(enum: Enum) = notYetImplemented()
override fun emit(enum: Enum, ast: AST) = notYetImplemented()

override fun emit(refined: Refined) = notYetImplemented()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ object OpenApiV2Parser {
schema.enum != null -> Reference.Custom(name, false, schema.additionalProperties != null)
else -> when (val type = schema.type) {
OpenapiType.STRING, OpenapiType.INTEGER, OpenapiType.NUMBER, OpenapiType.BOOLEAN ->
Reference.Primitive(type.toPrimitive(schema.format), null, false, schema.additionalProperties != null)
Reference.Primitive(type.toPrimitive(schema.format), false, schema.additionalProperties != null)

null, OpenapiType.OBJECT ->
when {
Expand Down Expand Up @@ -518,7 +518,7 @@ object OpenApiV2Parser {
?.let {
Endpoint.Segment.Param(
FieldIdentifier(param),
Reference.Primitive(it, null, false)
Reference.Primitive(it, false)
)
}
?: error(" Declared path parameter $param needs to be defined as a path parameter in path or operation level")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object OpenApiV3Emitter : Emitter(noLogger) {

override fun emit(type: Type, ast: AST) = notYetImplemented()

override fun emit(enum: Enum) = notYetImplemented()
override fun emit(enum: Enum, ast: AST) = notYetImplemented()

override fun emit(refined: Refined) = notYetImplemented()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ object OpenApiV3Parser {
else -> when (val type = schema.type) {
OpenapiType.STRING, OpenapiType.NUMBER, OpenapiType.INTEGER, OpenapiType.BOOLEAN -> Reference.Primitive(
type.toPrimitive(schema.format),
null,
false,
schema.additionalProperties != null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class AvroJavaEmitter(packageName: String, logger: Logger) : JavaEmitter(package
else -> "record.put(${index}, ${field.reference.emit()}.Avro.to(data.${emit(field.identifier)}()));"
}

is Reference.Primitive -> when {
reference.origin == "bytes" -> "record.put(${index}, java.nio.ByteBuffer.wrap(data.${emit(field.identifier)}().getBytes()));"
is Reference.Primitive -> when(reference.type) {
is Reference.Primitive.Type.Bytes -> "record.put(${index}, java.nio.ByteBuffer.wrap(data.${emit(field.identifier)}().getBytes()));"
else -> "record.put(${index}, data.${emit(field.identifier)}()${if (field.isNullable) ".orElse(null)" else ""});"
}

Expand All @@ -117,7 +117,7 @@ class AvroJavaEmitter(packageName: String, logger: Logger) : JavaEmitter(package

is Reference.Primitive -> when {
field.isNullable -> "(${field.emitType()}) java.util.Optional.ofNullable((${field.reference.emitType()}) record.get(${index}))"
reference.origin == "bytes" -> "(${field.emitType()}) new String(((java.nio.ByteBuffer) record.get(${index})).array())"
reference.type == Reference.Primitive.Type.Bytes -> "(${field.emitType()}) new String(((java.nio.ByteBuffer) record.get(${index})).array())"
reference.type == Reference.Primitive.Type.String -> "(${field.emitType()}) record.get(${index}).toString()"
else -> "(${field.emitType()}) record.get(${index})"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AvroKotlinEmitter(val packageName: String, logger: Logger) : KotlinEmitter
}

is Reference.Primitive -> when {
reference.origin == "bytes" -> "record.put(${index}, java.nio.ByteBuffer.wrap(model.${emit(field.identifier)}.toByteArray()));"
reference.type == Reference.Primitive.Type.Bytes -> "record.put(${index}, java.nio.ByteBuffer.wrap(model.${emit(field.identifier)}.toByteArray()));"
else -> "record.put(${index}, model.${emit(field.identifier)});"
}

Expand All @@ -116,7 +116,7 @@ class AvroKotlinEmitter(val packageName: String, logger: Logger) : KotlinEmitter
}

is Reference.Primitive -> when {
reference.origin == "bytes" -> "String((record.get(${index}) as java.nio.ByteBuffer).array())"
reference.type == Reference.Primitive.Type.Bytes -> "String((record.get(${index}) as java.nio.ByteBuffer).array())"
reference.type == Reference.Primitive.Type.String -> "record.get(${index}).toString() as ${field.emitType()}"
else -> "record.get(${index}) as ${field.emitType()}"
}
Expand Down

0 comments on commit 8565dd9

Please sign in to comment.