Skip to content

Cuttlefish Developer's API

Joe DeVivo edited this page Jan 9, 2014 · 1 revision

These are functions that are useful in writing translations

cuttlefish

cuttlefish:conf_get/2

Extract a specific value from Conf. If it's not set, it throws a not_found, which cuttlefish knows how to wrap in an error. If you don't like that behavior and want to use a default, choose the 3 arity version below. Example

Conf = [
	{["a", "b"], 4}
],

cuttlefish:conf_get("a.b", Conf), %% returns 4
cuttlefish:conf_get("a.c", Conf), %% throws 'not_found'

cuttlefish:conf_get/3

Conf = [
	{["a", "b"], 4}
],

cuttlefish:conf_get("a.b", Conf, undefined), %% returns 4
cuttlefish:conf_get("a.c", Conf, undefined), %% returns 'undefined'

cuttlefish:unset/0

When writing a translation and you get to a point where you don't want a value written to the app.config, just call cuttlefish:unset() and we'll take it from there.

cuttlefish:invalid/1

When you can't generate a valid config value, and you know it, call this function with an error message and cuttlefish will report it properly.

cuttlefish_variable

cuttlefish_variable:tokenize/1

This is pretty much string:tokens(Key, ".") but it skips escaped dots.

string:tokens("a.b\\.c.d", "."), %% returns ["a", "b\\", "c", "d"]
cuttlefish_variable:tokenize("a.b\\.c.d", "."), %% returns ["a", "b.c", "d"]

cuttlefish_variable:fuzzy_matches/2

If you've used a $name in your mapping, you can get a list of all possible values of $name.

Conf = [
	{["a", "b1", "c"], 4},
	{["a", "b1", "d"], 4},
	{["a", "b2", "c"], 4},
],

cuttlefish_variable:fuzzy_matches(["a", "$name", "c"], Conf), %% returns [{"$name", "b1"}, {"$name", "b2"}]. 

Unfortunately, if you ran the same function on ["a", "$name", "d"] you'd only return [{"$name", "b1"}]. [https://github.com/basho/cuttlefish/issues/99] is an open issue to fix this.

cuttlefish_variable:filter_by_prefix/2

Basically, this is "give me everything that starts with X".

Example:

%% HTTP Listeners
%% @doc listener.http.<name> is an IP address and TCP port that the Riak
%% HTTP interface will bind.
{mapping, "listener.http.$name", "riak_api.http", [
  {default, {"127.0.0.1", 8098}},
  {datatype, ip},
  {include_default, "internal"}
]}.

{translation,
 "riak_api.http",
  fun(Conf) ->
      HTTP = cuttlefish_variable:filter_by_prefix("listener.http", Conf),
      [ IP || {_, IP} <- HTTP]
  end
}.