suyanhanx commented on code in PR #9333:
URL: https://github.com/apache/opendal/pull/9333#discussion_r4725700725


##########
bindings/nodejs/tests/list-options-surface.test.mjs:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.
+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',
+]
+
+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('every member is optional', () => {
+    for (const member of members) {
+      assert.match(member, /^\w+\?: /)
+    }
+  })
+
+  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 () => {
+      assert.include(members, member)
+
+      // Each list method must accept the declared type instead of failing
+      // with a napi `*Expected` error.
+      const options = { [field]: SAMPLES[type] }
+      for (const list of [
+        () => op.list('/', options),
+        () => op.listSync('/', options),
+        () => op.lister('/', options),
+        () => op.listerSync('/', options),
+      ]) {
+        try {
+          await list()
+        } catch (err) {
+          assert.notMatch(String(err.code), /Expected$/, `${field}: 
${err.message}`)

Review Comment:
   The catch accepts any error whose code does not end in `Expected`, including 
a plain `TypeError` with no code. I replaced all four list methods with stubs 
that throw `TypeError` on every call and all 10 tests in this file still 
passed. Could the supported memory cases be required to succeed, with only the 
specific unsupported-option error accepted where the backend lacks that 
capability?



##########
bindings/nodejs/tests/list-options-surface.test.mjs:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.
+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',
+]
+
+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('every member is optional', () => {
+    for (const member of members) {
+      assert.match(member, /^\w+\?: /)
+    }
+  })
+
+  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 () => {
+      assert.include(members, member)

Review Comment:
   This inclusion check lets an unexpected optional field through. I added 
`glob?: string` to the generated declaration and the suite stayed green. Since 
exposing `glob` is explicitly deferred to step 2 of #9322, could this test 
compare the complete set of members against `FROZEN_MEMBERS`? An 
order-independent comparison would catch additions without making declaration 
order part of the API.



##########
dev/src/generate/nodejs.rs:
##########
@@ -169,6 +171,135 @@ fn metadata_field(f: metadata::MetadataField) -> 
MetadataField {
     }
 }
 
+/// The core `ListOptions` fields that JS exposes, in declaration order.
+///
+/// A core field missing here is not exposed in JS, and the conversion sets it
+/// to its default.
+const LIST_OPTIONS_FIELDS: &[&str] = &["limit", "start_after", "recursive", 
"versions", "deleted"];
+
+/// 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 rendered = render_list_options(&core_options::parse(&source)?)?;
+
+    let output = workspace_dir.join("bindings/nodejs/src/options/list.rs");
+    fs::write(output, format!("{rendered}\n"))?;

Review Comment:
   The new `src/options/` directory contains only generated output. Moving it 
aside and running `odev generate -l nodejs` fails with ENOENT. Creating the 
empty directory makes the same command succeed and recreate `list.rs`. Could 
the generator create the output directory before writing the file?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to