SlideShare a Scribd company logo
1 of 14
Download to read offline
Monocle
とかいうのがありまして
2015/05/30 Naoki Aoyama (@aoiroaoino)
おまえ誰だよ?
Naoki Aoyama
Twitte/GtiHub: @aoiroaoino
Maverick., inc
Programmer(Scala, JavaScript)
にゃーん
BSONとかいうのがありまして
BSONは主にMongoDBのデータストレージ及びネッ
トワーク転送フォーマットとして利用されている、
データ交換フォーマットである。 単純なデータ構
造や連想配列(MongoDBではオブジェクトまたは
ドキュメントと表す)を示すバイナリ構造であ
り、 名称はJSON由来であり"バイナリ型JSON"の略
語である。(wikipedia - BSON より)
http://mongodb.github.io/mongo-java-driver/3.0/bson/documents/
BSON type - Java type
例えば、こんなBSONがありまして
// bson1
{
"id": 1,
"name": "Aoino",
"address": [
"Tokyo",
"Ibaraki"
]
}
// bson2
[
1,
2,
3
]
BSONデータの作成
(in Java MongoDB Driver)
val bson1 = new BsonDocument()

.append("id", new BsonInt64(1))
.append("name", new BsonString("Aoino"))
.append("address", new BsonArray(Arrays.asList(
new BsonString("Tokyo"),
new BsonString("Ibaraki")))
val bson2 = new BsonArray(Arrays.asList(
new BsonInt32(1),
new BsonInt32(2),
new BsonInt32(3)))
BSONの操作
(in Java MongoDB Driver)
scala> val reader = new BsonDocumentReader(bison1)
reader: org.bson.BsonDocumentReader = org.bson.BsonDocumentReader@2d81357d
scala> reader.readStartDocument()
scala> reader.readName()
res33: String = id
scala> reader.readInt64()
res34: Long = 1
scala> reader.readString() // あっ、readName()忘れた!
res35: String = Aoino
scala> reader.readName()
res36: String = address
scala> reader.readString() // BsonArrayに対してreadString()呼んじゃった!
org.bson.BsonInvalidOperationException: readString can only be called when
CurrentBSONType is STRING, not when CurrentBSONType is ARRAY.
at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:655)
at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:687)
at org.bson.AbstractBsonReader.readString(AbstractBsonReader.java:428)
... 43 elided
チョットつらい…
Prismを定義してみる
object BSONPrism {
type BSONValue = org.bson.BsonValue
type BSONArray = org.bson.BsonArray
type BSONInt32 = org.bson.BsonInt32
type BSONInt64 = …
val bsonArray = Prism[BSONValue, List[BSONValue]]{
case x: BSONArray => Option(x.getValues.toList)
case _ => None
}(new BSONArray(_))
val bsonInt32 = Prism[BSONValue, Int]{
case x: BSONInt32 => Option(x.getValue)
case _ => None
}(new BSONInt32(_))
…
}
BSONの操作 [1] 

(Scala + Monocle)
(bsonDocument composeOptional index("id")
composePrism bsonInt64
).getOption(bson1)
//=> Some(1)
(bsonDocument composeOptional index("address")
composePrism bsonArray
composeOptional index(0)
composePrism bsonString
).modify(_.toLowerCase)(bson1)
//=> { "id" : { "$numberLong" : "1" },
"name" : "Aoino",
"address" : ["tokyo", "Ibaraki"]} ※(1)
BSONの操作 [2] 

(Scala + Monocle)
(bsonArray composeOptional index(1)
composePrism bsonInt32
).set(999)(bson2)
//=> BsonArray{values=[
BsonInt32{value=1},BsonInt32{value=999},BsonInt32{value=3}]}
※(2)
(bsonArray composeTraversal each
composePrism bsonInt32
).modify(_ * 100)(bson2)
//=> BsonArray{values=[
BsonInt32{value=100},BsonInt32{value=200},BsonInt32{value=300}]}
※(3)
https://github.com/julien-truffaut/Monocle/wiki/Release-note-1.0
Alias methods
\もっと短く書ける/
(bsonDocument ^|-? index("address")
^<-? bsonArray
^|-? index(0)
^<-? bsonString)
.modify(_.toLowerCase)(bison1) //=> (1)に同じ
(bsonArray ^|-? index(1)
^<-? bsonInt32
).set(999)(bson) //=> ※(2)に同じ
(bsonArray ^|->> each
^<-? bsonInt32
).modify(_ * 1000)(bson) //=> ※(3)に同じ
まとめ
MonocleでPrismを定義する方法
Prismを使っての操作方法
もしかしてJavaのライブラリ包む時に便利かも?

