Skip to content

Instantly share code, notes, and snippets.

@davegurnell
Created October 3, 2014 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davegurnell/64920adc3b1e3e01dbb1 to your computer and use it in GitHub Desktop.
Save davegurnell/64920adc3b1e3e01dbb1 to your computer and use it in GitHub Desktop.
Idea for modelling serialized PHP data in Scala
sealed trait PhpValue {
def \(field: PhpValue): Seq[PhpValue] = Seq.empty[PhpValue]
def \\(field: PhpValue): Seq[PhpValue] = Seq.empty[PhpValue]
}
sealed trait PhpArrayLike extends PhpValue {
def fields: Map[PhpValue, PhpValue]
override def \(field: PhpValue): Seq[PhpValue] =
fields.toSeq.collect {
case (f, v) if f == field => v
}
override def \\(field: PhpValue): Seq[PhpValue] =
fields.toSeq.foldLeft(Seq.empty[PhpValue]) { (accum, pair) =>
pair match {
case (f, v) if f == field => accum ++ (v +: (v \\ field))
case (f, v) => accum ++ (v \\ field)
}
}
}
final case class PhpInt(value: Int) extends PhpValue
final case class PhpDouble(value: Double) extends PhpValue
final case class PhpBoolean(value: Boolean) extends PhpValue
final case class PhpString(value: Array[Byte]) extends PhpValue
final case class PhpArray(fields: Map[PhpValue, PhpValue]) extends PhpArrayLike
final case class PhpObject(className: String, fields: Map[PhpValue, PhpValue]) extends PhpArrayLike
final case object PhpNull extends PhpValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment