[
https://issues.apache.org/jira/browse/THRIFT-4011?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15823163#comment-15823163
]
ASF GitHub Bot commented on THRIFT-4011:
----------------------------------------
Github user dcelasun commented on the issue:
https://github.com/apache/thrift/pull/1156
Hey @Jens-G, I'm not sure I follow you, what does this have anything to do
with maps? Assuming you meant sets, the docs say:
> An unordered set of unique elements. Translates to an STL set, Java
HashSet, set in Python, etc. Note: PHP does not support sets, so it is treated
similar to a List
Similar to PHP, Go does not have a native type for sets, so the best thing
to do is to treat it similar to a list.
> Given that, I would say it could be one option to error, when the user
inserts a duplicate.
This is not possible, because the caller doesn't "insert" anything, they
simply return a slice. Consider the following:
```thrift
service Foo {
set<string> bar() throws (1: Something error)
}
```
This generates an interface called `Foo` with the following method:
```go
type Foo interface {
Bar() ([]string, error)
}
```
So the user simply returns a string slice, the Thrift library has no
control over it. Once `Foo` returns, it's too late for Thrift itself to return
an error, only panic. Speaking of which, panicking in case of ***programmer
error*** is very common and idiomatic in Go. The standard library is full of
such panics (e.g search for "misuse"
[here](https://golang.org/src/sync/waitgroup.go)). I would consider returning a
non-unique slice for a Thrift set a programming error (and hence deserving a
panic), but if you disagree, I can update the PR with deduplication in the
library.
> Sets of Thrift structs generate Go code that can't be serialized to JSON
> ------------------------------------------------------------------------
>
> Key: THRIFT-4011
> URL: https://issues.apache.org/jira/browse/THRIFT-4011
> Project: Thrift
> Issue Type: Bug
> Components: Go - Compiler
> Reporter: Can Celasun
>
> Consider the following structs:
> {code}
> struct Foo {
> 1: optional string foo
> }
> struct Bar {
> 1: optional set<Foo> foos
> }
> {code}
> This compiles into the following Go code:
> {code}
> type Bar struct {
> Foos map[*Foo]struct{} `thrift:"foos,1" db:"foos" json:"foos,omitempty"`
> }
> {code}
> Even though the generated code has tags for JSON support, Bar can't be
> serialized to JSON:
> {code}
> json: unsupported type: map[*Foo]struct {}
> {code}
> One solution would be to use slices, not maps, for Thrift sets. Thoughts?
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)