diff --git a/docs/assets/images/rust-project-structure.png b/docs/assets/images/rust-project-structure.png new file mode 100644 index 0000000..4e30dc1 Binary files /dev/null and b/docs/assets/images/rust-project-structure.png differ diff --git a/docs/assets/themes/prima.css b/docs/assets/themes/prima.css index c6257a7..ba165c6 100644 --- a/docs/assets/themes/prima.css +++ b/docs/assets/themes/prima.css @@ -22,7 +22,7 @@ html * { --r-main-font-size: 40px; --r-main-color: #21283b; --r-block-margin: 20px; - --r-heading-margin: 0 0 20px 0; + --r-heading-margin: 0 0 50px 0; --r-heading-font: Lato, League Gothic, Impact, sans-serif; --r-heading-color: #21283b; --r-heading-line-height: 1.2; diff --git a/docs/practical_grpc.md b/docs/practical_grpc.md index d9b2a49..ee59761 100644 --- a/docs/practical_grpc.md +++ b/docs/practical_grpc.md @@ -19,8 +19,7 @@ + Manual project setup + Build script + Exposing a library - + Supporting multiple tonic versions - + CI/CD + + CI & Release flow + The Buf tool + Checking for breaking changes + How to release @@ -50,7 +49,7 @@ --- -## Ingredients to build a gRPC API +## Ingredients to build a **gRPC API**
@@ -60,7 +59,7 @@
---
-## Defining our API
+## Defining our **API**
@@ -68,7 +67,7 @@
## Project structure
-
+
https://github.com/primait/es-policy-grpc
@@ -76,13 +75,13 @@
## Project structure
-
+
https://github.com/primait/es-policy-grpc
---
-## Defining a request
+## Defining a **request**
```proto
syntax = "proto3";
@@ -116,7 +115,7 @@ message IssuePolicyRequest {
---
-## Defining a request
+## Defining a **request**
```proto
syntax = "proto3";
@@ -154,7 +153,7 @@ enum RoadsideAssistanceTier {
---
-## Defining a response
+## Defining a **response**
```proto
syntax = "proto3";
@@ -168,4 +167,344 @@ message IssuePolicyResponse {
---
-## About backwards compatibility
+## About **backwards** compatibility
+
+---
+
+### Required fields don't exist, everything has a default value
+
++ **Strings** => The empty string.
++ **Bytes** => Empty bytes.
++ **Bools** => False.
++ **Numeric** => Zero.
++ **Message** => Not set. Its exact value is language-dependent.
++ **Enums** => The first defined enum value, which must be 0.
+
+https://protobuf.dev/programming-guides/proto3/#default
+
+---
+
+### Enums must have an "**Unspecified**" variant
+
+```proto
+enum BundleSlug {
+ BUNDLE_SLUG_UNSPECIFIED = 0;
+ BUNDLE_SLUG_TERCEROS_BASICO = 1;
+ BUNDLE_SLUG_TERCEROS_AMPLIADO = 2;
+ BUNDLE_SLUG_TODO_RIESGO_CON_FRANQUICIA = 3;
+ BUNDLE_SLUG_TODO_RIESGO_CON_FRANQUICIA_300 = 4;
+ BUNDLE_SLUG_TODO_RIESGO_CON_FRANQUICIA_500 = 5;
+}
+```
+
+https://protobuf.dev/best-practices/dos-donts/#unspecified-enum
+
+---
+
+### Field tags must **never** be reused
+
+And deleted fields must be marked as reserved
+
+```proto
+message AmendWithdrawalRequest {
+ string policy_id = 1;
+ google.protobuf.Timestamp requested_at = 2;
+
+ reserved 3;
+ reserved "interruption_at";
+
+ optional string description = 4;
+ oneof reason {
+ es_policy_grpc.messages.withdraw_policy.request.v1.CustomerWithdrawReason customer = 5;
+ }
+}
+```
+
+https://protobuf.dev/best-practices/dos-donts#reuse-number
+
+---
+
+### Learn more about **Do's** and **Dont's**
+
+https://protobuf.dev/best-practices/dos-donts
+
+---
+
+## Defining a **service**
+
+```proto
+syntax = "proto3";
+
+package es_policy_grpc.service.v1;
+
+import "es_policy_grpc/messages/issue_policy/request/v1/request.proto";
+import "es_policy_grpc/messages/issue_policy/response/v1/response.proto";
+
+service PolicyManagementService {
+ rpc IssuePolicy(es_policy_grpc.messages.issue_policy.request.v1.IssuePolicyRequest) returns (es_policy_grpc.messages.issue_policy.response.v1.IssuePolicyResponse);
+}
+```
+
+---
+
+
+
+---
+
+### Build script
+
+```rust
+use std::io::Result;
+
+fn main() -> Result<()> {
+ // List of proto files containing a message definition
+ let proto_files = &[
+ //Domain
+ "proto/es_policy_grpc/domain/v1/address.proto",
+ "proto/es_policy_grpc/domain/v1/bundle.proto",
+ // etc.
+ // Messages
+ "proto/es_policy_grpc/messages/issue_policy/request/v1/request.proto",
+ "proto/es_policy_grpc/messages/issue_policy/response/v1/response.proto",
+ // Services
+ "proto/es_policy_grpc/service/v1/service.proto",
+ ];
+
+ // Name of the folder containing the proto definitions
+ let proto_folder = "proto";
+
+ tonic_prost_build::configure()
+ .protoc_arg("--experimental_allow_proto3_optional")
+ .compile_protos(proto_files, &[proto_folder])
+ .unwrap();
+
+ Ok(())
+}
+```
+
+---
+
+### Exposing the generated code
+
+```rust
+// src/lib.rs
+
+pub mod domain {
+ pub mod v1 {
+ include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.domain.v1.rs",));
+ }
+}
+
+pub mod messages {
+ pub mod issue_policy {
+ pub mod request {
+ pub mod v1 {
+ include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.messages.issue_policy.request.v1.rs"));
+ }
+ }
+
+ pub mod response {
+ pub mod v1 {
+ include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.messages.issue_policy.response.v1.rs"));
+ }
+ }
+ }
+}
+
+pub mod policy_service {
+ pub mod v1 {
+ include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.service.v1.rs"));
+ }
+}
+```
+
+---
+
+