Skip to content

Commit dc70244

Browse files
committed
xtask preprocess field
1 parent 6877bba commit dc70244

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ When updating or adding new parameters and endpoints, make changes directly in t
1212
Once your changes are merged, you can update this project as follows (you can also run tasks individually):
1313

1414
```bash
15-
cargo xtask fetch code-gen
15+
cargo xtask fetch preprocess code-gen
1616
```
1717

1818
This will:

preprocessed_openapi.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ paths:
349349
x-go-type: interface{}
350350
required: true
351351
x-rust-type: B
352-
x-rust-params-generic-parameter: <B>
353352
x-rust-generic-parameter: '<B: Serialize>'
353+
x-rust-params-generic-parameter: <B>
354354
delete:
355355
tags:
356356
- documents
@@ -1257,8 +1257,6 @@ paths:
12571257
x-go-type: interface{}
12581258
required: true
12591259
x-rust-type: B
1260-
x-rust-params-generic-parameter: <B>
1261-
x-rust-generic-parameter: '<B: Serialize>'
12621260
responses:
12631261
'200':
12641262
description: The document referenced by the ID was updated
@@ -1273,6 +1271,8 @@ paths:
12731271
application/json:
12741272
schema:
12751273
$ref: '#/components/schemas/ApiResponse'
1274+
x-rust-generic-parameter: '<B: Serialize>'
1275+
x-rust-params-generic-parameter: <B>
12761276
delete:
12771277
tags:
12781278
- documents

xtask/src/add_vendor_attributes.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,22 @@ pub fn add_vendor_attributes(doc_root: &mut Mapping) -> Result<(), String> {
6262
.supports_plain_text()
6363
.done()?;
6464

65+
attrs
66+
.operation("/collections/{collectionName}/documents", "patch")
67+
.generic_parameter("<B: Serialize>")
68+
.params_generic_parameter("<B>")
69+
.request_type("B")
70+
.done()?;
71+
72+
attrs
73+
.operation(
74+
"/collections/{collectionName}/documents/{documentId}",
75+
"patch",
76+
)
77+
.generic_parameter("<B: Serialize>")
78+
.params_generic_parameter("<B>")
79+
.request_type("B")
80+
.done()?;
81+
6582
Ok(())
6683
}

