Giter Club home page Giter Club logo

ptrans's Introduction

ptrans - a bunch of handy parse transforms

1. pl2rec - proplist to record converter

A lot of people prefer to pass gen_server options as a proplist and store them in a state being a record. So, they generally write something like that:

-record(state, {
    port = 8080,
    ip = {127,0,0,1},
    name
}).

...

init(Options) ->
    State = parse_options(Options, #state{}),
    {ok, State}.

...

parse_options([], State) -> State;
parse_options([{port, Port} | T], State) ->
    parse_options(T, State#state{port = Port});
parse_options([{ip, IP} | T], State) ->
    parse_options(T, State#state{ip = IP});
parse_options([{name, Name} | T], State) ->
    parse_options(T, State#state{name = Name}).

Annoying, isn't it?

But instead, you could just write:

-compile({parse_transform, pl2rec}).

-record(state, {
    port = 8080,
    ip = {127,0,0,1},
    name
}).

init(Options) ->
    State = parse_options(Options, #state{}),
    {ok, State}.

You can even parse multiple records this way. They may have same field names, still the parsing mechanism will work properly.

If you have some unexisting properties in your proplists, they will be just silently ignored during the parsing process.

Basic type-check guards will be added soon (they'll check if a given record has type definitions and insert the proper guards to the generated parse_options function.

ptrans's People

Contributors

doubleyou avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.