• This repository has been archived on 05/Oct/2019
  • Stars
    star
    118
  • Rank 293,294 (Top 6 %)
  • Language
    Erlang
  • License
    Other
  • Created almost 13 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parse transform for type based validation.

Sheriff

Sheriff is a parse transform that allows developers to check values against their type as defined through typespecs.

Sheriff generates validation functions for all the types in the module being compiled and then replaces all the sheriff:check/2 calls with calls to these validation functions.

Sheriff should be used where Dialyzer cannot do anything: when receiving external data, for example when reading from a file, a socket or receiving a process message.

Currently Sheriff should support all types excluding iolist/0 and maybe_improper_list/2. The main limitation is that it can only work with modules compiled with the Sheriff parse transform.

Also note that Sheriff does not work with opaque types. If you try to check an opaque type, the file won't compile. If you try to check a type which include an opaque type, a runtime error will be produced.

Usage

To compile a module with the Sheriff parse transform, simply add the following line at the top of your module:

-compile({parse_transform, sheriff}).

This compilation option can also be defined project-wide using rebar.config or the Erlang compiler.

Type Check

To check that a value matches a type, you need to first define a type, then call sheriff:check/2 with the value and the type as arguments.

-type colors() :: blue | red | green | yellow.

paint(Color, Object) ->
	case sheriff:check(Color, colors) of
		true ->
			do_paint(Color, Object);
		false ->
			{error, badarg}
	end.

Many times you will probably want to let it crash, though.

-type colors() :: blue | red | green | yellow.

paint(Color, Object) ->
	true = sheriff:check(Color, colors),
	do_paint(Color, Object).

You can check records. All the typed record values will be checked, along with making sure the value is a record of the expected type. To check for recordness, you must first define a type specifically for the record.

-type paintable_object() :: #paintable_object{}.

paint(Color, Object) ->
	true = sheriff:check(Color, colors),
	true = sheriff:check(Object, paintable_object),
    do_paint(Color, Object).

You can also check against a remote type.

paint(Color, Object) ->
	true = sheriff:check(Color, {picasso_module, colors}),
	do_paint(Color, Object).

You can finally use the inline notation. You can specify any built-in, local or remote type in a string and pass it to sheriff:check/2.

paint(Color, Object) ->
	true = sheriff:check(Color, "picasso_module:colors()"),
	do_paint(Color, Object).

erase(Pixels, Object) ->
	true = sheriff:check(Pixels, "list({integer(), integer()})"),
	do_erase(Pixels, Object).

Note that when passing atoms or tuples for the type to check against, Sheriff does not currently accept built-in types as arguments, only local or remote types. Also note that all types must be of arity 0, as sheriff:check/2 can only accept type names as argument at this time. This is a limitation only on the function call, not on the type specifications. You can use the inline notation to overcome it.

%% This type cannot be passed to sheriff:check/2.
-type a(A, B) :: [{A, B}].

%% These types can be passed to sheriff:check/2.
-type b() :: a(atom(), integer()).
-type c() :: list(integer()).
-type d() :: picasso_module:colors().

Thanks

Sheriff is available through the initial work and research by the students William Dang and Hamza Mahmood.

More Repositories

1

farwest

@deprecated Modern web application development platform
Erlang
118
star
2

elevators

Elevator control system demonstrating Erlang/OTP upgrades.
Erlang
62
star
3

ex_apns

An APNS client
Erlang
26
star
4

goldrush

@deprecated Small, Fast event processing and monitoring for Erlang/OTP applications.
Erlang
21
star
5

bank

Middleware for accessing SQL databases.
Erlang
19
star
6

xerl

Xerl, an eXtended ERLang language, is a language for the BEAM VM.
Erlang
17
star
7

bank_mysql

MySQL driver for Bank.
Erlang
15
star
8

ex_uri

@deprecated Please see https://github.com/heroku/ex_uri for a more up to date version.
Erlang
13
star
9

cowboy_fcgi

@deprecated See website for a maintained fork. A FastCGI handler for Cowboy
Erlang
13
star
10

shaman

@deprecated UI for distributed Erlang systems introspection.
JavaScript
13
star
11

erlang.bootstrap

@deprecated Please use the bootstrap features of erlang.mk directly!
13
star
12

ex_fcgi

@deprecated See website for a maintained fork. A FastCGI client
Erlang
12
star
13

ex_reloader

@deprecated Automatically reload Erlang modules.
Erlang
9
star
14

ezdoc

@deprecated Simplistic documentation format and associated parser.
Erlang
8
star
15

alien

@deprecated Alien is a framework for writing and running probes in Erlang systems.
Erlang
5
star
16

ex_cssmin

@deprecated CSS Minifier for Erlang
Erlang
4
star
17

ex_mysql

@deprecated Experimental, unmaintained MySQL client for Erlang
Erlang
3
star
18

farwest_core

Core Farwest application.
Erlang
3
star
19

ex_complete

@deprecated An application aiming to improve Erlang shell completion
Erlang
3
star
20

wee-website

@deprecated Web:Extend website
JavaScript
2
star
21

ex_xmlrpc

@deprecated An XML-RPC client
Erlang
2
star
22

ex_tidy

@deprecated A simple application bringing tidy in the marvelous realm of Erlang
C
2
star
23

ex_php

@deprecated A simple PHP serialization library
Erlang
1
star
24

ex_xml

@deprecated An XML canonization library for Erlang (wip)
Erlang
1
star
25

wee

@deprecated Web:Extend is an unmaintained PHP5 API
PHP
1
star
26

ex_http

@deprecated An HTTP support library.
Erlang
1
star
27

farwest_ui

Farwest administration UI.
CSS
1
star