Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointers for slices and maps #807

Closed
jkawamoto opened this issue Oct 10, 2017 · 1 comment
Closed

Pointers for slices and maps #807

jkawamoto opened this issue Oct 10, 2017 · 1 comment

Comments

@jkawamoto
Copy link

In arm package, some structures have pointers for slices and maps, for example, Tags of arm.batch.Account and Value of arm.batch.AccountListResult. Is there a particular reason for using pointers instead of having slices and maps directly?

Slice and map variables are nullable. I think it seems okay to not use pointers.

Currently, since those structures have pointers, we have to check whether they are nil or not, and it might be a cause of bugs.

For example, batch.AccountListResult.Value is a pointer as follows

type AccountListResult struct {
    autorest.Response `json:"-"`
    Value             *[]Account `json:"value,omitempty"`
    NextLink          *string    `json:"nextLink,omitempty"`
}

We then have to check whether Value is nil or not before enumerating it:

var res AccountListResult

res, err = cli.List() // cli is an instance of batch.AccountClient
if err !=nil {
  // error handling
}
if res == nil {
  // another error handling
}
for _, account := range *res.Value {
  fmt.Println(account)
}

If the type of Value is []Account, we don't need to check res == nil in the above code.

@marstr
Copy link
Member

marstr commented Oct 10, 2017

Thanks for reaching out, @jkawamoto. This is a known issue, and something that @jhendrixMSFT is changing in Azure/autorest.go#27

@marstr marstr closed this as completed Oct 10, 2017
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants