On Sat, Apr 27, 2013 at 5:00 AM, <
diesway...@gmail.com> wrote:
> Say I have a type 'foo', that can only take the values 1..80
> I set it up as
> .alias [
> .name foo
> .type int64
> .erlang-type "piqirun_custom:foo"
> ]
> In piqirun_custom, I have the following (amongst other stuff)
> -type(foo() :: 1..80).
> foo_of_int64(I) when is_integer(I) andalso I >= 1 andalso I =< 80 ->
> I.
> foo_to_int64(I) when is_integer(I) andalso I >= 1 andalso I =< 80 ->
> I.
> The problem is that when you run this through piqic_erlang, the generated
> getters/setters/... will include the following line
> default_int64() -> 0.
> default_foo() -> piqirun_custom:foo_of_int64(default_int64()).
> And dialyzer is going to choke all over this (the error looks like the
> following
> "The call piqirun_custom:foo_of_int64(0) will never return since it differs
> in the 1st argument from the success typing arguments: (1..255 |
> {1..255,_})"
>
> This actually makes complete sense, since the default is expected to be '0',
> which is not what the typing of piqirun_custom:foo() expects.
> Mind you, this isn't really a protobuf issue - its more about what happens
> in the 'erlang' side of the world, where we want to be able to appropriate
> validate the data...
>
> Am I missing something really obvious here?
You got it right. There's not much more to what you have described.
To avoid Dialyzer warnings and runtime errors we could introduce
another alias property for defining erlang-specific default values to
be used instead of the built-in ones. It could look like something
like this:
.alias [
.name foo
.type int64
.erlang-type "piqirun_custom:foo"
.erlang-default "1"
]
or even this:
.alias [
.name foo
.type int64
.erlang-type "piqirun_custom:foo"
.erlang-default "piqirun_custom:foo_default()"
]
It would be very easy to extend piqic_erlang to handle this.
By the way, using custom types for runtime type validation is
something that I have not envisaged myself. It is a pretty cool hack.
Anton