All Projects → vic → deco

vic / deco

Licence: Apache-2.0 license
Minimalist Function Decorators for Elixir

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to deco

Decorator
Function decorators for Elixir
Stars: ✭ 278 (+1223.81%)
Mutual labels:  decorators, ast
tinsel
Use Decorators to Transform Functions
Stars: ✭ 18 (-14.29%)
Mutual labels:  decorators, function-decorator
Plumier
A TypeScript backend framework focuses on development productivity, uses dedicated reflection library to help you create a robust, secure and fast API delightfully.
Stars: ✭ 223 (+961.9%)
Mutual labels:  decorators
decapi
Create GraphQL API by decorating TypeScript classes
Stars: ✭ 81 (+285.71%)
Mutual labels:  decorators
astexplorer-go
No description or website provided.
Stars: ✭ 17 (-19.05%)
Mutual labels:  ast
Kaop Ts
Simple Yet Powerful Library of ES2016 Decorators with Strongly typed method Interceptors like BeforeMethod, AfterMethod, OnException, etc
Stars: ✭ 235 (+1019.05%)
Mutual labels:  decorators
java-ast
Java Parser for JavaScript/TypeScript (based on antlr4ts)
Stars: ✭ 58 (+176.19%)
Mutual labels:  ast
Memo Decorator
Decorator which applies memoization to a method of a class.
Stars: ✭ 213 (+914.29%)
Mutual labels:  decorators
parser-reflection
Parser Reflection API - Provides source code analysis without loading classes into the PHP memory
Stars: ✭ 97 (+361.9%)
Mutual labels:  ast
vue-class-state
面向对象风格的vue状态管理
Stars: ✭ 14 (-33.33%)
Mutual labels:  decorators
gox
JSX for Go
Stars: ✭ 165 (+685.71%)
Mutual labels:  ast
html5parser
A super tiny and fast html5 AST parser.
Stars: ✭ 153 (+628.57%)
Mutual labels:  ast
Decorating
decorating: Literally decorating your terminal with decorators
Stars: ✭ 248 (+1080.95%)
Mutual labels:  decorators
awesome-ruby-ast
A list of awesome tools and libraries which deals with ASTs in Ruby
Stars: ✭ 24 (+14.29%)
Mutual labels:  ast
Node Decorators
node-decorators
Stars: ✭ 230 (+995.24%)
Mutual labels:  decorators
rehype-dom
HTML processor to parse and compile with browser APIs, powered by plugins
Stars: ✭ 20 (-4.76%)
Mutual labels:  ast
Mobx Task
Makes async function state management in MobX fun.
Stars: ✭ 218 (+938.1%)
Mutual labels:  decorators
stutter
Implement a Lisp, in C, from scratch, no libs
Stars: ✭ 65 (+209.52%)
Mutual labels:  ast
sast
Parse CSS, Sass, SCSS, and Less into a unist syntax tree
Stars: ✭ 51 (+142.86%)
Mutual labels:  ast
BBob
⚡️Blazing-fast js-bbcode-parser, bbcode js, that transforms and parses to AST with plugin support in pure javascript, no dependencies
Stars: ✭ 133 (+533.33%)
Mutual labels:  ast

Deco - Minimalist function decorators and around-advice for Elixir.

hexdocs.

Yes, yet another package for decorating elixir functions :).

However, deco's core is minimalist in comparission to others, uses much less magic, does not overrides anything like Kernel.def nor any operator.

deco has only one macro, decorators themselves are just plain functions Macro.t() -> Macro.t(), and imposes no run-time overhead because function decoration is performed at compile-time.

Usage

use Deco

The syntax is deco DECORATORS in FORM where decorators is a tuple of one or more decorators, and form is tipically a function definition.

decorators are just plain functions you write that take the AST of FORM and just return a modified AST of it. The Deco module has some convenience functions for updating the AST.

Since deco runs at compile time, you cannot use functions being defined in the same module that is using deco. Normally it's better to define your decorators on a separate module.

The following example decorates the function with a tracer that will use Logger to print the arguments given before applying the original function, and the its final result.

   deco { Trace.trace() } in
   def foo(x) do
     x
   end

Decorators can take arguments, the AST will be prepended to the list of arguments given by you.

Also, because decorators operate on the AST they have full access to it allowing them to for example, introduce new guards or remove them.

   deco { Deco.update_guard(fn _ -> nil end) } in
   def foo(x) when is_atom(x) do
     x
   end
   
   foo("hey")
   => "hey"

Decorators can be composed, the one at the end will take the original AST and produce a new one for the one on top of it.

   deco {
     Deco.pipe_result(String.capitalize),
     Deco.pipe_result(String.reverse),
     Deco.pipe_result(to_string)
   } in
   def foo(x) do
     x
   end
   
   foo(:john)
   => "Nhoj

You can decorate using a simple function, without having to mess with the AST if you dont want to. The Deco.around decorator will act as an around advice and will give you a reference to the decorated function (made private) and all the arguments given on invocation.

   # our private around advice
   defp bar_wrapper(decorated, name, say) do
     "#{say} #{decorated.(name)}"
   end

   deco {Deco.around( bar_wrapper("hello") )} in
   def bar(name) do
     name |> String.capitalize
   end


   bar("world")
   => "hello World"

For more examples, see the tests or use the source, Luke

AuthDeco

This example was adapted from arjan/decorator to show how it would look like using deco.

   defp is_authorized(decorated, conn, params) do
      if conn.assigns.user do
        decorated.(conn, params)
      else
        conn
        |> send_resp(401, "unauthorized")
        |> halt()
      end
   end

   deco {Deco.around(is_authorized)} in
   def create(conn, params) do
     ...
   end

Installation

def deps do
  [
    {:deco, "~> 0.1"}
  ]
end
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].