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

refactor to prefer generics and remove useless types #77

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

massimosiani
Copy link
Contributor

No description provided.

@massimosiani massimosiani requested review from simoexpo and lodamar March 3, 2020 09:57
case jsObject: JsObject => jsObjectToResponsePayload(jsObject)
case json => List(Left(ErrorMessageJson("", "", json.compactPrint, Instant.now)))
}

implicit def unmarshallerFrom[A](rf: RootJsonReader[A]): Unmarshaller[String, A] =
_fromStringUnmarshallerFromByteStringUnmarshaller(sprayJsonByteStringUnmarshaller(rf))

private def jsObjectToResponsePayload(json: JsObject): List[Either[ErrorMessageJson, FlightStateJson]] =
private def jsObjectToResponsePayload[A <: FlightStateJson](json: JsObject): List[Either[ErrorMessageJson, A]] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're converting to a concrete FlightStateJson why use a generic with a cast?
We'll always return List[Either[ErrorMessageJson, FlightStateJson]]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the type parameter from openSkyResponsePayloadJsonFormat, can you write a generic version of collectRightMessagesGraph?

Copy link
Member

@lodamar lodamar Mar 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any problem but I'll double check as soon as I can

).toEither.left.map(ex => ErrorMessageJson("", ex.getMessage, json.compactPrint, Instant.now))
}
}

private def jsArrayToResponsePayload(json: JsArray): List[Either[ErrorMessageJson, MessageJson]] =
private def jsArrayToResponsePayload[A <: MessageJson](json: JsArray): List[Either[ErrorMessageJson, A]] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above we don't need to be generic and we can avoid all casts

@@ -21,8 +21,8 @@ object SideStreamContext {
new KafkaSinkFactory[MonitoringMessageJson, Key, Monitoring.Value](kafkaConfig.monitoringTopic, producerSettings).sink
}

def invalidSink(kafkaConfig: KafkaConfig)(implicit system: ActorSystem): Sink[MessageJson, Future[Done]] = {
def invalidSink[A](kafkaConfig: KafkaConfig)(implicit system: ActorSystem): Sink[A, Future[Done]] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sink is used only for FlightMessageJson could we avoid the A type parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above. I tried but then you bind a type of the corresponding graph to FlightMessageJson everywhere. Did you try yourself?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can rewrite the mainGraph with:

val collectInvalidFlight = builder.add(
          Flow
            .fromFunction[Either[ErrorMessageJson, Message], Option[FlightMessageJson]] {
              case Right(x: FlightMessageJson) => Some(x)
              case _                           => None
            }
            .collect { case Some(x) => x }
        )

or something like that. Since the sink is specific for the flight I think is not completely wrong to keep only the FlightMessageJson in this collectInvalidFlight (new name for collectInvalid).

Probably the real problem here is that we are putting a logic specific for FlightMessageJson in a graph used by all type of messages :(

@@ -26,20 +26,20 @@ object JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val cityMessageJsonFormat: RootJsonFormat[CityMessageJson] = jsonFormat6(CityMessageJson.apply)
implicit val flightStatesJsonFormat: RootJsonFormat[FlightStatesJson] = jsonFormat2(FlightStatesJson.apply)

val aviationEdgePayloadJsonReader: RootJsonReader[List[Either[ErrorMessageJson, MessageJson]]] = {
def aviationEdgePayloadJsonReader[A]: RootJsonReader[List[Either[ErrorMessageJson, A]]] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we can add a JsonReader context bound so we also rewrite jsArrayToResponsePayload with a Try(json.convertTo[A])? The only draw back is that probably we have to provide the JsonReader directly when calling runAviationEdgeStream or in the ApiProviderStreamContext. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. We may provide the JsonReader for A and let some spray implicit do the dirty work. Let me check whether some implicits exist.

case jsArray: JsArray => jsArrayToResponsePayload(jsArray)
case json => List(Left(ErrorMessageJson("", "", json.compactPrint, Instant.now)))
}

val openSkyResponsePayloadJsonFormat: RootJsonReader[List[Either[ErrorMessageJson, MessageJson]]] = {
def openSkyResponsePayloadJsonFormat[A]: RootJsonReader[List[Either[ErrorMessageJson, A]]] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same proposal here (JsonReader context bound)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants