Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Forked from zeryx/nope.scala
Last active January 5, 2017 23:50
Show Gist options
  • Save Jacoby6000/fb96067591e2ae0b26a8c832e0db01cc to your computer and use it in GitHub Desktop.
Save Jacoby6000/fb96067591e2ae0b26a8c832e0db01cc to your computer and use it in GitHub Desktop.
package algorithmia.TechSupport
import algorithmia.TechSupport.Classes.DataPoint
import argonaut.Argonaut._
import argonaut.CodecJson
import argonaut._
/**
* Created by james on 05/01/17.
*/
sealed trait Input
//TODO: Learn how implicits and type inference work, because this is some weird shit.
//also talk to Jacoby6000 on irc.
object Input {
implicit def inputCodec: CodecJson[Input] = {
def encoder(data: Input): Json =
data match {
case train: Train => Train.trainCodec.encode(train)
case predict: Predict => Predict.predictCodec.encode(predict)
}
def decoder(json: HCursor): DecodeResult[Input] = {
val result =
for {
_ <- Predict.predictCodec.decode(json).map[Input](a => a).result.flip
lastOne <- Train.trainCodec.decode(json).map[Input](a => a).result.flip
} yield lastOne
result.fold(DecodeResult.ok(_), DecodeResult.fail(_, _).tupled
}
CodecJson[Input](encoder, decoder)
}
}
case class Train(labelData: Option[List[DataPoint]], labelFile: Option[String], namespace: Option[String]) extends Input
case class Predict(text: String, namespace: Option[String]) extends Input
object Train{
implicit def trainCodec: CodecJson[Train] =
casecodec3(Train.apply, Train.unapply)(
"labelData",
"labelFile",
"namespace"
)
}
object Predict{
implicit def predictCodec: CodecJson[Predict] =
casecodec2(Predict.apply, Predict.unapply)(
"text",
"namespace"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment