Skip to content
New issue

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

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

Already on GitHub? # to your account

gen: Support go.label on struct fields #377

Merged
merged 2 commits into from
Aug 27, 2018
Merged

gen: Support go.label on struct fields #377

merged 2 commits into from
Aug 27, 2018

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Aug 27, 2018

This adds support for the go.label annotation on struct fields.
Specifying this label on struct fields has two effects:

  • The name used for the field during logging with Zap will use the
    value specified in go.label.
  • The name of the field used while converting the struct to/from JSON
    will use the value specified in go.label unless a different
    value was specified with a go.tag = 'json:".."' annotation. I don't
    completely support making any assurances about the JSON
    representation of Thrift types but it seems to me that that ship
    sailed when we added the json tags.

Note that this intentionally leaves out the String() methods for
structs. Those methods currently output valid Go syntax with an
implication that the printed code can be used to rebuild the Go object.

Resolves #370

(This PR has another refactoring/cleanup commit that can be reviewed
separately.)

@abhinav abhinav requested review from mh-park and kriskowal and removed request for mh-park August 27, 2018 19:00
@abhinav
Copy link
Contributor Author

abhinav commented Aug 27, 2018

CC @cl1337

This generalizes the labelling logic (go.label) to support any
NamedEntity. To clarify: This isn't actually adding support for new
entities, only generalizing the logic so that it can be re-used for
struct field labels in the future.

As part of generalization, this logic was moved to its own file.
This adds support for the `go.label` annotation on struct fields.
Specifying this label on struct fields has two effects:

- The name used for the field during logging with Zap will use the value
  specified in `go.label`.
- The name of the field used while converting the struct to/from JSON
  will use the value specified in `go.label` **unless** a different
  value was specified with a `go.tag = 'json:".."'` annotation. I don't
  completely support making any assurances about the JSON representation
  of Thrift types but it seems to me that that ship sailed when we added
  the `json` tags.

Resolves #370
@abhinav abhinav force-pushed the abg/go-label-fields branch from faa2354 to 8687258 Compare August 27, 2018 19:44
@abhinav abhinav mentioned this pull request Aug 27, 2018
11 tasks
@mh-park
Copy link
Contributor

mh-park commented Aug 27, 2018

@abhinav Should we be using go.label for String()?

@abhinav
Copy link
Contributor Author

abhinav commented Aug 27, 2018

Good question. No, because the String() for structs currently returns Go
code that you would use to instantiate that struct. Changing it in String()
will break that code. This is basically a discrepancy between String() for
structs and String() for enums.

I'll add this information to the commit message.

@abhinav abhinav merged commit d586327 into dev Aug 27, 2018
@abhinav abhinav deleted the abg/go-label-fields branch August 27, 2018 21:33
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants