Skip to content

Instantly share code, notes, and snippets.

def flattenObject(obj: JsObject, prefix: Option[String] = None) :Seq[(String, JsValue)] = {
obj.fields.toList match {
case ("$date",_) :: Nil if prefix.isDefined => Seq(prefix.get -> obj)
case ("$date",_) :: Nil => throw new Exception("Can't save $date at the first level.")
case fs => fs.flatMap { t =>
val name = prefix.map(_ + ".").getOrElse("") + t._1
t._2 match {
case obj: JsObject => flattenObject(obj, Some(name))
case value => Seq(name -> value)
}
@srenault
srenault / a-json-mongo.scala
Last active December 20, 2015 02:59 — forked from playxamplez-admin/CODE
Flatten #json object for partial update with #ReactiveMongo #play2.1
def flattenObject(obj: JsObject, prefix: Option[String] = None) :Seq[(String, JsValue)] = {
obj.fields.toList match {
case ("$date",_) :: Nil if prefix.isDefined => Seq(prefix.get -> obj)
case ("$date",_) :: Nil => throw new Exception("Can't save $date at the first level.")
case fs => fs.flatMap { t =>
val name = prefix.map(_ + ".").getOrElse("") + t._1
t._2 match {
case obj: JsObject => flattenObject(obj, Some(name))
case value => Seq(name -> value)
}
sealed trait Literal extends Term {
def asOpt[A <: Literal](): Option[A] = {
this match {
case x: A => Some(x)
case _ => None
}
}
}
object Utils {
import scala.concurrent.duration._
import play.api.libs.concurrent.Promise
import play.api.libs.iteratee.{ Enumerator, Iteratee }
def sequence[A, B](seq: Seq[A])(f: A => Future[B]): Future[Seq[B]] = {
val enumerator = Enumerator[A](seq: _*)
val consummer = Iteratee.foldM[A, Seq[B]](Seq.empty[B]) {
case (acc, i) => f(i).map(t => t +: acc)
gulp.task('watch', ['compile'], function() {
var assets = Assets.ts.src.files.concat(Assets.styl.src.files);
gulp.src(assets)
.pipe(watch(assets))
.pipe(plumber())
.pipe(exec('tarifa build web', {
pipeStdout: true
}))
.pipe(exec.reporter({
err: true,
@srenault
srenault / mithril.ts
Last active August 29, 2015 14:08
mithril 'propMap'
function propMap<T>(values: Array<[string, T]>): (key: string, value?: T) => T {
var _prop = (map?: Map<string, T>) => {
var prop = (key: string, value?: T) => {
if (value !== undefined) map.set(key, value);
return map.get(key);
}
return prop;
}
var map = values.reduce((acc, pair) => {
@srenault
srenault / data.json
Last active August 29, 2015 14:09
prismic.io mask
{
"tags": ["toto"],
"type": "all",
"Group": [
{
"color": "#d85cb8"
},
{
"color": "#39ede7"
}
@srenault
srenault / api.json
Last active August 29, 2015 14:09
api.json
{
"license": "All Rights Reserved",
"version": "5541036",
"results": [
{
"data": {
"all": {
"Group": {
"value": [
{
val json = Json.obj("users" -> Json.arr(Json.obj("name" -> "martin"), Json.obj("name" -> "dupont")))
val path = ((__ \ "users")(1) \ "name")
__.json.update(path.json.put(JsString("toto"))).reads(json)
int main(int argc, char **argv) {
google::protobuf::Arena arena;
std::ifstream in("<path>");
m::cheminot::data::Graph* graphBuf = google::protobuf::Arena::CreateMessage<m::cheminot::data::Graph>(&arena);
graphBuf->ParseFromIstream(&in);
in.close();
}