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

structs: rename EnterpriseMeta constructor #9720

Merged
merged 1 commit into from
Feb 16, 2021
Merged

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented Feb 5, 2021

To match the Go convention.

@dnephin dnephin added the theme/internal-cleanup Used to identify tech debt, testing improvements, code refactoring, and non-impactful optimization label Feb 5, 2021
@hashicorp-ci
Copy link
Contributor

🤔 Double check that this PR does not require a changelog entry in the .changelog directory. Reference

@@ -43,7 +43,7 @@ func TestStreamingHealthServices_EmptySnapshot(t *testing.T) {
req := &structs.ServiceSpecificRequest{
Datacenter: "dc1",
ServiceName: "web",
EnterpriseMeta: structs.EnterpriseMetaInitializer(namespace),
EnterpriseMeta: structs.NewEnterpriseMeta(namespace),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious but is this still idiomatic naming if the new value isn't heap allocated. The reason I didn't go with New* originally was so that a caller could know by the name alone that this isn't doing an allocation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could just be my c/c++ background showing a little there.

Copy link
Contributor Author

@dnephin dnephin Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I have never heard of New functions only being used when a pointer or interface is returned.

I suspect that using func New for non-pointer returns is rare because generally structs in Go are either for data (in which case the zero value should be usable and there is no need for a constructor), or they are used for behaviour with pointer receiver methods (and a func New is necessary for creating one).

I think our use here is unique because normally we wouldn't need a func New, we only need it for enterprise differentiation.

I couldn't find any instances in the stdlib (likely because they would not use a constructor for this type). CodeReviewComments includes at least this one example: https://github.com/golang/go/wiki/CodeReviewComments#interfaces (the last example block in that section) showing a func New that returns a struct (not a pointer).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the compiler could still decide to heap allocate this variable if the code takes the address to it later on. The programmer doesn't necessary get to decide if it is heap or stack allocated, so most likely naming things with that assumption won't work out well.

@dnephin
Copy link
Contributor Author

dnephin commented Feb 5, 2021

The build failures are fixed by #9722. I'll rebase once that is merged.

Copy link
Member

@hanshasselberg hanshasselberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

To match the Go convention.
@dnephin dnephin force-pushed the dnephin/ent-meta-ergo-1 branch from d95924d to d1772ae Compare February 16, 2021 19:45
@vercel vercel bot temporarily deployed to Preview – consul February 16, 2021 19:46 Inactive
@vercel vercel bot temporarily deployed to Preview – consul-ui-staging February 16, 2021 19:46 Inactive
@dnephin dnephin merged commit d18e001 into master Feb 16, 2021
@dnephin dnephin deleted the dnephin/ent-meta-ergo-1 branch February 16, 2021 20:31
@hashicorp-ci
Copy link
Contributor

🍒 If backport labels were added before merging, cherry-picking will start automatically.

To retroactively trigger a backport after merging, add backport labels and re-run https://circleci.com/gh/hashicorp/consul/328222.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme/internal-cleanup Used to identify tech debt, testing improvements, code refactoring, and non-impactful optimization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants