Packages
ex_aws
0.4.8
2.7.0
2.6.1
2.6.0
2.5.11
2.5.10
2.5.9
2.5.8
2.5.7
2.5.6
2.5.5
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.4
2.3.3
2.3.2
2.3.1
2.3.0
2.2.10
2.2.9
2.2.8
2.2.7
2.2.6
2.2.5
2.2.4
2.2.3
2.2.2
2.2.1
2.2.0
2.1.9
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.1
1.0.0-beta3
1.0.0-beta2
1.0.0-beta1
1.0.0-beta0
0.5.0
0.4.19
0.4.18
0.4.17
0.4.15
0.4.14
0.4.13
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.1
0.3.0
0.2.0
0.1.2
0.1.1
0.1.0
0.0.5
0.0.4
0.0.3
AWS client for Elixir. Currently supports Dynamo, DynamoStreams, EC2, Firehose, Kinesis, KMS, Lambda, RRDS, Route53, S3, SES, SNS, SQS, STS and others.
Current section
Files
Jump to
Current section
Files
lib/ex_aws/dynamo/encodable.ex
defprotocol ExAws.Dynamo.Encodable do
@type t :: any
@doc "Converts an elixir value into a map tagging the value with its dynamodb type"
def encode(value)
end
defimpl ExAws.Dynamo.Encodable, for: Atom do
def encode(true), do: %{"BOOL" => "true"}
def encode(false), do: %{"BOOL" => "false"}
def encode(value), do: %{"S" => value |> Atom.to_string}
end
defimpl ExAws.Dynamo.Encodable, for: Integer do
def encode(val) do
%{"N" => val |> Integer.to_string}
end
end
defimpl ExAws.Dynamo.Encodable, for: Float do
def encode(val) do
%{"N" => val |> Float.to_string}
end
end
defimpl ExAws.Dynamo.Encodable, for: HashDict do
def encode(hashdict) do
%{"M" => ExAws.Dynamo.Encodable.Map.do_encode(hashdict)}
end
end
defimpl ExAws.Dynamo.Encodable, for: Map do
def encode(%{__struct__: _} = struct) do
struct
|> Map.from_struct
|> do_encode
end
def encode(map) do
%{"M" => do_encode(map)}
end
def do_encode(dict) do
Enum.reduce(dict, %{}, fn
({_, nil}, map) -> map
({_, []}, map) -> map
({k, v}, map) when is_binary(k) ->
Map.put(map, k, ExAws.Dynamo.Encodable.encode(v))
({k, v}, map) ->
key = String.Chars.to_string(k)
Map.put(map, key, ExAws.Dynamo.Encodable.encode(v))
end)
end
end
defimpl ExAws.Dynamo.Encodable, for: BitString do
def encode(val) do
%{"S" => val}
end
end
defimpl ExAws.Dynamo.Encodable, for: List do
alias ExAws.Dynamo.Encodable
def encode([]), do: []
@doc """
Dynamodb offers typed sets and L, a generic list of typed attributes.
If all items in the list have the same dynamo type, This will return the
corrosponding Dynamo type set ie
[%{"N" => 1},%{"N" => 2}] #=> %{"NS" => [1,2]}
Otherwise, it will use the generic list type ie
[%{"N" => 1},%{"S" => "Foo"}] #=> %{"L" => [%{"N" => 1},%{"S" => "Foo"}]}
"""
def encode(list) do
typed_values = list
|> Enum.map(&Encodable.encode/1)
{types, values} = Enum.reduce(list, {[], []}, fn(item, {types, values}) ->
[{type, value}] = item
|> Encodable.encode
|> Map.to_list
{[type | types], [value | values]}
end)
case types |> Enum.uniq do
["B"] -> %{"BS" => values |> Enum.reverse}
["N"] -> %{"NS" => values |> Enum.reverse}
["S"] -> %{"SS" => values |> Enum.reverse}
_ -> %{"L" => typed_values}
end
end
end