Erlang Common Data Types
Erlang Shell
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
include
rel
src
test
.gitignore
.travis.yml
LICENSE.md
Makefile
README.md
rebar
rebar.config

README.md

Erlang Common Data Types

Build Status

This is a library that does parsing and validation in Erlang of some commonly used input types.

Currently only South African ID numbers are parsed and validated in this library.

Future additions to the library may include:

Building

This project uses rebar:

$ ./rebar get-deps compile
$ ./rebar compile skip_deps=true eunit

Usage Examples (with a bit of programming philosophy)

“The string is a stark data structure and everywhere it is passed there is much duplication of process. It is a perfect vehicle for hiding information.”

— Alan J. Perlis, Epigrams in Programming, #34

I frequently see input fields, database fields and other information in programs that are treated as if they are strings, when they can rather be converted to a specific data type that has made all the hidden information visible.

In this library every supported type will have a from_str/1 function that parses it into a well-defined data type:

 > {ok, ID} = rsa_id_number:from_str("4304041794068").
{ok,{rsa_id_number,{1943,4,4},1,794,0,6,8}}

Validation of the input may happen at parse time if it makes sense for the type, otherwise a separate validate/1 function may be provided that validates the already parsed data type.

In the case of RSA ID number, validation happens at parse time. Only a valid ID number will return an ID type. Invalid ID numbers will return an error when attempting to parse the ID number:

 > rsa_id_number:from_str("4304041794067").
{error,{invalid_checksum,7}}

The corresponding to_str/1 function will convert the type back to the original string.

 > rsa_id_number:to_str(ID).
"4304041794068"

The idea is that any input should be converted to a well-defined type as soon as possible, and for the duration of the program the type is passed around as needed, not the original string. Utility functions can be provided that operate on the type, eg. to extract the gender from an ID number:

 > rsa_id_number:gender(ID).
female
 > rsa_id_number:citizen(ID).
rsa

Only when needed should the type be converted back to string, eg. for display purposes or if your storage backend cannot store well-defined types.

As an implementation note, I am using this library as an opportunity to see what benefit Erlang programs can get from adding type specifications to functions, in conjunction with tools like dialyzer and proper (a property-based test tool inspired by QuickCheck).

Running the tests

There are property based tests (using proper) wrapped in eunit, in addition to some normal eunit tests:

$ ./rebar compile skip_deps=true eunit
==> rel (compile)
==> erlcdt (compile)
==> rel (eunit)
==> erlcdt (eunit)
Compiled src/erlcdt_testhelper.erl
Testing rsa_id_number:checksum/1
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
OK: Passed 2000 test(s).

Testing rsa_id_number:citizen/1
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
OK: Passed 2000 test(s).

Testing rsa_id_number:gender/1
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
OK: Passed 2000 test(s).

Testing rsa_id_number:date_from_str/1
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
OK: Passed 2000 test(s).

Testing rsa_id_number:to_str/1
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
OK: Passed 2000 test(s).

Testing rsa_id_number:from_str/1
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
OK: Passed 2000 test(s).

Testing erlcdt_utils:strip_century/1
..........................................................................................................................................................................................................................................................
OK: Passed 250 test(s).

Testing erlcdt_utils:tomorrow/0
..........................................................................................................................................................................................................................................................
OK: Passed 250 test(s).

Testing erlcdt_utils:yesterday/0
..........................................................................................................................................................................................................................................................
OK: Passed 250 test(s).

Testing erlcdt_utils:today/0
..........................................................................................................................................................................................................................................................
OK: Passed 250 test(s).

Testing erlcdt_utils:dob_str/1
..........................................................................................................................................................................................................................................................
OK: Passed 250 test(s).

Testing erlcdt_utils:odd_even_elements/1
..........................................................................................................................................................................................................................................................
OK: Passed 250 test(s).

  All 13 tests passed.

LICENCE

Released under the MIT license, see LICENSE.md for details.