-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create unified json format for ObjectId (#6413)
* Create unified json format for ObjectId * remove unused imports Co-authored-by: leowe <[email protected]> Co-authored-by: Norman Rzepka <[email protected]>
- Loading branch information
1 parent
fa787c1
commit cc5b62c
Showing
9 changed files
with
49 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package utils | ||
|
||
import com.scalableminds.util.tools.{Fox, FoxImplicits} | ||
import play.api.libs.json._ | ||
import reactivemongo.bson.BSONObjectID | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
case class ObjectId(id: String) { | ||
override def toString: String = id | ||
} | ||
|
||
object ObjectId extends FoxImplicits { | ||
def generate: ObjectId = fromBsonId(BSONObjectID.generate) | ||
def fromString(input: String)(implicit ec: ExecutionContext): Fox[ObjectId] = | ||
fromStringSync(input).toFox ?~> s"The passed resource id ‘$input’ is invalid" | ||
private def fromBsonId(bson: BSONObjectID) = ObjectId(bson.stringify) | ||
private def fromStringSync(input: String) = BSONObjectID.parse(input).map(fromBsonId).toOption | ||
def dummyId: ObjectId = ObjectId("dummyObjectId") | ||
|
||
implicit object ObjectIdFormat extends Format[ObjectId] { | ||
override def reads(json: JsValue): JsResult[ObjectId] = | ||
json.validate[String].flatMap { idString => | ||
val parsedOpt = fromStringSync(idString) | ||
parsedOpt match { | ||
case Some(parsed) => JsSuccess(parsed) | ||
case None => JsError(f"bsonid.invalid: $idString") | ||
} | ||
} | ||
|
||
override def writes(o: ObjectId): JsValue = JsString(o.id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters