[ 
https://issues.apache.org/jira/browse/THRIFT-3703?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16029296#comment-16029296
 ] 

ASF GitHub Bot commented on THRIFT-3703:
----------------------------------------

GitHub user dcelasun opened a pull request:

    https://github.com/apache/thrift/pull/1281

    THRIFT-3703 Unions Field Count Does Not Consider Map/Set/List Fields

    Even though maps and slices are not pointer fields in the generated
    code, they are still nullable. Adding an extra check for map/set/list
    types fixes the generated Count* methods.
    
    Sample IDL:
    ```thrift
    union Foo{
        1: map<bool,bool> u1
        2: set<bool> u2
        3: list<bool> u3
        4: bool u4
    }
    ```
    
    Generated code before fix:
    ```go
    func (p *Foo) CountSetFieldsFoo() int {
        count := 0
        if p.IsSetU4() {
                count++
        }
        return count
    }
    ```
    
    After fix:
    ```go
    func (p *Foo) CountSetFieldsFoo() int {
        count := 0
        if p.IsSetU1() {
                count++
        }
        if p.IsSetU2() {
                count++
        }
        if p.IsSetU3() {
                count++
        }
        if p.IsSetU4() {
                count++
        }
        return count
    }
    ```
    
    Fixes THRIFT-3703.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/dcelasun/thrift THRIFT-3703

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/thrift/pull/1281.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1281
    
----
commit cacfd6fd041eba27540f61a897cc5d5826126197
Author: D. Can Celasun <c...@dcc.im>
Date:   2017-05-30T10:44:56Z

    THRIFT-3703 Unions Field Count Does Not Consider Map/Set/List Fields
    
    Even though maps and slices are not pointer fields in the generated
    code, they are still nullable. Adding an extra check for map/set/list
    types fixes the generated Count* methods.
    
    Fixes THRIFT-3703.

----


> Unions Field Count Does Not Consider Map/Set/List Fields
> --------------------------------------------------------
>
>                 Key: THRIFT-3703
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3703
>             Project: Thrift
>          Issue Type: Bug
>          Components: Go - Compiler
>    Affects Versions: 0.9.3, 0.10.0
>         Environment: Ubuntu 15.10
>            Reporter: Tom Deering
>            Assignee: Can Celasun
>              Labels: golang, thrift, union
>
> Go code for unions generated by Thrift 0.9.3 fails to consider map/set/list 
> fields of a union when enforcing that the number of fields set is 1. For 
> example:
> {code}
> union Foo{
>     1: map<bool,bool> u1
>     2: set<bool> u2,
>     3: list<bool> u3,
>     4: bool u4,
> }
> {code}
> Produces Go code:
> {code}
> type Foo struct {
>       U1 map[bool]bool `thrift:"u1,1" json:"u1,omitempty"`
>       U2 map[bool]bool `thrift:"u2,2" json:"u2,omitempty"`
>       U3 []bool        `thrift:"u3,3" json:"u3,omitempty"`
>       U4 *bool         `thrift:"u4,4" json:"u4,omitempty"`
> }
> ...
> func (p *Foo) CountSetFieldsFoo() int {
>       count := 0
>       if p.IsSetU4() {
>               count++
>       }
>       return count
> }
> ...
> func (p *Foo) Write(oprot thrift.TProtocol) error {
>       if c := p.CountSetFieldsFoo(); c != 1 {
>               return fmt.Errorf("%T write union: exactly one field must be 
> set (%d set).", p, c)
>       }
> ...
> {code}
> Due to this bug, Thrift will complain that a union with only the fields u1, 
> u2, or u3 set has no fields set. And it will allow all four fields to be set.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to