Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue 1067 - Add support for BinaryField in MongoDB

  • Loading branch information...
commit de697c6759be1388ddc705aca71e9ee6d0fcbc06 1 parent 9f0fe6a
@eltimn eltimn authored
View
1  persistence/mongodb-record/src/main/scala/net/liftweb/mongodb/record/BsonRecord.scala
@@ -99,6 +99,7 @@ trait BsonMetaRecord[BaseRecord <: BsonRecord[BaseRecord]] extends MetaRecord[Ba
case x if mongotype_?(x.getClass) => dbo.add(f.name, x)
case x if datetype_?(x.getClass) => dbo.add(f.name, datetype2dbovalue(x))
case x: BsonRecord[_] => dbo.add(f.name, x.asDBObject)
+ case x: Array[Byte] => dbo.add(f.name, x)
case o => dbo.add(f.name, o.toString)
})
}
View
57 persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/Fixtures.scala
@@ -68,10 +68,6 @@ trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
class FieldTypeTestRecord private () extends MongoRecord[FieldTypeTestRecord] with ObjectIdPk[FieldTypeTestRecord] {
def meta = FieldTypeTestRecord
- object mandatoryBinaryField extends BinaryField(this)
- object legacyOptionalBinaryField extends BinaryField(this) { override def optional_? = true }
- object optionalBinaryField extends OptionalBinaryField(this)
-
object mandatoryBooleanField extends BooleanField(this)
object legacyOptionalBooleanField extends BooleanField(this) { override def optional_? = true }
object optionalBooleanField extends OptionalBooleanField(this)
@@ -135,7 +131,6 @@ class FieldTypeTestRecord private () extends MongoRecord[FieldTypeTestRecord] wi
override def equals(other: Any): Boolean = other match {
case that: FieldTypeTestRecord =>
this.id.value == that.id.value &&
- //this.mandatoryBinaryField.value == that.mandatoryBinaryField.value &&
this.mandatoryBooleanField.value == that.mandatoryBooleanField.value &&
this.mandatoryCountryField.value == that.mandatoryCountryField.value &&
this.mandatoryDecimalField.value == that.mandatoryDecimalField.value &&
@@ -153,10 +148,56 @@ class FieldTypeTestRecord private () extends MongoRecord[FieldTypeTestRecord] wi
}
}
-case class MongoCaseClassTestObject(intField: Int, stringField: String)
-
object FieldTypeTestRecord extends FieldTypeTestRecord with MongoMetaRecord[FieldTypeTestRecord]
+class BinaryFieldTestRecord extends MongoRecord[BinaryFieldTestRecord] with IntPk[BinaryFieldTestRecord] {
+ def meta = BinaryFieldTestRecord
+
+ object mandatoryBinaryField extends BinaryField(this) {
+ // compare the elements of the Array
+ override def equals(other: Any): Boolean = other match {
+ case that: BinaryField[Any] =>
+ this.value.zip(that.value).filter(t => t._1 != t._2).length == 0
+ case _ => false
+ }
+ }
+ object legacyOptionalBinaryField extends BinaryField(this) {
+ override def optional_? = true
+ // compare the elements of the Array
+ override def equals(other: Any): Boolean = other match {
+ case that: BinaryField[Any] => (this.valueBox, that.valueBox) match {
+ case (Empty, Empty) => true
+ case (Full(a), Full(b)) =>
+ a.zip(b).filter(t => t._1 != t._2).length == 0
+ case _ => false
+ }
+ case _ => false
+ }
+ }
+ object optionalBinaryField extends OptionalBinaryField(this) {
+ // compare the elements of the Array
+ override def equals(other: Any): Boolean = other match {
+ case that: OptionalBinaryField[Any] => (this.valueBox, that.valueBox) match {
+ case (Empty, Empty) => true
+ case (Full(a), Full(b)) =>
+ a.zip(b).filter(t => t._1 != t._2).length == 0
+ case _ => false
+ }
+ case _ => false
+ }
+ }
+
+ override def equals(other: Any): Boolean = other match {
+ case that:BinaryFieldTestRecord =>
+ this.id.value == that.id.value &&
+ this.mandatoryBinaryField == that.mandatoryBinaryField &&
+ this.legacyOptionalBinaryField == that.legacyOptionalBinaryField &&
+ this.optionalBinaryField == that.optionalBinaryField
+ case _ => false
+ }
+}
+object BinaryFieldTestRecord extends BinaryFieldTestRecord with MongoMetaRecord[BinaryFieldTestRecord]
+
case class TypeTestJsonObject(
intField: Int,
@@ -221,6 +262,8 @@ class PasswordTestRecord private () extends MongoRecord[PasswordTestRecord] with
}
object PasswordTestRecord extends PasswordTestRecord with MongoMetaRecord[PasswordTestRecord]
+case class MongoCaseClassTestObject(intField: Int, stringField: String)
+
class ListTestRecord private () extends MongoRecord[ListTestRecord] with UUIDPk[ListTestRecord] {
def meta = ListTestRecord
View
14 persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoRecordSpec.scala
@@ -167,8 +167,9 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
"MongoRecord" should {
checkMongoIsRunning
+ val binData: Array[Byte] = Array(18, 19, 20)
+
val fttr = FieldTypeTestRecord.createRecord
- //.mandatoryBinaryField()
.mandatoryBooleanField(false)
.mandatoryCountryField(Countries.USA)
.mandatoryDecimalField(BigDecimal("3.14"))
@@ -183,6 +184,9 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
.mandatoryTextareaField("string")
.mandatoryTimeZoneField("America/Chicago")
+ val bftr = BinaryFieldTestRecord.createRecord
+ .mandatoryBinaryField(binData)
+
val mfttr = MongoFieldTypeTestRecord.createRecord
.mandatoryDateField(new Date)
.mandatoryJsonObjectField(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")))
@@ -328,6 +332,14 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
fttrFromDb foreach { tr =>
tr mustEqual fttr
}
+
+ bftr.save
+
+ val bftrFromDb = BinaryFieldTestRecord.find(bftr.id.value)
+ bftrFromDb must notBeEmpty
+ bftrFromDb foreach { tr =>
+ tr mustEqual bftr
+ }
}
"delete record properly" in {
Please sign in to comment.
Something went wrong with that request. Please try again.