From 416cf9e6bd25d9c08073964f8a8cd8fda5261df0 Mon Sep 17 00:00:00 2001 From: Gaurav Saini Date: Sun, 26 Sep 2021 11:42:58 +0530 Subject: [PATCH] Added parsing of files only when kind is 'Endpoint' --- boilerplate/spec/hello/root.yaml | 4 ++-- boilerplate/spec/hello/world.yaml | 4 ++-- boilerplate/spec/root.yaml | 6 +++--- src/endpoint.rs | 4 ++++ src/spec_parser.rs | 25 +++++++++++++++---------- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/boilerplate/spec/hello/root.yaml b/boilerplate/spec/hello/root.yaml index ca90326..830c3e3 100644 --- a/boilerplate/spec/hello/root.yaml +++ b/boilerplate/spec/hello/root.yaml @@ -1,5 +1,5 @@ -apiVersion: mocks/v1 -kind: MockEndpoint +apiVersion: apps/v1 +kind: Endpoint spec: requests: - description: I serve the path /hello diff --git a/boilerplate/spec/hello/world.yaml b/boilerplate/spec/hello/world.yaml index c4a5f32..e529969 100644 --- a/boilerplate/spec/hello/world.yaml +++ b/boilerplate/spec/hello/world.yaml @@ -1,5 +1,5 @@ -apiVersion: mocks/v1 -kind: MockEndpoint +apiVersion: apps/v1 +kind: Endpoint spec: requests: - description: Serve static files by providing paths! diff --git a/boilerplate/spec/root.yaml b/boilerplate/spec/root.yaml index cfe328c..c6f7f88 100644 --- a/boilerplate/spec/root.yaml +++ b/boilerplate/spec/root.yaml @@ -1,5 +1,5 @@ -apiVersion: mocks/v1 -kind: MockEndpoint +apiVersion: apps/v1 +kind: Endpoint spec: requests: - description: Returning the homepage of Mockerino @@ -12,4 +12,4 @@ spec: Hello! Welcome to Mockerino!
Click here to see how root.yaml serves the path root. - description: A post request which says "OK" method: POST - raw_body: OK \ No newline at end of file + raw_body: OK diff --git a/src/endpoint.rs b/src/endpoint.rs index 67922a5..745a1f9 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -19,4 +19,8 @@ impl Endpoint { pub fn request(&self) -> &Vec { &self.spec.requests } + + pub fn kind(&self) -> String { + self.kind.clone() + } } diff --git a/src/spec_parser.rs b/src/spec_parser.rs index a7e12c0..86363a0 100644 --- a/src/spec_parser.rs +++ b/src/spec_parser.rs @@ -46,16 +46,21 @@ fn process_file( let endpoint = parse_yaml_file(path)?; - // Handle dynamic routes here - if path_param_regex.find(request_path.as_ref()).is_some() { - // TODO: Add capability to use dynamic routes. - process_dynamic_path(endpoint, request_path.as_ref()) - } else { - Ok(endpoint - .request() - .iter() - .map(|req| RequestWithMetadata::new(req.clone(), request_path.to_string())) - .collect()) + match endpoint.kind().as_ref() { + "Endpoint" => { + // Handle dynamic routes here + if path_param_regex.find(request_path.as_ref()).is_some() { + // TODO: Add capability to use dynamic routes. + process_dynamic_path(endpoint, request_path.as_ref()) + } else { + Ok(endpoint + .request() + .iter() + .map(|req| RequestWithMetadata::new(req.clone(), request_path.to_string())) + .collect()) + } + } + _ => Err(anyhow::Error::msg("Unsupported 'kind' in spec file")), } }