Skip to content

Support named structs in gen-accessors.go. #587

@dmitshur

Description

@dmitshur

@gmlewis, I wanted to ask you a question regarding what you said in #577 (comment):

(Note that named structs themselves don't get accessors [intentionally]... there is no GetUser method, for example, because you can just do something like resp.User.GetName() for example and it will just work... even if resp.User == nil... does that answer the question?)

Did you take into account that the above won't work if accessing more than 1 field in a single expression?

For example, if the caller wants to get the name of user that created an issue, this will be okay as you mentioned:

name := issue.User.GetName()

Because of the if u == nil { return "" } check in (u *User) GetName().

But this will panic:

planName := issue.User.Plan.GetName()

// panic: runtime error: invalid memory address or nil pointer dereference

Because issue.User is (*github.User)(nil), and accessing Plan field of that causes a run-time panic:

The following rules apply to selectors:
[...]
5. If x is of pointer type and has the value nil and x.f denotes a struct field, assigning to or evaluating x.f causes a run-time panic.

(Source: https://golang.org/ref/spec#Selectors.)

Did you take that into account when making the decision to omit the named structs? If so, what's the expected work around? If not, do we want to make a change, or is it fine?

This is just a question at this point, I don't have any suggestions for next steps yet.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions