This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 649fa4845f feat(bindings/nodejs): generate ListOptions from core
(#9333)
649fa4845f is described below
commit 649fa4845fbee269aba736a74e90953c58cdbafa
Author: Chitral Verma <[email protected]>
AuthorDate: Sat Nov 28 20:59:03 2026 +0530
feat(bindings/nodejs): generate ListOptions from core (#9333)
* feat(bindings/nodejs): generate ListOptions from core
Generate the napi ListOptions object and its conversion to core from
core/core/src/types/options.rs through the shared core options parser.
The JSDoc now comes from the core doc comments. The JS fields, types,
optionality, and list method signatures stay as they were. The
conversion lists every core field, so a core field that JS does not
expose, such as glob, is set to its default explicitly.
Signed-off-by: Chitral Verma <[email protected]>
* fix(bindings/nodejs): address ListOptions review feedback
- Render code spans that name an exposed ListOptions field or a core
capability with their JS names, e.g. `startAfter` and
`Capability.listWithStartAfter`.
- Pin the core kind of each exposed field, so a kind change fails
generation instead of changing the JS type.
- Create bindings/nodejs/src/options/ before writing list.rs.
- Make the surface test compare the exact member set and require every
list method to accept each sample value.
Signed-off-by: Chitral Verma <[email protected]>
---------
Signed-off-by: Chitral Verma <[email protected]>
---
bindings/nodejs/generated.d.ts | 50 +--
bindings/nodejs/src/options.rs | 89 ++----
bindings/nodejs/src/options/list.rs | 68 ++++
.../nodejs/tests/list-options-surface.test.mjs | 86 +++++
dev/src/generate/nodejs.rs | 345 ++++++++++++++++++++-
dev/src/generate/nodejs/list_options_rs.j2 | 46 +++
6 files changed, 594 insertions(+), 90 deletions(-)
diff --git a/bindings/nodejs/generated.d.ts b/bindings/nodejs/generated.d.ts
index d3877018e9..baea5db44b 100644
--- a/bindings/nodejs/generated.d.ts
+++ b/bindings/nodejs/generated.d.ts
@@ -1223,41 +1223,43 @@ export declare const enum EntryMode {
Unknown = 2
}
+/**
+ * Options for list operations.
+ *
+ * # Groups
+ * - Traversal: `recursive`.
+ * - Pagination: `limit`, `startAfter`.
+ * - Versioning: `versions`, `deleted`.
+ *
+ * The operation fails with `crate::ErrorKind::Unsupported` when an option
+ * requires a capability that the operator lacks.
+ */
export interface ListOptions {
- /** * The limit passed to underlying service to specify the max results
- * that could return per-request.
- *
- * Users could use this to control the memory usage of list operation.
- */
+ /** Maximum results per request (backend hint) to control memory and
throttling. */
limit?: number
- /** * The start_after passed to underlying service to specify the specified
key
- * to start listing from.
+ /**
+ * Start listing after this key, excluding the key itself.
+ *
+ * Setting this requires `Capability.listWithStartAfter`.
*/
startAfter?: string
- /** * The recursive is used to control whether the list operation is
recursive.
- *
- * - If `false`, list operation will only list the entries under the given
path.
- * - If `true`, list operation will list all entries that starts with given
path.
+ /**
+ * Whether to list recursively under the prefix; default `false`.
*
- * Default to `false`.
+ * Setting this option to `true` requires the operator's effective
+ * `Capability.listWithRecursive` capability.
*/
recursive?: boolean
- /** * The versions is used to control whether the object versions should be
returned.
- *
- * - If `false`, list operation will not return with object versions
- * - If `true`, list operation will return with object versions if object
versioning is supported
- * by the underlying service
+ /**
+ * Include object versions; default `false`.
*
- * Default to `false`
+ * Requires `Capability.listWithVersions`.
*/
versions?: boolean
- /** * The deleted is used to control whether the deleted objects should be
returned.
- *
- * - If `false`, list operation will not return with deleted objects
- * - If `true`, list operation will return with deleted objects if object
versioning is supported
- * by the underlying service
+ /**
+ * Include delete markers; default `false`.
*
- * Default to `false`
+ * Requires `Capability.listWithDeleted`.
*/
deleted?: boolean
}
diff --git a/bindings/nodejs/src/options.rs b/bindings/nodejs/src/options.rs
index 18d8844ba5..72c34044cb 100644
--- a/bindings/nodejs/src/options.rs
+++ b/bindings/nodejs/src/options.rs
@@ -23,6 +23,9 @@ use std::collections::HashMap;
use crate::validate::parse_count;
+mod list;
+pub use list::ListOptions;
+
/// Convert a JavaScript `BigInt` into a `u64` offset or size.
///
/// `BigInt::get_u64` only reports the lowest 64-bit word and returns the sign
@@ -408,69 +411,6 @@ impl TryFrom<ReaderOptions> for
opendal::options::ReaderOptions {
}
}
-#[napi(object)]
-#[derive(Debug, Default)]
-pub struct ListOptions {
- /**
- * The limit passed to underlying service to specify the max results
- * that could return per-request.
- *
- * Users could use this to control the memory usage of list operation.
- */
- pub limit: Option<u32>,
-
- /**
- * The start_after passed to underlying service to specify the specified
key
- * to start listing from.
- */
- pub start_after: Option<String>,
-
- /**
- * The recursive is used to control whether the list operation is
recursive.
- *
- * - If `false`, list operation will only list the entries under the given
path.
- * - If `true`, list operation will list all entries that starts with
given path.
- *
- * Default to `false`.
- */
- pub recursive: Option<bool>,
-
- /**
- * The versions is used to control whether the object versions should be
returned.
- *
- * - If `false`, list operation will not return with object versions
- * - If `true`, list operation will return with object versions if object
versioning is supported
- * by the underlying service
- *
- * Default to `false`
- */
- pub versions: Option<bool>,
-
- /**
- * The deleted is used to control whether the deleted objects should be
returned.
- *
- * - If `false`, list operation will not return with deleted objects
- * - If `true`, list operation will return with deleted objects if object
versioning is supported
- * by the underlying service
- *
- * Default to `false`
- */
- pub deleted: Option<bool>,
-}
-
-impl From<ListOptions> for opendal::options::ListOptions {
- fn from(value: ListOptions) -> Self {
- Self {
- limit: value.limit.map(|limit| limit as usize),
- start_after: value.start_after,
- recursive: value.recursive.unwrap_or_default(),
- versions: value.versions.unwrap_or_default(),
- deleted: value.deleted.unwrap_or_default(),
- ..Default::default()
- }
- }
-}
-
#[napi(object)]
#[derive(Default, Debug)]
pub struct WriteOptions {
@@ -600,3 +540,26 @@ impl From<DeleteOptions> for
opendal::options::DeleteOptions {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::ListOptions;
+
+ #[test]
+ fn test_list_options_pass_explicit_zero_limit() {
+ let options = opendal::options::ListOptions::from(ListOptions {
+ limit: Some(0),
+ ..Default::default()
+ });
+ assert_eq!(options.limit, Some(0));
+ }
+
+ #[test]
+ fn test_list_options_default_omitted_fields() {
+ let options =
opendal::options::ListOptions::from(ListOptions::default());
+ assert_eq!(options.limit, None);
+ assert!(!options.recursive);
+ assert!(!options.versions);
+ assert!(!options.deleted);
+ }
+}
diff --git a/bindings/nodejs/src/options/list.rs
b/bindings/nodejs/src/options/list.rs
new file mode 100644
index 0000000000..19680b5615
--- /dev/null
+++ b/bindings/nodejs/src/options/list.rs
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// DO NOT EDIT IT MANUALLY. This file is generated by
opendal/dev/src/generate/nodejs.rs.
+
+/// Options for list operations.
+///
+/// # Groups
+/// - Traversal: `recursive`.
+/// - Pagination: `limit`, `startAfter`.
+/// - Versioning: `versions`, `deleted`.
+///
+/// The operation fails with `crate::ErrorKind::Unsupported` when an option
+/// requires a capability that the operator lacks.
+#[napi(object)]
+#[derive(Debug, Default)]
+pub struct ListOptions {
+ /// Maximum results per request (backend hint) to control memory and
throttling.
+ pub limit: Option<u32>,
+
+ /// Start listing after this key, excluding the key itself.
+ ///
+ /// Setting this requires `Capability.listWithStartAfter`.
+ pub start_after: Option<String>,
+
+ /// Whether to list recursively under the prefix; default `false`.
+ ///
+ /// Setting this option to `true` requires the operator's effective
+ /// `Capability.listWithRecursive` capability.
+ pub recursive: Option<bool>,
+
+ /// Include object versions; default `false`.
+ ///
+ /// Requires `Capability.listWithVersions`.
+ pub versions: Option<bool>,
+
+ /// Include delete markers; default `false`.
+ ///
+ /// Requires `Capability.listWithDeleted`.
+ pub deleted: Option<bool>,
+}
+
+impl From<ListOptions> for opendal::options::ListOptions {
+ fn from(value: ListOptions) -> Self {
+ Self {
+ limit: value.limit.map(|v| v as usize),
+ start_after: value.start_after,
+ recursive: value.recursive.unwrap_or_default(),
+ versions: value.versions.unwrap_or_default(),
+ deleted: value.deleted.unwrap_or_default(),
+ glob: Default::default(),
+ }
+ }
+}
diff --git a/bindings/nodejs/tests/list-options-surface.test.mjs
b/bindings/nodejs/tests/list-options-surface.test.mjs
new file mode 100644
index 0000000000..1aaa1681ec
--- /dev/null
+++ b/bindings/nodejs/tests/list-options-surface.test.mjs
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { readFileSync } from 'node:fs'
+import path from 'node:path'
+
+import { assert, describe, test } from 'vitest'
+
+import { Operator } from '../index.mjs'
+
+// `ListOptions` and the list methods as `generated.d.ts` declared them before
+// `ListOptions` was generated from core. Callers pass these options as object
+// literals or implement the interface structurally, so each member must stay
+// with its type and stay optional, and a new member must be added here on
+// purpose.
+const FROZEN_MEMBERS = [
+ 'limit?: number',
+ 'startAfter?: string',
+ 'recursive?: boolean',
+ 'versions?: boolean',
+ 'deleted?: boolean',
+]
+
+const FROZEN_SIGNATURES = [
+ 'list(path: string, options?: ListOptions | undefined | null):
Promise<Array<Entry>>',
+ 'listSync(path: string, options?: ListOptions | undefined | null):
Array<Entry>',
+ 'lister(path: string, options?: ListOptions | undefined | null):
Promise<Lister>',
+ 'listerSync(path: string, options?: ListOptions | undefined | null):
BlockingLister',
+]
+
+// Values of each declared type that the memory service accepts.
+const SAMPLES = { number: 0, string: 'a', boolean: false }
+
+function declared(kind, name) {
+ const dts = readFileSync(path.resolve(__dirname, '../generated.d.ts'),
'utf8')
+ const body = dts.match(new RegExp(`^export ${kind} ${name}
\\{\\n([\\s\\S]*?)^\\}`, 'm'))
+ assert.ok(body, `generated.d.ts declares ${kind} ${name}`)
+ return body[1]
+ .split('\n')
+ .filter((line) => /^ {2}\w/.test(line))
+ .map((line) => line.trim())
+}
+
+describe('ListOptions keeps its historical surface', () => {
+ const op = new Operator('memory')
+ const members = declared('interface', 'ListOptions')
+
+ test('declares exactly the frozen members', () => {
+ assert.sameMembers(members, FROZEN_MEMBERS)
+ })
+
+ for (const signature of FROZEN_SIGNATURES) {
+ test(signature, () => {
+ assert.include(declared('declare class', 'Operator'), signature)
+ })
+ }
+
+ for (const member of FROZEN_MEMBERS) {
+ const [, field, type] = member.match(/^(\w+)\?: (.+)$/)
+
+ test(member, async () => {
+ // Each list method must accept a value of the declared type.
+ const options = { [field]: SAMPLES[type] }
+ await op.list('/', options)
+ op.listSync('/', options)
+ await op.lister('/', options)
+ op.listerSync('/', options)
+ })
+ }
+})
diff --git a/dev/src/generate/nodejs.rs b/dev/src/generate/nodejs.rs
index 4282049a1d..4f5e97c1cf 100644
--- a/dev/src/generate/nodejs.rs
+++ b/dev/src/generate/nodejs.rs
@@ -17,9 +17,10 @@
//! Generator for the Node.js binding.
-use super::capability;
+use super::capability::{self, CapabilityField};
+use super::core_options::{self, OptionField, OptionKind, OptionsStruct};
use super::metadata::{self, MetadataKind};
-use anyhow::Result;
+use anyhow::{Result, bail};
use minijinja::value::Serde;
use minijinja::{Environment, context};
use serde::Serialize;
@@ -28,7 +29,8 @@ use std::path::Path;
pub fn generate(workspace_dir: &Path) -> Result<()> {
generate_capability(workspace_dir)?;
- generate_metadata(workspace_dir)
+ generate_metadata(workspace_dir)?;
+ generate_list_options(workspace_dir)
}
/// Render `bindings/nodejs/src/capability.rs` from
@@ -169,6 +171,194 @@ fn metadata_field(f: metadata::MetadataField) ->
MetadataField {
}
}
+/// The core `ListOptions` fields that JS exposes, in declaration order, with
+/// the core kind that their JS type was chosen for.
+///
+/// A core field missing here is not exposed in JS, and the conversion sets it
+/// to its default. Generation fails when core changes the kind of an exposed
+/// field, so the JS type never changes silently.
+const LIST_OPTIONS_FIELDS: &[(&str, OptionKind)] = &[
+ ("limit", OptionKind::OptionUsize),
+ ("start_after", OptionKind::OptionString),
+ ("recursive", OptionKind::Bool),
+ ("versions", OptionKind::Bool),
+ ("deleted", OptionKind::Bool),
+];
+
+/// Render model for one field of the napi `ListOptions` object.
+#[derive(Serialize)]
+struct ListOptionsField {
+ /// snake_case name, as in core. napi exposes it in camelCase.
+ name: String,
+ /// Rust type of the napi field. napi-rs derives the TS type from it.
+ rs_type: &'static str,
+ /// The core doc comment, one entry per line.
+ doc: Vec<String>,
+}
+
+/// One field of the core `ListOptions` literal that the conversion builds.
+#[derive(Serialize)]
+struct ListOptionsConversion {
+ name: String,
+ /// Expression that reads the JS value from `value`.
+ expr: String,
+}
+
+/// Render `bindings/nodejs/src/options/list.rs` from the core `ListOptions`
+/// in `core/core/src/types/options.rs`.
+///
+/// `generated.d.ts` is derived from this file by `napi build`.
+fn generate_list_options(workspace_dir: &Path) -> Result<()> {
+ let source = workspace_dir.join("core/core/src/types/options.rs");
+ let capabilities =
capability::parse(&workspace_dir.join("core/core/src/types/capability.rs"))?;
+ let rendered = render_list_options(&core_options::parse(&source)?,
&capabilities)?;
+
+ let output = workspace_dir.join("bindings/nodejs/src/options/list.rs");
+ fs::create_dir_all(output.parent().expect("output has a parent"))?;
+ fs::write(output, format!("{rendered}\n"))?;
+ Ok(())
+}
+
+fn render_list_options(
+ parsed: &[OptionsStruct],
+ capabilities: &[CapabilityField],
+) -> Result<String> {
+ let Some(core) = parsed.iter().find(|options| options.name ==
"ListOptions") else {
+ bail!("core has no options struct `ListOptions`");
+ };
+
+ let mut fields = Vec::with_capacity(LIST_OPTIONS_FIELDS.len());
+ for &(name, kind) in LIST_OPTIONS_FIELDS {
+ let Some(field) = core.fields.iter().find(|field| field.name == name)
else {
+ bail!("core ListOptions has no field `{name}`");
+ };
+ if field.kind != kind {
+ bail!(
+ "core ListOptions::{name} is {:?}, but its JS type was chosen
for {kind:?}",
+ field.kind
+ );
+ }
+ fields.push(ListOptionsField {
+ name: field.name.clone(),
+ rs_type: list_option_type(field)?.0,
+ doc: js_doc(&field.doc, capabilities),
+ });
+ }
+
+ // List every core field instead of ending with `..Default::default()`, so
+ // a new core field fails to compile in the binding until this file is
+ // regenerated, and the regenerated conversion shows it as unexposed.
+ let mut conversions = Vec::with_capacity(core.fields.len());
+ for field in &core.fields {
+ let exposed = LIST_OPTIONS_FIELDS
+ .iter()
+ .any(|&(name, _)| name == field.name);
+ let expr = if exposed {
+ list_option_type(field)?.1
+ } else {
+ "Default::default()".to_string()
+ };
+ conversions.push(ListOptionsConversion {
+ name: field.name.clone(),
+ expr,
+ });
+ }
+
+ let mut env = Environment::new();
+ env.add_template("list_rs", include_str!("nodejs/list_options_rs.j2"))?;
+ Ok(env.get_template("list_rs")?.render(context! {
+ doc => js_doc(&core.doc, capabilities),
+ fields => Serde(&fields),
+ conversions => Serde(&conversions),
+ has_unmirrored_fields => core.has_unmirrored_fields,
+ })?)
+}
+
+/// The napi type of an exposed core `ListOptions` field and the expression
+/// that converts it back.
+///
+/// These keep the types the binding used before codegen: `limit` is a
+/// `number` read as `u32`, so an explicit `0` reaches core as `Some(0)`, and
+/// an omitted boolean is `false`.
+fn list_option_type(field: &OptionField) -> Result<(&'static str, String)> {
+ let name = &field.name;
+ Ok(match field.kind {
+ OptionKind::OptionUsize => ("Option<u32>",
format!("value.{name}.map(|v| v as usize)")),
+ OptionKind::OptionString => ("Option<String>",
format!("value.{name}")),
+ OptionKind::Bool => ("Option<bool>",
format!("value.{name}.unwrap_or_default()")),
+ kind => bail!("core ListOptions::{name} is {kind:?}, which has no JS
type yet"),
+ })
+}
+
+/// The core doc comment as it reads in JS. napi-rs copies it into
+/// `generated.d.ts`.
+///
+/// Rustdoc links become plain code spans, because JS documentation cannot
+/// resolve Rust paths:
``[`Capability::list_with_limit`](crate::Capability::list_with_limit)``
+/// and ``[`crate::layers::SimulateLayer`]`` keep only their code span.
+/// Code spans that name a JS property then use its JS name (see
+/// [`js_code_span`]); other code spans stay as written.
+fn js_doc(lines: &[String], capabilities: &[CapabilityField]) -> Vec<String> {
+ lines
+ .iter()
+ .map(|line| {
+ let mut unlinked = String::with_capacity(line.len());
+ let mut rest = line.as_str();
+ while let Some(start) = rest.find("[`") {
+ let Some(len) = rest[start + 1..].find("`]") else {
+ break;
+ };
+ unlinked.push_str(&rest[..start]);
+ unlinked.push_str(&rest[start + 1..start + len + 2]);
+ rest = &rest[start + len + 3..];
+ if let Some(target) = rest.strip_prefix('(')
+ && let Some(end) = target.find(')')
+ {
+ rest = &target[end + 1..];
+ }
+ }
+ unlinked.push_str(rest);
+
+ let mut out = String::with_capacity(unlinked.len());
+ let mut rest = unlinked.as_str();
+ while let Some(start) = rest.find('`') {
+ let Some(len) = rest[start + 1..].find('`') else {
+ break;
+ };
+ let code = &rest[start + 1..start + 1 + len];
+ out.push_str(&rest[..=start]);
+ out.push_str(&js_code_span(code,
capabilities).unwrap_or_else(|| code.to_string()));
+ out.push('`');
+ rest = &rest[start + len + 2..];
+ }
+ out.push_str(rest);
+ out
+ })
+ .collect()
+}
+
+/// The JS name of a code span that names a property of the Node.js surface,
+/// or `None` to keep the code span as written.
+///
+/// napi-rs exposes snake_case Rust names in lower camel case, so an exposed
+/// `ListOptions` field like `start_after` reads `startAfter`, and a core
+/// capability like `Capability::list_with_start_after` reads
+/// `Capability.listWithStartAfter`, the getter on the JS `Capability` that
+/// `op.capability()` returns.
+fn js_code_span(code: &str, capabilities: &[CapabilityField]) ->
Option<String> {
+ if LIST_OPTIONS_FIELDS.iter().any(|&(name, _)| name == code) {
+ return Some(heck::AsLowerCamelCase(code).to_string());
+ }
+ let name = code
+ .strip_prefix("crate::")
+ .unwrap_or(code)
+ .strip_prefix("Capability::")?;
+ capabilities
+ .iter()
+ .any(|f| f.name == name)
+ .then(|| format!("Capability.{}", heck::AsLowerCamelCase(name)))
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -240,4 +430,153 @@ mod tests {
// JS receives a copy of user metadata, not core's borrowed view.
assert!(!rendered.contains("borrowed"));
}
+
+ fn core_list_options() -> OptionsStruct {
+ let source =
crate::workspace_dir().join("core/core/src/types/options.rs");
+ core_options::parse(&source)
+ .unwrap()
+ .into_iter()
+ .find(|options| options.name == "ListOptions")
+ .unwrap()
+ }
+
+ fn core_capabilities() -> Vec<CapabilityField> {
+
capability::parse(&crate::workspace_dir().join("core/core/src/types/capability.rs"))
+ .unwrap()
+ }
+
+ /// The napi struct, which napi-rs turns into `generated.d.ts`.
+ fn napi_struct(rendered: &str) -> &str {
+ let start = rendered.find("#[napi(object)]").unwrap();
+ let end = rendered[start..].find("\n}\n").unwrap();
+ &rendered[start..start + end]
+ }
+
+ #[test]
+ fn test_list_options_new_core_field_is_not_exposed() {
+ let core = core_list_options();
+ let mut mutated = core.clone();
+ mutated.fields.insert(
+ 0,
+ core_options::OptionField {
+ name: "fake_option".to_string(),
+ kind: OptionKind::OptionString,
+ doc: vec!["A fake option.".to_string()],
+ },
+ );
+
+ let before = render_list_options(&[core],
&core_capabilities()).unwrap();
+ let after = render_list_options(&[mutated],
&core_capabilities()).unwrap();
+ assert_eq!(napi_struct(&before), napi_struct(&after));
+ assert!(!before.contains("fake_option"));
+ assert!(after.contains("fake_option: Default::default(),"));
+ }
+
+ #[test]
+ fn test_list_options_reject_unknown_shapes() {
+ let mut missing = core_list_options();
+ missing.fields.retain(|field| field.name != "limit");
+ let err = render_list_options(&[missing], &core_capabilities())
+ .unwrap_err()
+ .to_string();
+ assert!(err.contains("has no field `limit`"), "{err}");
+
+ let mut changed = core_list_options();
+ let limit = changed
+ .fields
+ .iter_mut()
+ .find(|f| f.name == "limit")
+ .unwrap();
+ limit.kind = OptionKind::OptionU64;
+ let err = render_list_options(&[changed], &core_capabilities())
+ .unwrap_err()
+ .to_string();
+ assert!(err.contains("ListOptions::limit is OptionU64"), "{err}");
+
+ // A kind change to another supported kind must fail too, instead of
+ // silently turning `startAfter` into a boolean.
+ let mut changed = core_list_options();
+ let start_after = changed
+ .fields
+ .iter_mut()
+ .find(|f| f.name == "start_after")
+ .unwrap();
+ start_after.kind = OptionKind::Bool;
+ let err = render_list_options(&[changed], &core_capabilities())
+ .unwrap_err()
+ .to_string();
+ assert!(err.contains("ListOptions::start_after is Bool"), "{err}");
+ }
+
+ #[test]
+ fn test_list_options_docs_come_from_core() {
+ let mut core = core_list_options();
+ core.doc = vec!["Struct doc from core.".to_string()];
+ let limit = core.fields.iter_mut().find(|f| f.name ==
"limit").unwrap();
+ limit.doc = vec![
+ "First line.".to_string(),
+ String::new(),
+ "Third line.".to_string(),
+ ];
+
+ let rendered = render_list_options(&[core],
&core_capabilities()).unwrap();
+ assert!(rendered.contains("/// Struct doc from
core.\n#[napi(object)]"));
+ assert!(rendered.contains(
+ " /// First line.\n ///\n /// Third line.\n pub limit:
Option<u32>,"
+ ));
+ }
+
+ #[test]
+ fn test_js_doc_strips_rustdoc_links() {
+ let lines = [
+ "Rely on
[`crate::layers::SimulateLayer`](crate::layers::SimulateLayer) and
[`Self::recursive`].",
+ "A [plain](https://opendal.apache.org) link and `code` stay.",
+ ]
+ .map(String::from);
+ assert_eq!(
+ js_doc(&lines, &core_capabilities()),
+ [
+ "Rely on `crate::layers::SimulateLayer` and
`Self::recursive`.",
+ "A [plain](https://opendal.apache.org) link and `code` stay.",
+ ]
+ );
+ }
+
+ #[test]
+ fn test_js_doc_uses_js_names() {
+ let lines = [
+ "- Pagination: `limit`, `start_after`.",
+ "Requires
[`Capability::list_with_limit`](crate::Capability::list_with_limit).",
+ "Requires [`crate::Capability::list_with_versions`] and
`Capability::list_with_deleted`.",
+ "Unknown `Capability::no_such_flag`, `start_after_key`, `glob`,
and prose start_after stay.",
+ ]
+ .map(String::from);
+ assert_eq!(
+ js_doc(&lines, &core_capabilities()),
+ [
+ "- Pagination: `limit`, `startAfter`.",
+ "Requires `Capability.listWithLimit`.",
+ "Requires `Capability.listWithVersions` and
`Capability.listWithDeleted`.",
+ "Unknown `Capability::no_such_flag`, `start_after_key`,
`glob`, and prose start_after stay.",
+ ]
+ );
+ }
+
+ #[test]
+ fn test_list_options_docs_use_js_names() {
+ let rendered = render_list_options(&[core_list_options()],
&core_capabilities()).unwrap();
+ // Core writes `start_after` as a plain code span in the struct doc and
+ // the capability as a rustdoc link in the field doc.
+ assert!(rendered.contains("/// - Pagination: `limit`,
`startAfter`.\n"));
+ assert!(rendered.contains(
+ " /// Setting this requires `Capability.listWithStartAfter`.\n
pub start_after: Option<String>,"
+ ));
+ for line in rendered
+ .lines()
+ .filter(|l| l.trim_start().starts_with("///"))
+ {
+ assert!(!line.contains("`start_after`"), "{line}");
+ assert!(!line.contains("Capability::"), "{line}");
+ }
+ }
}
diff --git a/dev/src/generate/nodejs/list_options_rs.j2
b/dev/src/generate/nodejs/list_options_rs.j2
new file mode 100644
index 0000000000..183069c0d6
--- /dev/null
+++ b/dev/src/generate/nodejs/list_options_rs.j2
@@ -0,0 +1,46 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// DO NOT EDIT IT MANUALLY. This file is generated by
opendal/dev/src/generate/nodejs.rs.
+{% for line in doc %}
+///{{ " " ~ line if line }}
+{%- endfor %}
+#[napi(object)]
+#[derive(Debug, Default)]
+pub struct ListOptions {
+{%- for f in fields %}
+{%- if not loop.first %}
+{% endif %}
+{%- for line in f.doc %}
+ ///{{ " " ~ line if line }}
+{%- endfor %}
+ pub {{ f.name }}: {{ f.rs_type }},
+{%- endfor %}
+}
+
+impl From<ListOptions> for opendal::options::ListOptions {
+ fn from(value: ListOptions) -> Self {
+ Self {
+{%- for c in conversions %}
+ {{ c.name }}: {{ c.expr }},
+{%- endfor %}
+{%- if has_unmirrored_fields %}
+ ..Default::default()
+{%- endif %}
+ }
+ }
+}