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 is a small example of how to use Play! Json to validate a JsValue, when you need to consider a combination | |
* of fields. In this example, I have ContactInfo, where either a phone number or an email address have to provided | |
* (or both). Each field is represented as a simple String, when in reality you may want to do further validation | |
* on each field to make sure it is a valid phone number and/or email. | |
*/ | |
import play.api.libs.json._ | |
object Main { |
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 is an example of how to express Infinity in Json using the Play! Scala Json library. | |
// Infinity cannot be expressed as a Scalar value in Json, so I am using an object to hold it instead. | |
// | |
// Example of a finite value: | |
// { | |
// "value": 12.009, | |
// "isInfinite": false | |
// } | |
// | |
// Example of an infinite value: |
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
# Create a new Parser process | |
iex(2)> {:ok, pid} = Parser.start_link() | |
{:ok, #PID<0.118.0>} | |
# Verify the process is alive | |
iex(3)> Process.alive?(pid) | |
true | |
# Send it a valid message | |
iex(4)> GenServer.call(pid, "100") |
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
defmodule Parser do | |
use GenServer | |
def start_link() do | |
GenServer.start_link(__MODULE__, :ok, []) | |
end | |
def handle_call(message, _from, state) do | |
i = String.to_integer(message) | |
{:reply, i, state} |
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
# Create a new Parser process | |
iex(1)> {:ok, pid} = Parser.start_link() | |
{:ok, #PID<0.112.0>} | |
# Figure out our iex process' PID | |
iex(2)> self() | |
#PID<0.110.0> | |
# See that our iex process is linked to the Parser process | |
iex(3)> Process.info(self(), :links) |
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
## lib/MySupervisor.ex | |
defmodule MySupervisor do | |
use GenServer | |
## API | |
def start_link(child_spec_list) do | |
GenServer.start_link(__MODULE__, child_spec_list) | |
end | |
def list_processes(pid) do |
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
# Start a supervisor with two child specifications | |
iex(1)> {:ok, pid} = MySupervisor.start_link([{Parser, :start_link, []}, {Parser, :start_link, []}]) | |
{:ok, #PID<0.123.0>} | |
# See the two processes that the supervisor started | |
iex(2)> MySupervisor.list_processes(pid) | |
%{#PID<0.124.0> => {Parser, :start_link, []}, | |
#PID<0.125.0> => {Parser, :start_link, []}} | |
# Verify the supervisor has linked to the processes |
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
# Get a reference to one of the Parser process pid | |
iex(4)> parser_pid = MySupervisor.list_processes(pid) |> Map.keys |> List.first | |
#PID<0.124.0> | |
# Send a valid message | |
iex(5)> GenServer.call(parser_pid, "100") | |
100 | |
# Send an invalid message | |
iex(6)> GenServer.call(parser_pid, "tyler") |
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
## OTP Callbacks | |
def handle_info({:EXIT, dead_pid, _reason}, state) do | |
# Start new process based on dead_pid spec | |
{new_pid, child_spec} = state | |
|> Map.get(dead_pid) | |
|> start_child() | |
# Remove the dead_pid and insert the new_pid with its spec | |
new_state = state |
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
# Start a supervisor with two child specifications | |
iex(1)> {:ok, pid} = MySupervisor.start_link([{Parser, :start_link, []}, {Parser, :start_link, []}]) | |
{:ok, #PID<0.112.0>} | |
# See the two processes that the supervisor started | |
iex(2)> MySupervisor.list_processes(pid) | |
%{#PID<0.113.0> => {Parser, :start_link, []}, | |
#PID<0.114.0> => {Parser, :start_link, []}} | |
# Get a reference to one of the Parser process pid |
OlderNewer