Skip to content
/ coers Public
forked from xvw/coers

A small library for coercing to primitive Erlang types

License

Notifications You must be signed in to change notification settings

erlsci/coers

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

coers

Build Status Erlang Versions Tag

Project Logo

A small library for coercion to primitive Erlang types

About

Coers is a very small library to provide small coercion on primitive types in Erlang. This library was built essentially for internal tools at derniercri.io

Build & Test

$ rebar3 compile
$ rebar3 check

The later includes not only unit tests, but also other checks and coverage assessment.

Usage

Each coercion is wrapped into a special record:

-record(result, {
  value :: term(),
  error :: term(),
}).

If a coercion fail, the value field is undefined and the error field is populated with an appropriate error. If the coersion succeed, the value field becomes the coerced data and the error field is undefined.

You can use these functions from the coers API to examine the coercion status:

  • coers:value(Result)
  • coers:error(Result)
  • coers:has_error(Result)

For example :

1> R1 = coers:to_int("10").
{result,10,undefined}
2> R2 = coers:to_int("foo").
{result,undefined,{badarg,"Could not convert \"foo\" (type any) to int"}}

Additional convenience functions are available via the results library:

3> results:has_values([R1, R2]).
[true,false]
4> results:has_errors([R1, R2]).
[false,true]
5> results:values([R1, R2]).
[10,undefined]

Note that fractions are supported (via the rationals Erlang library):

6> coers:to_rational("1/42").
{result,{fraction,1,42},undefined}
7> coers:to_rational(<<"1/42">>).
{result,{fraction,1,42},undefined}
8> coers:to_rational({1, 42}).
{result,{fraction,1,42},undefined}

Example usgage in LFE:

(defun ->lfe (arg)
  (case arg
   (#"#t" 'true)
   (#"#f" 'false)
   (#"NIL" 'nil)
   (_ (cond ((?= `#(result true ,val) (coers:to_int arg))
             val)
            ((?= `#(result true ,val) (coers:to_float arg))
             val)
            ((?= `#(result true ,val) (coers:to_bool arg))
             val)
            ((?= `#(result true ,val) (coers:to_string arg))
             val)
            ('true arg)))))

License

MIT

Copyright © 2020-2021, Erlang-Aided Enrichment Center

Copyright © 2016, Xavier van De Woestyne