chitralverma opened a new pull request, #9379:
URL: https://github.com/apache/opendal/pull/9379

   # Which issue does this PR close?
   
   Part of #9323 (tracker #9320).
   
   # Rationale for this change
   
   #9335, #9350, and #9368 generated the C and Go `ListOptions`, `StatOptions`, 
and `DeleteOptions` from core. This PR generates the existing `ReadOptions` the 
same way, with the same table, templates, and tests. It exposes no new option: 
core's `limit`, `if_version_match`, `if_version_not_match`, and `checksum` keep 
their defaults in the conversion. The reader, write, and copy option structs 
stay hand-written.
   
   # What changes are included in this PR?
   
   - `dev/src/generate/c/options.rs`: the released-options table gains 
`ReadOptions` in its C member order. Four new representations record today's 
encodings, and each pins one core kind, so generation fails when an exposed 
field leaves core or changes its kind:
   
     | Representation | Fields | C members and setters | Go option, private 
field |
     |---|---|---|---|
     | `Range` (`BytesRange`) | `range` | `offset`, `has_length`, `length`; 
`_set_range(opts, offset, length)` and `_set_range_from(opts, offset)` | 
`ReadWithRange(offset, length uint64)` and `ReadWithRangeFrom(offset uint64)`; 
the last one passed wins |
     | `Usize` (`usize`) | `concurrent` | `usize` | `uint`; `0` is skipped, 
which is also C's default |
     | `FlaggedUsize` (`Option<usize>`) | `chunk`, `gap` | `has_<name>` and 
`usize`; the setter sets both | `uint`; `0` is skipped, so Go cannot set `0` |
     | `FlaggedU64` (`Option<u64>`) | `content_length_hint` | `has_<name>` and 
`u64`; the setter sets both | `*uint64`; any value set reaches C, including `0` 
|
   
     The six strings reuse `BorrowedString` (the setter stores the caller's 
pointer, Go skips `""` and keeps the bytes alive), and the two timestamps reuse 
`FlaggedMillis`, as for `StatOptions`.
   - Docs come from core under the #9350 and #9368 rules, plus:
     - `OptionsSpec::docs` replaces core's doc where it does not hold for C and 
Go. Core documents `range` in Rust range syntax, so `offset` and `length` get 
their own docs, which the range members, both setters, and both Go options 
render. Core documents `gap` as how `Reader::fetch` merges ranges, which C and 
Go do not expose; each C and Go read selects one range, so the doc says that 
`gap` has no effect. Generation fails if a range lacks either doc, or a doc 
names a field that the struct does not release.
     - `OptionsSpec::rewrites` replaces core text that names something C and Go 
lack, and generation fails when core stops containing it. The struct doc's "a 
zero limit or an empty range" becomes "an empty range, such as one that 
`opendal_read_options_set_range(opts, offset, 0)` sets" (Go: 
`ReadWithRange(offset, 0)`), since C and Go have no `limit` yet. The `chunk` 
doc's "an open-ended or nonempty suffix range" (#9375) becomes "an open-ended 
range, such as no range or one that `opendal_read_options_set_range_from()` 
sets" (Go: `ReadWithRangeFrom`), since they have no suffix range.
     - For every generated struct, as in #9375 for Node.js: core's exact "Refer 
to the performance guide for more details." paragraph is dropped, because that 
guide covers the Rust API only, and "`Some(0)` has the same behavior as `None`" 
reads "`0` has the same behavior as leaving it unset". Only `ReadOptions` has 
this text today.
     - For every generated struct, a C doc line wider than 97 columns is 
wrapped at 80. cbindgen lays out a prototype one parameter per line when a line 
of its doc comment would exceed 100 columns in `opendal.h`, and core's `chunk` 
doc has a 99-column line. No list, stat, or delete doc line is that wide, so 
their output is unchanged.
   - C: the generated file now holds `opendal_read_options`, `_new`, the 14 