More Related Content

What's hot

MongoDB〜その性質と利用場面〜
MongoDB〜その性質と利用場面〜MongoDB〜その性質と利用場面〜
MongoDB〜その性質と利用場面〜Naruhiko Ogasawara
 
RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)
RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)
RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)Ryuji Tamagawa
 
Db tech showcase2015 how to replicate between clusters
Db tech showcase2015 how to replicate between clustersDb tech showcase2015 how to replicate between clusters
Db tech showcase2015 how to replicate between clustersHiroaki Kubota
 
URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!treby
 
Beam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタBeam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタKenichi Kambara
 
20130529 ku-librarians勉強会#163:国立国会図書館のILLサービス
20130529 ku-librarians勉強会#163:国立国会図書館のILLサービス20130529 ku-librarians勉強会#163:国立国会図書館のILLサービス
20130529 ku-librarians勉強会#163:国立国会図書館のILLサービスkulibrarians
 
Web Packaging - Use cases and Loading
Web Packaging - Use cases and LoadingWeb Packaging - Use cases and Loading
Web Packaging - Use cases and LoadingKinuko Yasuda
 
Json syntax quiz
Json syntax quizJson syntax quiz
Json syntax quizkomem3
 

What's hot (11)

MongoDB〜その性質と利用場面〜
MongoDB〜その性質と利用場面〜MongoDB〜その性質と利用場面〜
MongoDB〜その性質と利用場面〜
 
RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)
RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)
RDB経験者に送るMongoDBの勘所(db tech showcase tokyo 2013)
 
Db tech showcase2015 how to replicate between clusters
Db tech showcase2015 how to replicate between clustersDb tech showcase2015 how to replicate between clusters
Db tech showcase2015 how to replicate between clusters
 
Groovyでjson
GroovyでjsonGroovyでjson
Groovyでjson
 
初めてのMongo db
初めてのMongo db初めてのMongo db
初めてのMongo db
 
20120525 mt websocket
20120525 mt websocket20120525 mt websocket
20120525 mt websocket
 
URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!
 
Beam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタBeam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタ
 
20130529 ku-librarians勉強会#163:国立国会図書館のILLサービス
20130529 ku-librarians勉強会#163:国立国会図書館のILLサービス20130529 ku-librarians勉強会#163:国立国会図書館のILLサービス
20130529 ku-librarians勉強会#163:国立国会図書館のILLサービス
 
Web Packaging - Use cases and Loading
Web Packaging - Use cases and LoadingWeb Packaging - Use cases and Loading
Web Packaging - Use cases and Loading
 
Json syntax quiz
Json syntax quizJson syntax quiz
Json syntax quiz
 

Viewers also liked

Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料時響 逢坂
 
Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料時響 逢坂
 
Neural Network as a function
Neural Network as a functionNeural Network as a function
Neural Network as a functionTaisuke Oe
 
今から始める Lens/Prism
今から始める Lens/Prism今から始める Lens/Prism
今から始める Lens/PrismNaoki Aoyama
 
Reducing Boilerplate and Combining Effects: A Monad Transformer Example
Reducing Boilerplate and Combining Effects: A Monad Transformer ExampleReducing Boilerplate and Combining Effects: A Monad Transformer Example
Reducing Boilerplate and Combining Effects: A Monad Transformer ExampleConnie Chen
 
Make your programs Free
Make your programs FreeMake your programs Free
Make your programs FreePawel Szulc
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless Jared Roesch
 

Viewers also liked (10)

Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料
 
Beyond Scala Lens
Beyond Scala LensBeyond Scala Lens
Beyond Scala Lens
 
Xpath in-lens
Xpath in-lensXpath in-lens
Xpath in-lens
 
Phantom Type in Scala
Phantom Type in ScalaPhantom Type in Scala
Phantom Type in Scala
 
Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料
 
Neural Network as a function
Neural Network as a functionNeural Network as a function
Neural Network as a function
 
今から始める Lens/Prism
今から始める Lens/Prism今から始める Lens/Prism
今から始める Lens/Prism
 
Reducing Boilerplate and Combining Effects: A Monad Transformer Example
Reducing Boilerplate and Combining Effects: A Monad Transformer ExampleReducing Boilerplate and Combining Effects: A Monad Transformer Example
Reducing Boilerplate and Combining Effects: A Monad Transformer Example
 
Make your programs Free
Make your programs FreeMake your programs Free
Make your programs Free
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless
 

Monocleとかいうのがありまして