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

clean up #32

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.austek.plugin
package com.github.austek.pact

import au.com.dius.pact.core.model.matchingrules.MatchingRule
import au.com.dius.pact.core.model.matchingrules.expressions.MatchingRuleDefinition
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.github.austek.plugin.avro

object AvroPactTestConstants {
object AvroPactConstants {
val RecordName = "record-name"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.austek.plugin.avro

import au.com.dius.pact.core.model.matchingrules.{MatchingRule, MatchingRuleCategory}
import com.github.austek.plugin.RuleParser.parseRules
import com.github.austek.pact.RuleParser.parseRules
import com.github.austek.plugin.avro.AvroPluginConstants.MatchingRuleCategoryName
import com.github.austek.plugin.avro.error._
import com.github.austek.plugin.avro.utils.StringUtils._
Expand Down Expand Up @@ -64,13 +64,14 @@ object Avro {
inValue: Value,
appendPath: Boolean = true
): Either[Seq[PluginError[_]], AvroValue] = {
val path = if (appendPath) rootPath :+ fieldName else rootPath
logger.debug(s">>> buildFieldValue($path, $fieldName, $inValue)")
val valueSchema = schema.getType match {
def valueSchema = schema.getType match {
case ARRAY => schema.getElementType
case MAP => schema.getValueType
case _ => schema
}

val path = if (appendPath) rootPath :+ fieldName else rootPath
logger.debug(s">>> buildFieldValue($path, $fieldName, $inValue)")
inValue.kind match {
case Empty => Right(AvroNull(path, fieldName))
case NullValue(_) => Right(AvroNull(path, fieldName))
Expand All @@ -81,13 +82,9 @@ object Avro {
}
.left
.map(e => Seq(e))
case ListValue(_) => Left(Seq(PluginErrorMessage(s"List kind value for field is not supported")))
case NumberValue(_) => Left(Seq(PluginErrorMessage(s"Number kind value for field is not supported")))
case BoolValue(_) => Left(Seq(PluginErrorMessage(s"Bool kind value for field is not supported")))
case StructValue(_) if valueSchema.getType == RECORD =>
AvroRecord(path, fieldName, valueSchema, inValue.getStructValue.fields)
case StructValue(_) =>
Left(Seq(PluginErrorMessage(s"Struct kind value for field is not supported")))
case _ => Left(Seq(PluginErrorMessage(s"${inValue.kind.getClass.getSimpleName} kind value for field is not supported")))
}
}

Expand All @@ -103,18 +100,18 @@ object Avro {
case _ =>
(schemaType match {
case BOOLEAN => Try(AvroBoolean(path, fieldName, fieldValue.asInstanceOf[Boolean], rules)).toEither
case DOUBLE => Try(AvroDouble(path, fieldName, fieldValue.asInstanceOf[Double], rules)).toEither
case ENUM => Try(AvroEnum(path, fieldName, fieldValue.asInstanceOf[String])).toEither
case FLOAT => Try(AvroFloat(path, fieldName, fieldValue.asInstanceOf[Float], rules)).toEither
case INT => Try(AvroInt(path, fieldName, fieldValue.asInstanceOf[Int], rules)).toEither
case LONG => Try(AvroLong(path, fieldName, fieldValue.asInstanceOf[Long], rules)).toEither
case NULL => Right(AvroNull(path, fieldName))
case STRING => Try(AvroString(path, fieldName, fieldValue.asInstanceOf[String], rules)).toEither
case BYTES | FIXED =>
Right(
AvroString(path, fieldName, new String(fieldValue.asInstanceOf[Array[Byte]], StandardCharsets.UTF_8), rules)
)
case DOUBLE => Try(AvroDouble(path, fieldName, fieldValue.asInstanceOf[Double], rules)).toEither
case ENUM => Try(AvroEnum(path, fieldName, fieldValue.asInstanceOf[String])).toEither
case FLOAT => Try(AvroFloat(path, fieldName, fieldValue.asInstanceOf[Float], rules)).toEither
case INT => Try(AvroInt(path, fieldName, fieldValue.asInstanceOf[Int], rules)).toEither
case LONG => Try(AvroLong(path, fieldName, fieldValue.asInstanceOf[Long], rules)).toEither
case NULL => Right(AvroNull(path, fieldName))
case STRING => Try(AvroString(path, fieldName, fieldValue.asInstanceOf[String], rules)).toEither
case t => Left(FieldUnsupportedTypeException(t, fieldName, fieldValue))
case t => Left(FieldUnsupportedTypeException(t, fieldName, fieldValue))
}).left.map(e => PluginErrorException(e))
}
}
Expand Down Expand Up @@ -348,14 +345,10 @@ object Avro {
}
.asJava
)
case RECORD =>
record.put(key, value.asInstanceOf[AvroRecord].toGenericRecord(fieldSchema))
case FIXED =>
record.put(key, new GenericData.Fixed(fieldSchema, value.asInstanceOf[String].getBytes))
case UNION =>
fieldToRecord(record, key, value, fieldSchema.getTypes.asScala.filterNot(_.getType == NULL).head)
case _ =>
record.put(key, value)
case RECORD => record.put(key, value.asInstanceOf[AvroRecord].toGenericRecord(fieldSchema))
case FIXED => record.put(key, new GenericData.Fixed(fieldSchema, value.asInstanceOf[String].getBytes))
case UNION => fieldToRecord(record, key, value, fieldSchema.getTypes.asScala.filterNot(_.getType == NULL).head)
case _ => record.put(key, value)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.austek.plugin.avro

import com.github.austek.plugin.avro.AvroPactTestConstants._
import com.github.austek.plugin.avro.AvroPactConstants._
import com.github.austek.plugin.avro.AvroPluginConstants._
import com.github.austek.plugin.avro.ContentTypeConstants._
import com.github.austek.plugin.avro.compare.CompareContentsResponseBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@ object InteractionBuilder extends StrictLogging {
configuration: Struct
): Either[Seq[PluginError[_]], InteractionResponse] = {
schema.getType match {
case UNION =>
schema.getTypes.asScala.find(s => s.getType == RECORD && s.getName == recordName) match {
case Some(value) => buildResponse(value, recordName, avroSchemaHash, configuration)
case None => Left(Seq(PluginErrorMessage(s"Avro union schema didn't contain record: '$recordName'")))
}
case RECORD if schema.getName == recordName =>
buildResponse(schema, recordName, avroSchemaHash, configuration)
case RECORD if schema.getName != recordName =>
Left(Seq(PluginErrorMessage(s"Record '$recordName' was not found in avro Schema provided")))
case t =>
Left(Seq(PluginErrorMessage(s"Schema provided is of type: '$t', but expected to be ${UNION.getName}/${RECORD.getName}")))
case UNION => buildUnionResponse(schema, recordName, avroSchemaHash, configuration)
case RECORD if schema.getName == recordName => buildRecordResponse(schema, recordName, avroSchemaHash, configuration)
case RECORD if schema.getName != recordName => Left(Seq(PluginErrorMessage(s"Record '$recordName' was not found in avro Schema provided")))
case t => Left(Seq(PluginErrorMessage(s"Schema provided is of type: '$t', but expected to be ${UNION.getName}/${RECORD.getName}")))
}
}

private def buildResponse(
private def buildUnionResponse(schema: Schema, recordName: String, avroSchemaHash: AvroSchemaBase16Hash, configuration: Struct) =
schema.getTypes.asScala.find(s => s.getType == RECORD && s.getName == recordName) match {
case Some(value) => buildRecordResponse(value, recordName, avroSchemaHash, configuration)
case None => Left(Seq(PluginErrorMessage(s"Avro union schema didn't contain record: '$recordName'")))
}

private def buildRecordResponse(
schema: Schema,
recordName: String,
avroSchemaHash: AvroSchemaBase16Hash,
Expand Down