setters, `_free`, and a crate-private `unsafe fn to_core(&self) -> 
opendal::Result<options::ReadOptions>`. It replaces the safe 
`TryFrom<&opendal_read_options>` impl, which read the raw string pointers. 
`opendal_operator_read_with_cancel` and 
`opendal_operator_presign_read_with_cancel` call it with a `// SAFETY:` 
comment, and the `# Safety` sections of `opendal_operator_read_with`, 
`opendal_operator_presign_read_with`, and their `_with_cancel` forms state the 
contract, as #9368 did for the other generated structs. `range` converts to 
`BytesRange::new(offset, has_length.then_some(length))`, which equals `main`'s 
conversion for every input, since `BytesRange::default()` is 
`BytesRange::new(0, None)`.
   - Go: `WithReadFn`, the 14 `ReadWith*` options, `readOptions`, 
`parseReadOptions`, `newOpendalReadOptions`, and the 16 read option FFI 
declarations move from `reader.go` to the generated `options.go`. They keep 
their signatures, private field names, nil handling, and call order. Two 
private types change: the timestamps are stored as `*time.Time` and converted 
with `UnixMilli()` when the C options are built, as for `StatOptions`, and the 
returned keep-alive is `[][]byte` instead of `readOptionsKeepAlive`, which 
`ReaderOptions` still uses. `TestReadWithOptions` compares the timestamps 
through `UnixMilli()` accordingly.
   - Tests:
     - `bindings/c/tests/read.cpp` (new): `static_assert`s pin the 20 member 
types, exactly 20 members, the 64-bit `sizeof` (160) and every offset, and the 
16 prototypes, all copied from `opendal.h` on `main`. At runtime, the setters 
store the caller's string pointers and set their `has_*` members, 
`_set_range_from` clears the length, and NULL options are a no-op. On a memory 
operator: bounded and open-ended ranges, an offset at or past the end with and 
without `chunk`, a length past the end, an empty range on a missing path (and 
its `Unsupported` with `if_match`), `Unsupported` for the version and each 
condition on an existing and a missing path, `ConfigInvalid` for an 
out-of-range time, `chunk`, `concurrent`, and `gap`, and a 
`content_length_hint` that a chunked open-ended read trusts.
     - `bindings/c/src/types.rs`: `test_read_options_to_core` checks the 
defaults, every setter through the conversion (including the four defaulted 
core fields), that a zero `chunk`, `gap`, and hint stay set, and the range 
forms.
     - `bindings/go/options_test.go`: compile-time assignments pin `WithReadFn` 
and the 14 option signatures from `main`, and a test requires exactly these 14 
exported `ReadWith*` functions. A memory operator reads through the live C 
library with each option, as the C test does. A stub test checks that each 
option reaches its C setter with its value, that times arrive in Unix 
milliseconds, that the last range option wins, that `""` and zero counts reach 
no setter while a zero hint does, and that the six strings are kept alive.
     - `dev`: `test_docs_come_from_core` now covers `ReadOptions`: full 
paragraphs of each C member, C setter, Go option, the C struct, and the Go 
function type, computed from core's docs with the rules above written 
independently in the test, with the replaced docs and rewrites as literals, and 
the range members, setters, and Go options checked against literals. New tests 
cover rewrites that no longer match core, misplaced replacement docs, the 
Rust-only rules, the C line width, read string ownership and `has_*` notes, the 
four defaulted fields, the conversion layout, and removed or retyped read 
fields.
   
   Each new test fails under a mutation (scratch edits on the committed tree):
   
   - `dev`: claiming that `gap` has an effect, dropping either rewrite, keeping 
the performance guide paragraph or `Some(0)`, dropping "depending on the 
service" from the `offset` doc, a `_set_range_from` note that keeps 
`has_length`, removing the C line wrap, naming a missing C function in a 
rewrite, skipping the kind check, exposing `limit`, ignoring `has_length` in 
the range conversion, laying the hint conversion out on one line, and 
documenting a zero hint as unset. With core `chunk` changed to `Option<u64>`, 
`range` removed, or the suffix-range phrase reworded, `generate -l c` exits 
with an error naming the field or the rewrite.
   - `tests/read.cpp` fails to compile when `concurrent` moves after 
`has_chunk` (offsets) or a member is appended (member count and `sizeof`), and 
fails at runtime when `_set_range_from` keeps `has_length` or the conversion 
drops `version`, `if_match`, `chunk`, or `content_length_hint`. 
`test_read_options_to_core` fails when the conversion drops `concurrent` or 
`gap`.
   - `options_test.go` fails when Go passes a zero chunk, skips a zero hint, 
