Skip to content

Commit

Permalink
refactor(stats)!: change generated message field cases (#975)
Browse files Browse the repository at this point in the history
* refactor: change codegen to set snake case for api consistency

BREAKING CHANGE: change of message field name in API

* refactor(template): also set snake case for consistency

* test(stats): test more cases of serialization
  • Loading branch information
bragov4ik authored Jul 4, 2024
1 parent d7f035c commit 5485d92
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
4 changes: 2 additions & 2 deletions service-template/{{project-name}}-proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ fn compile(
.compile_well_known_types()
.protoc_arg("--openapiv2_out=swagger/v1")
.protoc_arg("--openapiv2_opt")
.protoc_arg("grpc_api_configuration=proto/v1/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name={{project-name}}")
.protoc_arg("grpc_api_configuration=proto/v1/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name={{project-name}},json_names_for_fields=false")
.bytes(["."])
.btree_map(["."])
.type_attribute(".", "#[actix_prost_macros::serde]")
.type_attribute(".", "#[actix_prost_macros::serde(rename_all=\"snake_case\")]")
// .field_attribute(
// ".blockscout.{{projectName}}.v1.<MessageName>.<DefaultFieldName>",
// "#[serde(default)]"
Expand Down
4 changes: 2 additions & 2 deletions stats/stats-proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn compile(
.compile_well_known_types()
.protoc_arg("--openapiv2_out=swagger")
.protoc_arg("--openapiv2_opt")
.protoc_arg("grpc_api_configuration=proto/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name=stats")
.protoc_arg("grpc_api_configuration=proto/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name=stats,json_names_for_fields=false")
.bytes(["."])
.type_attribute(".", "#[actix_prost_macros::serde]")
.type_attribute(".", "#[actix_prost_macros::serde(rename_all=\"snake_case\")]")
.field_attribute(
".blockscout.stats.v1.HealthCheckRequest.service",
"#[serde(default)]"
Expand Down
22 changes: 21 additions & 1 deletion stats/stats-proto/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const PRECISE_POINT_1: &str = r#"
{
"date": "2024-03-14",
"value": "188542399",
"isApproximate": false
"is_approximate": false
}
"#;

Expand All @@ -17,13 +17,23 @@ const PRECISE_POINT_2: &str = r#"
}
"#;

const IMPRECISE_POINT: &str = r#"
{
"date": "2024-03-14",
"value": "188542399",
"is_approximate": true
}
"#;

#[test]
fn is_approximate_serialization() {
// deserialize
let point: proto::Point = serde_json::from_str(PRECISE_POINT_1).unwrap();
assert!(!point.is_approximate);
let point: proto::Point = serde_json::from_str(PRECISE_POINT_2).unwrap();
assert!(!point.is_approximate);
let point: proto::Point = serde_json::from_str(IMPRECISE_POINT).unwrap();
assert!(point.is_approximate);

// serialize
let point = proto::Point {
Expand All @@ -36,4 +46,14 @@ fn is_approximate_serialization() {
serialized_point.replace([' ', '\n'], ""),
PRECISE_POINT_2.replace([' ', '\n'], "")
);
let point = proto::Point {
date: "2024-03-14".to_owned(),
value: "188542399".to_owned(),
is_approximate: true,
};
let serialized_point = serde_json::to_string(&point).unwrap();
assert_eq!(
serialized_point.replace([' ', '\n'], ""),
IMPRECISE_POINT.replace([' ', '\n'], "")
);
}
2 changes: 1 addition & 1 deletion stats/stats-proto/swagger/stats.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,6 @@ definitions:
type: string
value:
type: string
isApproximate:
is_approximate:
type: boolean
title: All integers are encoded as strings to prevent data loss

0 comments on commit 5485d92

Please sign in to comment.