Erlang Common Data Types
  • Erlang 60.3%
  • Shell 23.1%
  • Batchfile 9.9%
  • Makefile 6.7%
Find a file
2013-11-13 16:42:04 +02:00
include Implement to_str 2013-10-02 21:25:59 +02:00
rel Modify release config so that "rebar generate" works 2013-09-16 17:35:02 +02:00
src Use error_m monad and get rid of most exception throwing/catching 2013-11-13 13:38:49 +02:00
test Rename to "Erlang Common Data Types" 2013-11-10 18:50:13 +02:00
.gitignore Ignore dialyzer plt file 2013-10-21 11:21:11 +02:00
.travis.yml Use a forked version of erlando which hopefully compiles without errors on R16B 2013-11-13 16:42:04 +02:00
LICENSE.md This project has an MIT license 2013-10-01 20:51:15 +02:00
Makefile Include extra dialyzer options 2013-11-10 19:24:47 +02:00
README.md Rename to "Erlang Common Data Types" 2013-11-10 18:50:13 +02:00
rebar Try rebar compiled with R14B03 2013-10-02 22:00:54 +02:00
rebar.config Use a forked version of erlando which hopefully compiles without errors on R16B 2013-11-13 16:42:04 +02:00

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.