View Source ExAws.CodePipeline.Utils (ex_aws_code_pipeline v2.1.0)
Helper utility functions
Summary
Types
Approach to camelization
The camelize, camelize_list, and camelize_map take an argument that is a data structure providing rules for capitalization.
Functions
Camelize an atom or string value
Camelize keys, including traversing values that are also Maps.
Return the default camelize rules
If val is a Keyword then convert to a Map, else return val.
Types
@type camelization() :: :upper | :lower
Approach to camelization
- upper - all words are upper-cased
- lower - the first word is lower cased and the remaining words are upper-cased
Examples
:upper
iex> camelize_rules = %{default: :upper}
iex> ExAws.CodePipeline.Utils.camelize(:test_things, camelize_rules)
"TestThings"
iex> camelize_rules = %{default: :upper}
iex> ExAws.CodePipeline.Utils.camelize(:abc, camelize_rules)
"Abc"
iex> camelize_rules = %{default: :upper}
iex> ExAws.CodePipeline.Utils.camelize("test-a-string", camelize_rules)
"TestAString"
:lower
iex> camelize_rules = %{default: :lower}
iex> ExAws.CodePipeline.Utils.camelize(:test_things, camelize_rules)
"testThings"
iex> camelize_rules = %{default: :lower}
iex> ExAws.CodePipeline.Utils.camelize(:abc, camelize_rules)
"abc"
iex> camelize_rules = %{default: :lower}
iex> ExAws.CodePipeline.Utils.camelize("test-a-string", camelize_rules)
"testAString"
@type camelize_rules() :: %{ optional(:subkeys) => %{optional(atom()) => camelization()}, optional(:keys) => %{optional(atom()) => camelization()}, default: camelization() }
The camelize, camelize_list, and camelize_map take an argument that is a data structure providing rules for capitalization.
- subkeys - this provides a map that indicates how keys found under this particular key should be camelized
- keys - this provides a map that indicate how particular keys should be camelized
- default - indicates whether
:upper
or:lower
is used by default
Functions
camelize(val, camelize_rules \\ %{default: :lower, keys: %{s3_bucket: :upper, s3_object_key: :upper}, subkeys: %{}})
View Source@spec camelize(atom() | binary(), camelize_rules()) :: binary()
Camelize an atom or string value
This works as expected if the val uses an underscore or hyphen to separate words. This only works for atoms and strings. Passing another value type (integer, list, map) will raise exception.
The regex used to split a String into words is: ~r/(?:^|[-_])|(?=[A-Z])/
Example
iex> ExAws.CodePipeline.Utils.camelize(:test_val)
"testVal"
iex> ExAws.CodePipeline.Utils.camelize(:"test_val")
"testVal"
iex> ExAws.CodePipeline.Utils.camelize(:"abc-def-a123")
"abcDefA123"
iex> ExAws.CodePipeline.Utils.camelize(:A_test_of_initial_cap)
"aTestOfInitialCap"
camelize_map(val, camelize_rules \\ %{default: :lower, keys: %{s3_bucket: :upper, s3_object_key: :upper}, subkeys: %{}})
View SourceCamelize keys, including traversing values that are also Maps.
The caller can pass in an argument to indicate whether the first letter of a key for the map are downcased or capitalized.
Keys should be atoms and follow the rules listed for the camelize/2
function.
Example
iex> val = %{abc_def: 123, another_val: "val2"}
iex> ExAws.CodePipeline.Utils.camelize_map(val)
%{"abcDef" => 123, "anotherVal" => "val2"}
iex> val = %{abc_def: 123, another_val: %{embed_value: "val2"}}
iex> ExAws.CodePipeline.Utils.camelize_map(val)
%{"abcDef" => 123, "anotherVal" => %{"embedValue" => "val2"}}
iex> val = %{abc_def: 123, another_val: %{embed_value: "val2"}}
iex> ExAws.CodePipeline.Utils.camelize_map(val, %{subkeys: %{}, keys: %{}, default: :upper})
%{"AbcDef" => 123, "AnotherVal" => %{"EmbedValue" => "val2"}}
@spec camelize_rules() :: camelize_rules()
Return the default camelize rules
A caller can override this by creating a camelize_rules/0
and passing
it into functions instead of the default.
If val is a Keyword then convert to a Map, else return val.
This function works recursively. If you have a Keyword list where the value for the key is a keyword then the val is converted to a Map as well.
Examples
iex> ExAws.CodePipeline.Utils.keyword_to_map([{:a, 7}, {:b, "abc"}])
%{a: 7, b: "abc"}
iex> ExAws.CodePipeline.Utils.keyword_to_map(%{a: 7, b: %{c: "abc"}})
%{a: 7, b: %{c: "abc"}}
iex> ExAws.CodePipeline.Utils.keyword_to_map([1, 2, 3])
[1, 2, 3]
iex> ExAws.CodePipeline.Utils.keyword_to_map[test: [inner: "abc"]]
%{test: %{inner: "abc"}}