|
| 1 | +use tailcall_valid::Valid; |
| 2 | + |
| 3 | +use crate::core::config::{Config, Grpc, Http, Resolver, ResolverSet}; |
| 4 | +use crate::core::Transform; |
| 5 | + |
| 6 | +pub struct ConnectRPC; |
| 7 | + |
| 8 | +impl Transform for ConnectRPC { |
| 9 | + type Value = Config; |
| 10 | + type Error = String; |
| 11 | + |
| 12 | + fn transform(&self, mut config: Self::Value) -> Valid<Self::Value, Self::Error> { |
| 13 | + for type_ in config.types.values_mut() { |
| 14 | + for field_ in type_.fields.values_mut() { |
| 15 | + let new_resolvers = field_ |
| 16 | + .resolvers |
| 17 | + .0 |
| 18 | + .iter() |
| 19 | + .map(|resolver| match resolver { |
| 20 | + Resolver::Grpc(grpc) => Resolver::Http(Http::from(grpc.clone())), |
| 21 | + other => other.clone(), |
| 22 | + }) |
| 23 | + .collect(); |
| 24 | + |
| 25 | + field_.resolvers = ResolverSet(new_resolvers); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + Valid::succeed(config) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl From<Grpc> for Http { |
| 34 | + fn from(grpc: Grpc) -> Self { |
| 35 | + let url = grpc.url; |
| 36 | + let body = grpc.body.or_else(|| { |
| 37 | + // if body isn't present while transforming the resolver, we need to provide an |
| 38 | + // empty object. |
| 39 | + Some(serde_json::Value::Object(serde_json::Map::new())) |
| 40 | + }); |
| 41 | + |
| 42 | + // remove the last |
| 43 | + // method: package.service.method |
| 44 | + // remove the method from the end. |
| 45 | + let parts = grpc.method.split(".").collect::<Vec<_>>(); |
| 46 | + let method = parts[..parts.len() - 1].join(".").to_string(); |
| 47 | + let endpoint = parts[parts.len() - 1].to_string(); |
| 48 | + |
| 49 | + let new_url = format!("{}/{}/{}", url, method, endpoint); |
| 50 | + let headers = grpc.headers; |
| 51 | + let batch_key = grpc.batch_key; |
| 52 | + let dedupe = grpc.dedupe; |
| 53 | + let select = grpc.select; |
| 54 | + let on_response_body = grpc.on_response_body; |
| 55 | + |
| 56 | + Self { |
| 57 | + url: new_url, |
| 58 | + body: body.map(|b| b.to_string()), |
| 59 | + method: crate::core::http::Method::POST, |
| 60 | + headers, |
| 61 | + batch_key, |
| 62 | + dedupe, |
| 63 | + select, |
| 64 | + on_response_body, |
| 65 | + ..Default::default() |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use serde_json::{json, Value}; |
| 73 | + |
| 74 | + use super::*; |
| 75 | + use crate::core::config::KeyValue; |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn test_grpc_to_http_basic_conversion() { |
| 79 | + let grpc = Grpc { |
| 80 | + url: "http://localhost:8080".to_string(), |
| 81 | + method: "package.service.method".to_string(), |
| 82 | + body: Some(json!({"key": "value"})), |
| 83 | + headers: Default::default(), |
| 84 | + batch_key: Default::default(), |
| 85 | + dedupe: Default::default(), |
| 86 | + select: Default::default(), |
| 87 | + on_response_body: Default::default(), |
| 88 | + }; |
| 89 | + |
| 90 | + let http = Http::from(grpc); |
| 91 | + |
| 92 | + assert_eq!(http.url, "http://localhost:8080/package.service/method"); |
| 93 | + assert_eq!(http.method, crate::core::http::Method::POST); |
| 94 | + assert_eq!(http.body, Some(r#"{"key":"value"}"#.to_string())); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_grpc_to_http_empty_body() { |
| 99 | + let grpc = Grpc { |
| 100 | + url: "http://localhost:8080".to_string(), |
| 101 | + method: "package.service.method".to_string(), |
| 102 | + body: Default::default(), |
| 103 | + headers: Default::default(), |
| 104 | + batch_key: Default::default(), |
| 105 | + dedupe: Default::default(), |
| 106 | + select: Default::default(), |
| 107 | + on_response_body: Default::default(), |
| 108 | + }; |
| 109 | + |
| 110 | + let http = Http::from(grpc); |
| 111 | + |
| 112 | + assert_eq!(http.body, Some("{}".to_string())); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_grpc_to_http_with_headers() { |
| 117 | + let grpc = Grpc { |
| 118 | + url: "http://localhost:8080".to_string(), |
| 119 | + method: "a.b.c".to_string(), |
| 120 | + body: None, |
| 121 | + headers: vec![KeyValue { key: "X-Foo".to_string(), value: "bar".to_string() }], |
| 122 | + batch_key: Default::default(), |
| 123 | + dedupe: Default::default(), |
| 124 | + select: Default::default(), |
| 125 | + on_response_body: Default::default(), |
| 126 | + }; |
| 127 | + |
| 128 | + let http = Http::from(grpc); |
| 129 | + |
| 130 | + assert_eq!(http.url, "http://localhost:8080/a.b/c"); |
| 131 | + assert_eq!( |
| 132 | + http.headers |
| 133 | + .iter() |
| 134 | + .find(|h| h.key == "X-Foo") |
| 135 | + .unwrap() |
| 136 | + .value, |
| 137 | + "bar".to_string() |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_grpc_to_http_all_fields() { |
| 143 | + let grpc = Grpc { |
| 144 | + url: "http://localhost:8080".to_string(), |
| 145 | + method: "package.service.method".to_string(), |
| 146 | + body: Some(json!({"key": "value"})), |
| 147 | + headers: vec![KeyValue { key: "X-Foo".to_string(), value: "bar".to_string() }], |
| 148 | + batch_key: vec!["batch_key_value".to_string()], |
| 149 | + dedupe: Some(true), |
| 150 | + select: Some(Value::String("select_value".to_string())), |
| 151 | + on_response_body: Some("on_response_body_value".to_string()), |
| 152 | + }; |
| 153 | + |
| 154 | + let http = Http::from(grpc); |
| 155 | + |
| 156 | + assert_eq!(http.url, "http://localhost:8080/package.service/method"); |
| 157 | + assert_eq!(http.method, crate::core::http::Method::POST); |
| 158 | + assert_eq!(http.body, Some(r#"{"key":"value"}"#.to_string())); |
| 159 | + assert_eq!( |
| 160 | + http.headers |
| 161 | + .iter() |
| 162 | + .find(|h| h.key == "X-Foo") |
| 163 | + .unwrap() |
| 164 | + .value, |
| 165 | + "bar".to_string() |
| 166 | + ); |
| 167 | + assert_eq!(http.batch_key, vec!["batch_key_value".to_string()]); |
| 168 | + assert_eq!(http.dedupe, Some(true)); |
| 169 | + assert_eq!(http.select, Some(Value::String("select_value".to_string()))); |
| 170 | + assert_eq!( |
| 171 | + http.on_response_body, |
| 172 | + Some("on_response_body_value".to_string()) |
| 173 | + ); |
| 174 | + } |
| 175 | +} |
0 commit comments