Anonymous avatar Anonymous committed 57a0488

enable setting of Javascript linkfun over raw http resource

PUT /raw/Bucket

where ... is:
{jsanon:SourceCodeAsString} to specify function source
{jsanon:{bucket:Bucket,key:Key}} to specify the bucket and key of an object holding function source
{jsfun:NameAsString} to specify a predefined function by name

Comments (0)

Files changed (2)


 -define(JSON_MOD,     <<"mod">>).
 -define(JSON_FUN,     <<"fun">>).
 -define(JSON_CHASH,   <<"chash_keyfun">>).
+-define(JSON_JSFUN,    <<"jsfun">>).
+-define(JSON_JSANON,   <<"jsanon">>).
+-define(JSON_JSBUCKET, <<"bucket">>).
+-define(JSON_JSKEY,    <<"key">>).
 %% Names of HTTP query parameters
 -define(Q_PROPS, "props").


 jsonify_bucket_prop({linkfun, {qfun, _}}) ->
     {?JSON_LINKFUN, <<"qfun">>};
+jsonify_bucket_prop({linkfun, {jsfun, Name}}) ->
+    {?JSON_LINKFUN, {struct, [{?JSON_JSFUN, Name}]}};
+jsonify_bucket_prop({linkfun, {jsanon, {Bucket, Key}}}) ->
+    {?JSON_LINKFUN, {struct, [{?JSON_JSANON,
+                               {struct, [{?JSON_JSBUCKET, Bucket},
+                                         {?JSON_JSKEY, Key}]}}]}};
+jsonify_bucket_prop({linkfun, {jsanon, Source}}) ->
+    {?JSON_LINKFUN, {struct, [{?JSON_JSANON, Source}]}};
 jsonify_bucket_prop({chash_keyfun, {Module, Function}}) ->
     {?JSON_CHASH, {struct, [{?JSON_MOD,
 %% @doc The reverse of jsonify_bucket_prop/1.  Converts JSON representation
 %%      of bucket properties to their Erlang form.
 erlify_bucket_prop({?JSON_LINKFUN, {struct, Props}}) ->
-    {linkfun, {modfun,
-               list_to_existing_atom(
-                 binary_to_list(
-                   proplists:get_value(?JSON_MOD, Props))),
-               list_to_existing_atom(
-                 binary_to_list(
-                   proplists:get_value(?JSON_FUN, Props)))}};
+    case {proplists:get_value(?JSON_MOD, Props),
+          proplists:get_value(?JSON_FUN, Props)} of
+        {Mod, Fun} when is_binary(Mod), is_binary(Fun) ->
+            {linkfun, {modfun,
+                       list_to_existing_atom(binary_to_list(Mod)),
+                       list_to_existing_atom(binary_to_list(Fun))}};
+        {undefined, undefined} ->
+            case proplists:get_value(?JSON_JSFUN, Props) of
+                Name when is_binary(Name) ->
+                    {linkfun, {jsfun, Name}};
+                undefined ->
+                    case proplists:get_value(?JSON_JSANON, Props) of
+                        {struct, Bkey} ->
+                            Bucket = proplists:get_value(?JSON_JSBUCKET, Bkey),
+                            Key = proplists:get_value(?JSON_JSKEY, Bkey),
+                            %% bomb if malformed
+                            true = is_binary(Bucket) andalso is_binary(Key),
+                            {linkfun, {jsanon, {Bucket, Key}}};
+                        Source when is_binary(Source) ->
+                            {linkfun, {jsanon, Source}}
+                    end
+            end
+    end;
 erlify_bucket_prop({?JSON_CHASH, {struct, Props}}) ->
     {chash_keyfun, {list_to_existing_atom(
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.