-
Notifications
You must be signed in to change notification settings - Fork 4
/
OrientFormat.scala
97 lines (62 loc) · 3.7 KB
/
OrientFormat.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package oriented
import java.util.Date
import cats.data.Reader
import com.tinkerpop.blueprints.impls.orient.OrientElement
import oriented.free.dsl._
import oriented.free.interpreters.{ReadInterpreter, ReadMapInterpreter}
import oriented.syntax.OrientRead
/**
* OrientFormat typeclass makes it able to transform from and to OrientElements from a specific model A.
*/
trait OrientFormat[A] {
lazy val reader: Reader[OrientElement, A] = read.foldMapUnsafe(ReadInterpreter)
lazy val readerMap: Reader[Map[String, Any], A] = read.foldMapUnsafe(ReadMapInterpreter)
/**
* Formats an OrientElement to the model of type A
*/
def read: OrientRead[A]
/**
* The name of the Model (class name)
*/
def name: String
/**
* A Map of properties where each name of the property of the class is the String and Any is the value.
* TODO: Change to Shapeless implementation
*/
def properties(model: A): Map[String, Any]
private val element: Reads[ReadDSL] = Reads.reads[ReadDSL]
def read[R](r: R): OrientRead[R] = element.read(r)
def read[T](fieldName: String)(implicit orientFormat: OrientFormat[T]): OrientRead[T] = element.readEmbedded(fieldName, orientFormat)
def readList[T](fieldName: String)(implicit orientFormat: OrientFormat[T]): OrientRead[List[T]] = element.readList(fieldName, orientFormat)
def readListOpt[T](fieldName: String)(implicit orientFormat: OrientFormat[T]): OrientRead[Option[List[T]]] = element.readListOpt(fieldName, orientFormat)
def readBoolean(fieldName: String): OrientRead[Boolean] = element.readBoolean(fieldName)
def readBooleanOpt(fieldName: String): OrientRead[Option[Boolean]] = element.readBooleanOpt(fieldName)
def readInt(fieldName: String): OrientRead[Int] = element.readInt(fieldName)
def readIntOpt(fieldName: String): OrientRead[Option[Int]] = element.readIntOpt(fieldName)
def readShort(fieldName: String): OrientRead[Short] = element.readShort(fieldName)
def readShortOpt(fieldName: String): OrientRead[Option[Short]] = element.readShortOpt(fieldName)
def readLong(fieldName: String): OrientRead[Long] = element.readLong(fieldName)
def readLongOpt(fieldName: String): OrientRead[Option[Long]] = element.readLongOpt(fieldName)
def readFloat(fieldName: String): OrientRead[Float] = element.readFloat(fieldName)
def readFloatOpt(fieldName: String): OrientRead[Option[Float]] = element.readFloatOpt(fieldName)
def readDouble(fieldName: String): OrientRead[Double] = element.readDouble(fieldName)
def readDoubleOpt(fieldName: String): OrientRead[Option[Double]] = element.readDoubleOpt(fieldName)
def readDatetime(fieldName: String): OrientRead[Date] = element.readDatetime(fieldName)
def readDatetimeOpt(fieldName: String): OrientRead[Option[Date]] = element.readDatetimeOpt(fieldName)
def readString(fieldName: String): OrientRead[String] = element.readString(fieldName)
def readStringOpt(fieldName: String): OrientRead[Option[String]] = element.readStringOpt(fieldName)
def readBinary(fieldName: String): OrientRead[List[Byte]] = element.readBinary(fieldName)
def readBigDecimal(fieldName: String): OrientRead[BigDecimal] = element.readBigDecimal(fieldName)
def readBigDecimalOpt(fieldName: String): OrientRead[Option[BigDecimal]] = element.readBigDecimalOpt(fieldName)
// for {
// id <- read[Int]
// name <- read[String]
// birthday <- read[Date]
// yield User(id, name, birthday)
// case class User(id: Int, name: String, birthday: Date)
// name: "User"
// nameTypes: "id" -> Int, "name" -> String, "birthday" -> Date
// values= User(1, "Thomas", Date("...")) -> 1, "Thomas", Date(...)
// properties = nameTypes.map(_._1) zip values
// format = nameTypes.map(case (n, t) -> element.getProperty[t](n)) ==> User
}