Phoenixを勉強しはじめているのですが、ちょっと疑問に思ってたことに対する答えが見つかったので
// hello_controller.ex
def world(conn, %{"name" => name}) do
render conn, "world.html", name: name
end
パラメータを%{"name" => name}
のようの文字列のキーを使うのが一般的なようなのですが、
他の処理ではAtomとか使っているのだけど、ここだけなんでなんだろう?
Programming Phoenixの中のコラムAtom keys vs. string keys?では以下のように書いてあります。
In the world action in our controllers, the external parameters have string keys, "name" => name, while internally we use name: name. That’s a convention followed throughout Phoenix. External data is unsafe, so we explicitly match on the string keys, and then our application boundaries like controllers and channels will convert them into atoms keys which we will rely on everywhere else inside Phoenix.
英語苦手なので正しく読めてるかわかりませんが、こう理解しました。
Phoenixの内部処理ではAtomを使うが、外部とのやり取りではStringのパラメータの使っているのはデータの安全性を考慮してのこと。
文字列のかわりにAtomを使うとなぜ安全じゃないのか?
ちょうど、stackoverflowに質問している人がいました。
http://stackoverflow.com/questions/34446221/atom-keys-vs-string-keys-in-phoenix
Atomの上限が関係しているんですね。
1048576が上限のようで、そういえばE本で読んだことがあったような気がします。