From be8864775227f6543a9cb00b36abb66bf26362f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=AA=E5=8D=93=E5=BF=97?= Date: Wed, 28 Jun 2023 09:42:19 +0800 Subject: [PATCH] feat: extract boilerplate code of `on_configure` to function (DRY) (#396) --- .../wasm-rust/extensions/say-hello/src/lib.rs | 33 ++++------------ plugins/wasm-rust/src/rule_matcher.rs | 38 +++++++++++++++---- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/plugins/wasm-rust/extensions/say-hello/src/lib.rs b/plugins/wasm-rust/extensions/say-hello/src/lib.rs index 7c6302c6b..458a7a466 100644 --- a/plugins/wasm-rust/extensions/say-hello/src/lib.rs +++ b/plugins/wasm-rust/extensions/say-hello/src/lib.rs @@ -13,12 +13,12 @@ // limitations under the License. use higress_wasm_rust::log::Log; -use higress_wasm_rust::rule_matcher::RuleMatcher; +use higress_wasm_rust::rule_matcher::{on_configure, RuleMatcher}; use proxy_wasm::traits::{Context, HttpContext, RootContext}; use proxy_wasm::types::{Action, ContextType, LogLevel}; use serde::Deserialize; -use serde_json::{from_slice, Value}; use std::cell::RefCell; +use std::ops::DerefMut; use std::rc::Rc; proxy_wasm::main! {{ @@ -53,29 +53,12 @@ impl Context for SayHelloRoot {} impl RootContext for SayHelloRoot { fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool { - let config_buffer = match self.get_plugin_configuration() { - None => { - self.log - .error("Error when configuring RootContext, no configuration supplied"); - return false; - } - Some(bytes) => bytes, - }; - - let value = match from_slice::(config_buffer.as_slice()) { - Err(error) => { - self.log.error( - format!("cannot parse plugin configuration JSON string: {}", error).as_str(), - ); - return false; - } - Ok(value) => value, - }; - - self.rule_matcher - .borrow_mut() - .parse_rule_config(&value) - .is_ok() + on_configure( + self, + _plugin_configuration_size, + self.rule_matcher.borrow_mut().deref_mut(), + &self.log, + ) } fn create_http_context(&self, _context_id: u32) -> Option> { diff --git a/plugins/wasm-rust/src/rule_matcher.rs b/plugins/wasm-rust/src/rule_matcher.rs index b8de27662..10a874dd2 100644 --- a/plugins/wasm-rust/src/rule_matcher.rs +++ b/plugins/wasm-rust/src/rule_matcher.rs @@ -14,11 +14,13 @@ use crate::error::WasmRustError; use crate::internal::{get_http_request_header, get_property}; +use crate::log::Log; use proxy_wasm::hostcalls::log; +use proxy_wasm::traits::RootContext; use proxy_wasm::types::LogLevel; -use serde_json::{Map, Value}; -use std::collections::HashSet; use serde::de::DeserializeOwned; +use serde_json::{from_slice, Map, Value}; +use std::collections::HashSet; enum Category { Route, @@ -55,12 +57,9 @@ pub struct RuleMatcher { impl RuleMatcher where - PluginConfig: Default+DeserializeOwned, + PluginConfig: Default + DeserializeOwned, { - pub fn parse_rule_config( - &mut self, - config: &Value, - ) -> Result<(), WasmRustError> { + pub fn parse_rule_config(&mut self, config: &Value) -> Result<(), WasmRustError> { let empty_object = Map::new(); let empty_vec = Vec::new(); @@ -216,3 +215,28 @@ where false } } + +pub fn on_configure( + root_context: &RC, + _plugin_configuration_size: usize, + rule_matcher: &mut RuleMatcher, + log: &Log, +) -> bool { + let config_buffer = match root_context.get_plugin_configuration() { + None => { + log.error("Error when configuring RootContext, no configuration supplied"); + return false; + } + Some(bytes) => bytes, + }; + + let value = match from_slice::(config_buffer.as_slice()) { + Err(error) => { + log.error(format!("cannot parse plugin configuration JSON string: {}", error).as_str()); + return false; + } + Ok(value) => value, + }; + + rule_matcher.parse_rule_config(&value).is_ok() +}