drops a string's keep-alive, sends `ReadWithRangeFrom` to the range setter, 
never calls the gap, version, or `override_cache_control` setter, or exports 
`ReadWithLimit`.
   
   Doc claims, checked through the C library on builds of this branch:
   
   - On `memory` and `fs`, a missing file returns `OPENDAL_NOT_FOUND` without 
options, and `OPENDAL_UNSUPPORTED` with an option the service lacks (`if_match` 
on both, `if_modified_since` on `memory`, `version` on both), also for an empty 
range. With `if_modified_since`, which `fs` supports, `fs` returns 
`OPENDAL_NOT_FOUND` for a missing file, the data for an old time, and 
`OPENDAL_CONDITION_NOT_MATCH` for a future one.
   - An empty range (`_set_range(opts, offset, 0)`) returns an empty buffer for 
a missing path on both. An offset at or past the end returns an empty buffer; 
with `chunk`, an offset past the end returns `OPENDAL_RANGE_NOT_SATISFIED`. A 
length past the end fails with `OPENDAL_UNEXPECTED`, or 
`OPENDAL_RANGE_NOT_SATISFIED` when the offset is past the end too.
   - Against a logging HTTP endpoint configured as an S3 service, a plain read 
with the three overrides sends `GET 
/test/f?response-content-disposition=attachment&response-content-type=text/x-probe&response-cache-control=no-store`,
 and `opendal_operator_presign_read_with` puts the same three parameters into 
the URL. A read with `chunk` and no range or `_set_range_from` sends `HEAD` 
before reading, a bounded range or a set `content_length_hint` does not, and on 
`fs`, which lacks `stat_with_if_modified_since`, `chunk` with 
`if_modified_since` fails with `OPENDAL_UNSUPPORTED` at `stat` unless 
`content_length_hint` is set.
   - `gap` is read only by `Reader::fetch`, which neither C nor Go calls; `gap` 
values of 0, 1, and 1 MiB return the same bytes.
   
   # Are there any user-facing changes?
   
   Doc comments only. No API, ABI, or behavior change.
   
   - C and Zig: with comments removed, `opendal.h` has exactly the same lines 
as `main`, and the `opendal_read_options` definition is byte-identical. The 
only difference is order: the read option prototypes now come before the write 
option prototypes, and `opendal_read_options_free` after the setters, because 
cbindgen writes prototypes in source order and the read options moved from 
`types.rs` to the generated `options.rs`. `opendal_read_options` is 160 bytes 
with the same offsets on macOS arm64 and on Linux x86-64 and arm64. The library 
exports the same 201 `opendal_*` symbols as `main`.
   - A C program compiled with `-Wall -Wextra -Werror` against `main`'s header 
prints the same 298 lines on macOS arm64 with `main`'s library and with this 
branch's. It prints the size and offsets, calls every setter (including on NULL 
options), initializes stack structs by position and by designators, and reads 
on `memory` and `fs` with each option and with offset, length, and chunk grids, 
and presigns an S3 read.
   - A read option string that is not valid UTF-8 still panics; the message now 
names the field (`malformed version: …` instead of `malformed option`), as for 
`StatOptions` in #9350.
   - Go: `go doc -short` is identical to `main`, and `go doc -all` has the same 
declarations; only the docs of `WithReadFn` and the 14 `ReadWith*` options 
differ. `main`'s Go package passes its 85 tests against this branch's library, 
and this branch's Go package passes its 88 tests (`main`'s 85 plus 3 new) 
against `main`'s library.
   
   # Breaking changes
   
   None.
   
   # AI Usage Statement
   
   - **Harness:** Claude Code via T3 Code | **Model:** Claude Opus 5.5 | 
**Effort:** xhigh | **Role:** Implemented the generator entry, representations, 
doc rules, and tests. Ran the surface diffs, compatibility probes, mutation 
checks, and doc-claim checks on `memory`, `fs`, and an S3 logging endpoint, 
plus the C, Go, and `odev` CI steps on macOS arm64 and in Linux containers. 
Drafted this description. The human author reviews the change and is 
responsible for it.
   


-- 
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