From 19ff4d282e8143351a81739ef0c85d5031bc7871 Mon Sep 17 00:00:00 2001 From: austek Date: Sun, 22 Oct 2023 23:09:35 +0100 Subject: [PATCH] clean up --- .../austek/{plugin => pact}/RuleParser.scala | 2 +- ...onstants.scala => AvroPactConstants.scala} | 2 +- .../austek/plugin/avro/AvroRecord.scala | 43 ++++++++----------- .../plugin/avro/PactAvroPluginService.scala | 2 +- .../avro/interaction/InteractionBuilder.scala | 23 +++++----- 5 files changed, 32 insertions(+), 40 deletions(-) rename modules/plugin/src/main/scala/com/github/austek/{plugin => pact}/RuleParser.scala (97%) rename modules/plugin/src/main/scala/com/github/austek/plugin/avro/{AvroPactTestConstants.scala => AvroPactConstants.scala} (70%) diff --git a/modules/plugin/src/main/scala/com/github/austek/plugin/RuleParser.scala b/modules/plugin/src/main/scala/com/github/austek/pact/RuleParser.scala similarity index 97% rename from modules/plugin/src/main/scala/com/github/austek/plugin/RuleParser.scala rename to modules/plugin/src/main/scala/com/github/austek/pact/RuleParser.scala index 1091d23..be6e734 100644 --- a/modules/plugin/src/main/scala/com/github/austek/plugin/RuleParser.scala +++ b/modules/plugin/src/main/scala/com/github/austek/pact/RuleParser.scala @@ -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 diff --git a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroPactTestConstants.scala b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroPactConstants.scala similarity index 70% rename from modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroPactTestConstants.scala rename to modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroPactConstants.scala index 6d6bc27..f3c9829 100644 --- a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroPactTestConstants.scala +++ b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroPactConstants.scala @@ -1,5 +1,5 @@ package com.github.austek.plugin.avro -object AvroPactTestConstants { +object AvroPactConstants { val RecordName = "record-name" } diff --git a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroRecord.scala b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroRecord.scala index 1a30619..8f200ed 100644 --- a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroRecord.scala +++ b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/AvroRecord.scala @@ -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._ @@ -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)) @@ -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"))) } } @@ -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)) } } @@ -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) } } diff --git a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/PactAvroPluginService.scala b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/PactAvroPluginService.scala index 498a4ff..fe2dc0e 100644 --- a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/PactAvroPluginService.scala +++ b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/PactAvroPluginService.scala @@ -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 diff --git a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/interaction/InteractionBuilder.scala b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/interaction/InteractionBuilder.scala index f277fb4..5830ae6 100644 --- a/modules/plugin/src/main/scala/com/github/austek/plugin/avro/interaction/InteractionBuilder.scala +++ b/modules/plugin/src/main/scala/com/github/austek/plugin/avro/interaction/InteractionBuilder.scala @@ -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,