Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lok52 committed Mar 12, 2024
1 parent 9148543 commit adfef22
Show file tree
Hide file tree
Showing 25 changed files with 3,896 additions and 1,732 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ Cargo.lock

.pre-commit-config.yaml
config.toml

**/*.swagger.yaml
6 changes: 5 additions & 1 deletion actix-prost-build/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,13 @@ impl ConversionsGenerator {
_ => {
let convert = &self.convert_prefix;

let from = match field_conversions.len() + extra_field_conversions.len() {
0 => quote!(_from),
_ => quote!(from),
};
quote!(
impl #convert<#from_struct_ident> for #to_struct_ident {
fn try_convert(from: #from_struct_ident) -> Result<Self, String> {
fn try_convert(#from: #from_struct_ident) -> Result<Self, String> {
Ok(Self {
#(#field_conversions,)*
#(#extra_field_conversions,)*
Expand Down
3 changes: 2 additions & 1 deletion actix-prost-build/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config::HttpRule, conversions::ConversionsGenerator, method::Method, Config};
use crate::{config::HttpRule, method::Method, Config};
use proc_macro2::TokenStream;
use prost_build::{Service, ServiceGenerator};
use quote::quote;
Expand Down Expand Up @@ -135,6 +135,7 @@ impl ServiceGenerator for ActixGenerator {

#[cfg(feature = "conversions")]
{
use crate::conversions::ConversionsGenerator;
let conversions = ConversionsGenerator::new().ok().map(|mut g| {
g.messages = Rc::clone(&self.messages);
g.create_conversions(&service)
Expand Down
4 changes: 3 additions & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
actix-prost = { path = "../actix-prost" }
actix-prost-macros = { path = "../actix-prost-macros" }
async-trait = "0.1"
convert-trait ={ path = "../convert-trait" }
tonic = "0.8"
prost = "0.11"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
Expand All @@ -15,9 +16,10 @@ actix-web = "4"
http = "0.2"
serde_json = "1.0"
serde_with = { version = "2.0", features = ["base64"] }
ethers = "2.0.14"

[build-dependencies]
actix-prost-build = { path = "../actix-prost-build" }
actix-prost-build = { path = "../actix-prost-build", features = ["conversions"]}
tonic-build = "0.8"
prost-build = "0.11"

Expand Down
22 changes: 10 additions & 12 deletions tests/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use actix_prost_build::{ActixGenerator, GeneratorList};
use prost_build::{Config, ServiceGenerator};
use std::path::Path;
use std::{
env,
path::{Path, PathBuf},
};

// custom function to include custom generator
fn compile(
Expand All @@ -11,6 +14,10 @@ fn compile(
let mut config = Config::new();
config
.service_generator(generator)
.file_descriptor_set_path(
PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"))
.join("file_descriptor_set.bin"),
)
.out_dir("src/proto")
.bytes(["."])
.compile_well_known_types()
Expand All @@ -19,17 +26,6 @@ fn compile(
.protoc_arg("grpc_api_configuration=proto/http_api.yaml,output_format=yaml")
.type_attribute(".", "#[actix_prost_macros::serde]");

// for path in protos.iter() {
// println!("cargo:rerun-if-changed={}", path.as_ref().display())
// }

// for path in includes.iter() {
// // Cargo will watch the **entire** directory recursively. If we
// // could figure out which files are imported by our protos we
// // could specify only those files instead.
// println!("cargo:rerun-if-changed={}", path.as_ref().display())
// }

config.compile_protos(protos, includes)?;
Ok(())
}
Expand All @@ -42,8 +38,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
compile(
&[
"proto/rest.proto",
"proto/simple.proto",
"proto/types.proto",
"proto/errors.proto",
"proto/conversions.proto",
],
&["proto/", "proto/googleapis", "proto/grpc-gateway"],
gens,
Expand Down
38 changes: 38 additions & 0 deletions tests/proto/conversions.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
syntax = "proto3";
package conversions;

import "convert_options.proto";

option go_package = "github.com/blockscout/actix-prost/tests";

service ConversionsRPC { rpc ConvertRPC(ConversionsRequest) returns (ConversionsResponse); }

message Nested {
string address = 3 [ (convert_options.convert) = { type : "ethers::types::Address" } ];
}

message MapValue {
string address = 1 [ (convert_options.convert) = { type : "ethers::types::Address" } ];
}

message ConversionsRequest {
option (convert_options.extra_fields) = { name: "field1", type: "String" };
option (convert_options.extra_fields) = { name: "field2", type: "i32" };
map<string, MapValue> map_field = 1;

enum NestedEnum {
NESTED_OK = 0;
NESTED_ERROR = 1;
}

string query = 2 [ (convert_options.convert) = { override : "Default::default()" } ];
repeated string addresses = 3 [ (convert_options.convert) = { type : "std::collections::HashSet<ethers::types::Address>" } ];
NestedEnum nested_enum = 4;
Nested nested = 5 [ (convert_options.convert) = { required : true } ];
}

message ConversionsResponse {
string address = 1 [ (convert_options.convert) = { type : "ethers::types::Address" } ];
Nested nested = 2;
map<string, MapValue> map_field = 3;
}
20 changes: 20 additions & 0 deletions tests/proto/convert_options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";

package convert_options;
option go_package = "github.com/blockscout/actix-prost/convert_options";

import "google/protobuf/descriptor.proto";

message ConvertOptions {
string type = 1;
string override = 2;
bool required = 3;
}

message ExtraFieldOptions {
string name = 1;
string type = 2;
}

extend google.protobuf.MessageOptions { repeated ExtraFieldOptions extra_fields = 50000; }
extend google.protobuf.FieldOptions { optional ConvertOptions convert = 50001; }
172 changes: 0 additions & 172 deletions tests/proto/errors.swagger.yaml

This file was deleted.

6 changes: 5 additions & 1 deletion tests/proto/http_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ http:
body: "*"
response_body: "foo"

- selector: "rest.SimpleRPC.PostRPC"
- selector: "simple.SimpleRPC.PostRPC"
post: /rest/post/{foo}
body: long_name

Expand Down Expand Up @@ -64,3 +64,7 @@ http:
- selector: "errors.ErrorsRPC.Error"
post: /errors/{code}
body: "message"

- selector: "conversions.ConversionsRPC.ConvertRPC"
post: /conversions
body: "*"
2 changes: 0 additions & 2 deletions tests/proto/rest.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ service RestRPC {
rpc PostResponseGetRPC(Post) returns (Get);
}

service SimpleRPC { rpc PostRPC(Post) returns (Post); }

message Get {
string foo = 1;
int64 bar = 2;
Expand Down
Loading

0 comments on commit adfef22

Please sign in to comment.