You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Anthony Molinaro (Resolved) (JIRA)" <ji...@apache.org> on 2012/03/22 07:38:23 UTC

[jira] [Resolved] (THRIFT-1532) The type specifications in the generated Erlang code should include "undefined" where it's used as a default value

     [ https://issues.apache.org/jira/browse/THRIFT-1532?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Anthony Molinaro resolved THRIFT-1532.
--------------------------------------

       Resolution: Fixed
    Fix Version/s: 0.9

Okay, so I used most of your patch, but modified as there were a few problems (mainly that default values went away with your patch).

I did not include this chunk
{code}
@@ -497,7 +499,7 @@
   } else if (type->is_enum()) {
     return "integer()";
   } else if (type->is_struct() || type->is_xception()) {
-    return "#" + uncapitalize(type->get_name()) + "{}";
+    return "#" + uncapitalize(type->get_name()) + "{} | undefined";
   } else if (type->is_map()) {
     return "dict()";
   } else if (type->is_set()) {
{code}
as I don't believe it's necessary as the docs state (http://www.erlang.org/doc/reference_manual/typespec.html#id75102) that it should be unnecessary to include undefined.

I also went ahead for string types and the have the type of 'string() | binary()' as that is technically what is allowed.

An example generation is for this
{code}
struct StructA
{
  1: string a,
  2: binary b,
  3: optional string c,
  4: optional binary d,
  5: required string e,
  6: required binary f,
  7: string g = "foo", 
  8: i32 h,       
  9: optional i32 i,
  10: required i32 j, 
  11: required i32 k = 5,
  12: double l,   
  13: optional double m,
  14: required double n,
  15: double o = 3.14159,
  16: list<string> string_list,
  17: list<byte> byte_list = [1, 2, 3],
  18: set<string> string_set, 
  19: map<string, string> string_map
}                 
{code}

It now generates
{code}
-record(structA, {a :: string() | binary(),
                  b :: string() | binary(),
                  c :: string() | binary(),
                  d :: string() | binary(),
                  e :: string() | binary(),
                  f :: string() | binary(),
                  g = "foo" :: string() | binary(),
                  h :: integer(),
                  i :: integer(),
                  j :: integer(),
                  k = 5 :: integer(),
                  l :: float(),
                  m :: float(),
                  n :: float(),
                  o = 3.14159 :: float(),
                  string_list = [] :: list(),
                  byte_list = [1,2,3] :: list(),
                  string_set = sets:new() :: set(),
                  string_map = dict:new() :: dict()}).
{code}

Which I feel is more correct.

Please try out SVN head and let me know if it works for you.  And if you feel I should include that chunk that I left out please include a thrift file which breaks and I can try it out.
                
> The type specifications in the generated Erlang code should include "undefined" where it's used as a default value
> ------------------------------------------------------------------------------------------------------------------
>
>                 Key: THRIFT-1532
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1532
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Erlang - Compiler
>    Affects Versions: 0.8
>         Environment: Erlang/OTP R15B.
>            Reporter: Björn Bylander
>            Assignee: Anthony Molinaro
>            Priority: Minor
>              Labels: compiler, dialyzer, erlang, specification, type
>             Fix For: 0.9
>
>         Attachments: THRIFT-1532_t_erl_generator.cc.patch, agent.tar.gz
>
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> The Erlang code generated by the compiler for, for example, an integer field of a struct sets a default value of 'undefined' but the type to just 'integer()' which doesn't include the default value. This makes Dialyzer complain if the field has not been initialized as it gets set to 'undefined' instead of an integer value.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira