Permalink
Browse files

Make Dialyzer happy with the generated default field values

When we assign default values for optional fields we need to explicitly specify
that these fields can also be 'undefined'. Otherwise, Dialyzer will generate
errors on the attempt to set such fields to 'undefined'.
  • Loading branch information...
1 parent 6550351 commit 56df2b692b80eee4aca61b0f998eb86c0fe38523 @alavrik committed Apr 4, 2013
Showing with 19 additions and 13 deletions.
  1. +2 −2 src/piqi_piqi.hrl
  2. +12 −6 src/piqic_erlang_types.erl
  3. +5 −5 tests/misc/defaults_piqi.hrl-test
View
@@ -49,7 +49,7 @@
-record(field, {
name :: name(),
type :: type(),
- mode = required :: field_mode(),
+ mode = required :: field_mode() | 'undefined',
default :: piqi_any(),
deprecated = false :: boolean(),
piq_format :: piq_format(),
@@ -132,7 +132,7 @@
protobuf_package :: string() | binary(),
erlang_module :: string() | binary(),
erlang_type_prefix :: string() | binary(),
- erlang_string_type = binary :: erlang_string_type()
+ erlang_string_type = binary :: erlang_string_type() | 'undefined'
}).
-record(import, {
View
@@ -193,23 +193,29 @@ gen_field(Context, X) ->
Name = erlname_of_field(Context, X),
% initialize repeated fields with [] & populate default values if they are
% defined in Piqi
- Default = gen_field_default(Context, X),
+ {Default, CanBeUndefined} = gen_field_default(Context, X),
[
- Name, Default, " :: ", gen_field_type(Context, X#field.mode, X#field.type)
+ Name, Default, " :: ", gen_field_type(Context, X#field.mode, X#field.type),
+ case CanBeUndefined of
+ true -> " | 'undefined'";
+ false -> ""
+ end
].
gen_field_default(Context, X) ->
case X#field.mode of
repeated ->
- " = []";
+ {" = []", _CanBeUndefined = false};
optional when X#field.type =:= 'undefined' -> % flag
- " = false";
+ {" = false", _CanBeUndefined = false};
optional when X#field.default =/= 'undefined' ->
Value = gen_field_default(Context, X#field.type, X#field.default, _WireType = 'undefined'),
- [" = ", Value];
+ % NOTE: we need 'undefined' here, because otherwise Dialyzer won't
+ % treat it as a valid field value
+ {[" = ", Value], _CanBeUndefined = true};
_ ->
- ""
+ {"", _CanBeUndefined = false}
end.
@@ -3,11 +3,11 @@
-record(defaults_r, {
- i = -1 :: integer(),
- s = <<"foo">> :: string() | binary(),
- bin = <<"foo">> :: binary(),
- b = true :: boolean(),
- f = 1.0 :: number()
+ i = -1 :: integer() | 'undefined',
+ s = <<"foo">> :: string() | binary() | 'undefined',
+ bin = <<"foo">> :: binary() | 'undefined',
+ b = true :: boolean() | 'undefined',
+ f = 1.0 :: number() | 'undefined'
}).
-type defaults_r() :: #defaults_r{}.

0 comments on commit 56df2b6

Please sign in to comment.