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

feat(build): Expose prost-build type_attributes and field_attribu… #60

Merged
merged 2 commits into from
Oct 8, 2019

Conversation

adamhjk
Copy link
Contributor

@adamhjk adamhjk commented Oct 8, 2019

Motivation

When using tonic, you sometimes want to also serialize the generated types with Serde.

Solution

Expose the underlying prost-build.Config options for type_attributes and field_attributes, which allows you to extend the generated types.

   tonic_build::configure()
        .out_dir(tmp)
        .format(false)
        .type_attribute(".", "#[derive(Serialize, Deserialize)]")
        .type_attribute(".", "#[serde(rename_all = \"camelCase\")]")
        .field_attribute("in", "#[serde(rename = \"in\")]")
        .compile(&["tests/protos/wellknown.proto"], &["tests/protos"])
        .unwrap();

Would make the resulting generated code serializable, deserializable, camelCase, and preserve 'in' vs the prost generated 'in_'.

When generating ctags/universal-ctags, a 'tags' file is generated. This
commit adds any generted 'tags' file to .gitignore.
This commit exposes the `type_attribute` and `field_attribute`
configuration settings from Prost. These are useful to tweak/extend the
generated types.

For example:

```
tonic_build::configure()
    .out_dir(tmp)
    .format(false)
    .type_attribute(".", "#[derive(Serialize, Deserialize)]")
    .type_attribute(".", "#[serde(rename_all = \"camelCase\")]")
    .field_attribute("in", "#[serde(rename = \"in\")]")
    .compile(&["tests/protos/wellknown.proto"], &["tests/protos"])
    .unwrap();
```

Would add the serde `Serialize` and `Deserialize` traits, while renaming
all the fields to camelCase, and having serde keep fields named `in`
named `in`, rather than Prost's `in_`, to every type generated by Prost.
@adamhjk adamhjk changed the title Prost config expose feat(build): Expose prost-build type_attributes and field_attributes Oct 8, 2019
Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

Great! thanks!

@LucioFranco LucioFranco changed the title feat(build): Expose prost-build type_attributes and field_attributes feat(build): Expose prost-build type_attributes and field_attribu… Oct 8, 2019
@LucioFranco LucioFranco merged commit 06ff619 into hyperium:master Oct 8, 2019
blittable pushed a commit to blittable/tonic that referenced this pull request Oct 22, 2019
…yperium#60)

* chore: Add tags to .gitignore

When generating ctags/universal-ctags, a 'tags' file is generated. This
commit adds any generted 'tags' file to .gitignore.

* feat(build): Expose type_attribute and field_attribute

This commit exposes the `type_attribute` and `field_attribute`
configuration settings from Prost. These are useful to tweak/extend the
generated types.

For example:

```
tonic_build::configure()
    .out_dir(tmp)
    .format(false)
    .type_attribute(".", "#[derive(Serialize, Deserialize)]")
    .type_attribute(".", "#[serde(rename_all = \"camelCase\")]")
    .field_attribute("in", "#[serde(rename = \"in\")]")
    .compile(&["tests/protos/wellknown.proto"], &["tests/protos"])
    .unwrap();
```

Would add the serde `Serialize` and `Deserialize` traits, while renaming
all the fields to camelCase, and having serde keep fields named `in`
named `in`, rather than Prost's `in_`, to every type generated by Prost.
# 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.

2 participants