xtask/src/main.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ struct Cli {
3232
#[derive(ValueEnum, Clone, Debug)]
3333
#[clap(rename_all = "kebab-case")] // Allows us to type `code-gen` instead of `CodeGen`
3434
enum Task {
35-
/// Fetches the latest OpenAPI spec from [the Typesense repository](https://github.com/typesense/typesense-api-spec/blob/master/openapi.yml).
36-
Fetch,
3735
/// Generates client code from the spec file using the Docker container.
3836
CodeGen,
37+
/// Fetches the latest OpenAPI spec from [the Typesense repository](https://github.com/typesense/typesense-api-spec/blob/master/openapi.yml).
38+
Fetch,
39+
/// Preprocesses fetched OpenAPI spec file into a new one
40+
Preprocess,
3941
}
4042

4143
#[cfg(target_family = "wasm")]
@@ -48,16 +50,17 @@ fn main() -> Result<()> {
4850
for task in cli.tasks {
4951
println!("▶️ Running task: {:?}", task);
5052
match task {
51-
Task::Fetch => task_fetch_api_spec()?,
5253
Task::CodeGen => task_codegen()?,
54+
Task::Fetch => task_fetch_api_spec()?,
55+
Task::Preprocess => preprocess_openapi_file(INPUT_SPEC_FILE, OUTPUT_PREPROCESSED_FILE)
56+
.expect("Preprocess failed, aborting!"),
5357
}
5458
}
5559
Ok(())
5660
}
5761

58-
#[cfg(not(target_family = "wasm"))]
5962
fn task_fetch_api_spec() -> Result<()> {
60-
println!("▶️ Running codegen task...");
63+
println!("▶️ Running fetch task...");
6164

6265
println!(" - Downloading spec from {}", SPEC_URL);
6366
let response =
@@ -80,10 +83,6 @@ fn task_fetch_api_spec() -> Result<()> {
8083
/// Task to generate client code from the OpenAPI spec using a Docker container.
8184
fn task_codegen() -> Result<()> {
8285
println!("▶️ Running codegen task via Docker...");
83-
84-
println!("Preprocessing the Open API spec file...");
85-
preprocess_openapi_file(INPUT_SPEC_FILE, OUTPUT_PREPROCESSED_FILE)
86-
.expect("Preprocess failed, aborting!");
8786
// Get the absolute path to the project's root directory.
8887
// std::env::current_dir() gives us the directory from which `cargo xtask` was run.
8988
let project_root = env::current_dir().context("Failed to get current directory")?;

xtask/src/preprocess_openapi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub fn preprocess_openapi_file(
88
input_path: &str,
99
output_path: &str,
1010
) -> Result<(), Box<dyn std::error::Error>> {
11+
println!("Preprocessing the Open API spec file...");
1112
// --- Step 1: Read the OpenAPI spec from the input file ---
1213
println!("Reading OpenAPI spec from {}...", input_path);
1314
let input_content = fs::read_to_string(input_path)

xtask/src/vendor_attributes.rs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@ use serde_yaml::{Mapping, Value};
33
/// Where to apply a vendor (x-*) attribute.
44
pub enum VendorLocation<'a> {
55
Schema(&'a str),
6-
SchemaField { schema: &'a str, field: &'a str },
7-
Operation { path: &'a str, method: &'a str },
6+
SchemaField {
7+
schema: &'a str,
8+
field: &'a str,
9+
},
10+
Operation {
11+
path: &'a str,
12+
method: &'a str,
13+
},
14+
OperationField {
15+
path: &'a str,
16+
method: &'a str,
17+
field: &'a str,
18+
},
819
}
920

1021
/// Main helper struct that holds a mutable borrow of the OpenAPI root mapping.
@@ -86,6 +97,19 @@ impl<'a> VendorAttributes<'a> {
8697
Self::insert_into_map(op_map, attr, val);
8798
Ok(self)
8899
}
100+
VendorLocation::OperationField {
101+
path,
102+
method,
103+
field,
104+
} => {
105+
let field_map =
106+
self.get_map_mut(&["paths", path, method, field])
107+
.map_err(|_| {
108+
format!("operation field not found: {} {} {}", method, path, field)
109+
})?;
110+
Self::insert_into_map(field_map, attr, val);
111+
Ok(self)
112+
}
89113
}
90114
}
91115

@@ -166,13 +190,43 @@ impl<'a, 'b> OperationContext<'a, 'b> {
166190
}
167191
}
168192

193+
fn try_set_field(&mut self, field: &str, attr: &str, val: Value) {
194+
if self.error.is_some() {
195+
return;
196+
}
197+
if let Err(e) = self.vendor.set_attr(
198+
VendorLocation::OperationField {
199+
path: self.path,
200+
method: self.method,
201+
field,
202+
},
203+
attr,
204+
val,
205+
) {
206+
self.error = Some(e);
207+
}
208+
}
209+
169210
pub fn generic_parameter(mut self, generic: &str) -> Self {
170211
self.try_set("x-rust-generic-parameter", Value::String(generic.into()));
171212
self
172213
}
173214

174-
pub fn return_type(mut self, rust_type: &str) -> Self {
175-
self.try_set("x-rust-return-type", Value::String(rust_type.into()));
215+
pub fn params_generic_parameter(mut self, generic: &str) -> Self {
216+
self.try_set(
217+
"x-rust-params-generic-parameter",
218+
Value::String(generic.into()),
219+
);
220+
self
221+
}
222+
223+
pub fn return_type(mut self, typ: &str) -> Self {
224+
self.try_set("x-rust-return-type", Value::String(typ.into()));
225+
self
226+
}
227+
228+
pub fn request_type(mut self, typ: &str) -> Self {
229+
self.try_set_field("requestBody", "x-rust-type", Value::String(typ.into()));
176230
self
177231
}
178232

0 commit comments

Comments
 (0)