Skip to content

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

Merged
LucioFranco merged 2 commits intohyperium:masterfrom
adamhjk:prost-config-expose
Oct 8, 2019
Merged

feat(build): Expose prost-build type_attributes and field_attribu…#60
LucioFranco merged 2 commits intohyperium:masterfrom
adamhjk:prost-config-expose

Conversation

@adamhjk
Copy link
Copy Markdown
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
Copy Markdown
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants