space-data-module-sdk 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "space-data-module-sdk",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Module SDK for building, validating, signing, and deploying WebAssembly modules on the Space Data Network.",
6
6
  "type": "module",
@@ -9,8 +9,9 @@
9
9
  // - "params" / "result": one aligned record of the operation's table here (TAB WIRE_FORMAT ALIGNED_BINARY, TYPE_REF
10
10
  // SCHEMA_NAME "HostOps.fbs", ROOT_TYPE the table, BYTE_LENGTH its size);
11
11
  // - a list port (e.g. "entries"): a run of aligned records of one table, back to back at a fixed stride;
12
- // - a raw port (e.g. "body", "records", "url", "sql", "path"): unbounded bytes, verbatim - bodies, keys, record
13
- // streams (size-prefixed frames concatenated), and text that may exceed an aligned string (UTF-8).
12
+ // - a raw port (e.g. "body", "records", "url", "sql", "path", "config"): unbounded bytes, verbatim - bodies, keys,
13
+ // record streams (size-prefixed frames concatenated), typed FlatBuffers (plugin.getConfig's ModuleConfig), and
14
+ // text that may exceed an aligned string (UTF-8).
14
15
  // - SQL values (raw "params" in, raw "cells" out) use FlatSQL's own tagged parameter encoding (flatsql capi
15
16
  // decodeParamsNoThrow), value after value with no count: u8 tag, u32 little-endian payload length, payload.
16
17
  // Tags: 0 null (length 0), 1 bool (1 byte, 0 / 1), 2 i64 (8 bytes LE), 3 f64 (8 bytes LE), 4 string (UTF-8),
@@ -41,16 +42,9 @@ table HasCapabilityParams { CAPABILITY: string (aligned_max_length: 64); }
41
42
  /// LENGTH 1..8192 (0 = 32); the bytes come back on raw "data".
42
43
  table RandomBytesParams { LENGTH: uint32; }
43
44
 
44
- enum configEntryKind : ubyte { String, Number, Bool }
45
- /// One configured value; nested tables and lists are flattened into dotted paths ("targets.0.provider_id",
46
- /// "widths.1"). plugin.getConfig answers with a run of these on list "entries".
47
- table ConfigEntry {
48
- KEY: string (aligned_max_length: 255);
49
- KIND: configEntryKind;
50
- STRING_VALUE: string (aligned_max_length: 255);
51
- NUMBER_VALUE: double;
52
- BOOL_VALUE: bool;
53
- }
45
+ // plugin.getConfig answers on raw "config" with one ModuleConfig FlatBuffer (schemas/runtime/ModuleConfig.fbs, file
46
+ // identifier MCFG, not size-prefixed): typed values, number and string lists, nested tables - one compact buffer, read
47
+ // in place (typedHostcall.hpp sdm_piv::get_config()).
54
48
 
55
49
  // ---- http (raw in: "url", "headers" (an HTTP header block, "name: value" lines), "body"; raw out: the same) --------
56
50
 
@@ -225,64 +225,6 @@ const _RandomBytesParams = class _RandomBytesParams {
225
225
  __publicField(_RandomBytesParams, "SIZE", 4);
226
226
  __publicField(_RandomBytesParams, "ALIGN", 4);
227
227
  let RandomBytesParams = _RandomBytesParams;
228
- const _ConfigEntry = class _ConfigEntry {
229
- constructor(buffer, offset = 0) {
230
- __publicField(this, "buffer", buffer);
231
- __publicField(this, "offset", offset);
232
- __publicField(this, "view");
233
- this.view = new DataView(buffer);
234
- }
235
- static fromPointer(buffer, offset = 0) {
236
- return new _ConfigEntry(buffer, offset);
237
- }
238
- hasKEY() {
239
- return __readPresence(this.view, this.offset, 0);
240
- }
241
- setHasKEY(value) {
242
- __writePresence(this.view, this.offset, 0, value);
243
- }
244
- kEY() {
245
- return __decodeString(this.view, this.offset + 1, 255);
246
- }
247
- setKEY(value) {
248
- __encodeString(this.view, this.offset + 1, 255, value);
249
- this.setHasKEY(true);
250
- }
251
- kIND() {
252
- return this.view.getUint8(this.offset + 257);
253
- }
254
- setKIND(value) {
255
- this.view.setUint8(this.offset + 257, value);
256
- }
257
- hasSTRING_VALUE() {
258
- return __readPresence(this.view, this.offset, 1);
259
- }
260
- setHasSTRING_VALUE(value) {
261
- __writePresence(this.view, this.offset, 1, value);
262
- }
263
- sTRING_VALUE() {
264
- return __decodeString(this.view, this.offset + 258, 255);
265
- }
266
- setSTRING_VALUE(value) {
267
- __encodeString(this.view, this.offset + 258, 255, value);
268
- this.setHasSTRING_VALUE(true);
269
- }
270
- nUMBER_VALUE() {
271
- return this.view.getFloat64(this.offset + 520, true);
272
- }
273
- setNUMBER_VALUE(value) {
274
- this.view.setFloat64(this.offset + 520, value, true);
275
- }
276
- bOOL_VALUE() {
277
- return this.view.getUint8(this.offset + 528) !== 0;
278
- }
279
- setBOOL_VALUE(value) {
280
- this.view.setUint8(this.offset + 528, value ? 1 : 0);
281
- }
282
- };
283
- __publicField(_ConfigEntry, "SIZE", 536);
284
- __publicField(_ConfigEntry, "ALIGN", 8);
285
- let ConfigEntry = _ConfigEntry;
286
228
  const _HttpParams = class _HttpParams {
287
229
  constructor(buffer, offset = 0) {
288
230
  __publicField(this, "buffer", buffer);
@@ -3844,7 +3786,6 @@ export {
3844
3786
  BodyRef,
3845
3787
  BoolValue,
3846
3788
  CacheKeyParams,
3847
- ConfigEntry,
3848
3789
  ContextParams,
3849
3790
  CountValue,
3850
3791
  CronField,
@@ -243,76 +243,6 @@ export class RandomBytesParams {
243
243
 
244
244
  }
245
245
 
246
- export class ConfigEntry {
247
- static readonly SIZE = 536;
248
- static readonly ALIGN = 8;
249
- static fromPointer(buffer: BufferLike, offset = 0): ConfigEntry { return new ConfigEntry(buffer, offset); }
250
-
251
- readonly view: DataView;
252
- constructor(public readonly buffer: BufferLike, public readonly offset = 0) {
253
- this.view = new DataView(buffer);
254
- }
255
-
256
- hasKEY(): boolean {
257
- return __readPresence(this.view, this.offset, 0);
258
- }
259
-
260
- setHasKEY(value: boolean) {
261
- __writePresence(this.view, this.offset, 0, value);
262
- }
263
-
264
- kEY(): string {
265
- return __decodeString(this.view, this.offset + 1, 255);
266
- }
267
-
268
- setKEY(value: string) {
269
- __encodeString(this.view, this.offset + 1, 255, value);
270
- this.setHasKEY(true);
271
- }
272
-
273
- kIND(): number {
274
- return this.view.getUint8(this.offset + 257);
275
- }
276
-
277
- setKIND(value: number) {
278
- this.view.setUint8(this.offset + 257, value);
279
- }
280
-
281
- hasSTRING_VALUE(): boolean {
282
- return __readPresence(this.view, this.offset, 1);
283
- }
284
-
285
- setHasSTRING_VALUE(value: boolean) {
286
- __writePresence(this.view, this.offset, 1, value);
287
- }
288
-
289
- sTRING_VALUE(): string {
290
- return __decodeString(this.view, this.offset + 258, 255);
291
- }
292
-
293
- setSTRING_VALUE(value: string) {
294
- __encodeString(this.view, this.offset + 258, 255, value);
295
- this.setHasSTRING_VALUE(true);
296
- }
297
-
298
- nUMBER_VALUE(): number {
299
- return this.view.getFloat64(this.offset + 520, true);
300
- }
301
-
302
- setNUMBER_VALUE(value: number) {
303
- this.view.setFloat64(this.offset + 520, value, true);
304
- }
305
-
306
- bOOL_VALUE(): boolean {
307
- return (this.view.getUint8(this.offset + 528) !== 0);
308
- }
309
-
310
- setBOOL_VALUE(value: boolean) {
311
- this.view.setUint8(this.offset + 528, (value ? 1 : 0));
312
- }
313
-
314
- }
315
-
316
246
  export class HttpParams {
317
247
  static readonly SIZE = 40;
318
248
  static readonly ALIGN = 8;
@@ -1,10 +1,8 @@
1
1
  const deliverMode = Object.freeze({ "Inline": 0, "Ref": 1 });
2
- const configEntryKind = Object.freeze({ "String": 0, "Number": 1, "Bool": 2 });
3
2
  const reconcileKind = Object.freeze({ "Duplicates": 0, "None": 1, "Current": 2 });
4
3
  const sandboxWant = Object.freeze({ "Stream": 0, "Rows": 1, "Auto": 2 });
5
4
  const cryptoAlgorithm = Object.freeze({ "Default": 0, "Ed25519": 1, "Secp256k1": 2, "Secp256k1Digest": 3, "X25519": 4, "Sha256": 5, "Sha512": 6, "Aes256Gcm": 7, "HkdfSha256": 8 });
6
5
  export {
7
- configEntryKind,
8
6
  cryptoAlgorithm,
9
7
  deliverMode,
10
8
  reconcileKind,
@@ -1,6 +1,5 @@
1
1
  // generated by scripts/generate-runtime-bindings.mjs from HostOps.fbs
2
2
  export const deliverMode = Object.freeze({"Inline":0,"Ref":1});
3
- export const configEntryKind = Object.freeze({"String":0,"Number":1,"Bool":2});
4
3
  export const reconcileKind = Object.freeze({"Duplicates":0,"None":1,"Current":2});
5
4
  export const sandboxWant = Object.freeze({"Stream":0,"Rows":1,"Auto":2});
6
5
  export const cryptoAlgorithm = Object.freeze({"Default":0,"Ed25519":1,"Secp256k1":2,"Secp256k1Digest":3,"X25519":4,"Sha256":5,"Sha512":6,"Aes256Gcm":7,"HkdfSha256":8});
@@ -64,12 +64,6 @@ enum deliverMode : uint8_t {
64
64
  deliverMode_Ref = 1,
65
65
  };
66
66
 
67
- enum configEntryKind : uint8_t {
68
- configEntryKind_String = 0,
69
- configEntryKind_Number = 1,
70
- configEntryKind_Bool = 2,
71
- };
72
-
73
67
  enum reconcileKind : uint8_t {
74
68
  reconcileKind_Duplicates = 0,
75
69
  reconcileKind_None = 1,
@@ -340,56 +334,6 @@ namespace sdn {
340
334
  namespace runtime {
341
335
  namespace Aligned {
342
336
 
343
- struct ConfigEntry {
344
- uint8_t __presence[1];
345
- ::flatbuffers::aligned_runtime::AlignedString<255> KEY;
346
- configEntryKind KIND;
347
- ::flatbuffers::aligned_runtime::AlignedString<255> STRING_VALUE;
348
- double NUMBER_VALUE;
349
- bool BOOL_VALUE;
350
-
351
- bool has_KEY() const {
352
- return (__presence[0] & 1) != 0;
353
- }
354
- void set_has_KEY(bool value) {
355
- if (value) __presence[0] |= 1;
356
- else __presence[0] &= ~1;
357
- }
358
- bool has_STRING_VALUE() const {
359
- return (__presence[0] & 2) != 0;
360
- }
361
- void set_has_STRING_VALUE(bool value) {
362
- if (value) __presence[0] |= 2;
363
- else __presence[0] &= ~2;
364
- }
365
-
366
- static ConfigEntry* fromBytes(void* data) {
367
- return reinterpret_cast<ConfigEntry*>(data);
368
- }
369
- static const ConfigEntry* fromBytes(const void* data) {
370
- return reinterpret_cast<const ConfigEntry*>(data);
371
- }
372
- void copyTo(void* dest) const { std::memcpy(dest, this, 536); }
373
- void copyFrom(const ConfigEntry& src) { std::memcpy(this, &src, 536); }
374
- };
375
- static_assert(sizeof(ConfigEntry) == 536, "ConfigEntry size mismatch");
376
- static_assert(alignof(ConfigEntry) == 8, "ConfigEntry alignment mismatch");
377
- static_assert(offsetof(ConfigEntry, KEY) == 1, "ConfigEntry.KEY offset mismatch");
378
- static_assert(offsetof(ConfigEntry, KIND) == 257, "ConfigEntry.KIND offset mismatch");
379
- static_assert(offsetof(ConfigEntry, STRING_VALUE) == 258, "ConfigEntry.STRING_VALUE offset mismatch");
380
- static_assert(offsetof(ConfigEntry, NUMBER_VALUE) == 520, "ConfigEntry.NUMBER_VALUE offset mismatch");
381
- static_assert(offsetof(ConfigEntry, BOOL_VALUE) == 528, "ConfigEntry.BOOL_VALUE offset mismatch");
382
- constexpr size_t ConfigEntry_SIZE = 536;
383
- constexpr size_t ConfigEntry_ALIGN = 8;
384
-
385
- } // namespace Aligned
386
- } // namespace runtime
387
- } // namespace sdn
388
-
389
- namespace sdn {
390
- namespace runtime {
391
- namespace Aligned {
392
-
393
337
  struct HttpParams {
394
338
  uint8_t __presence[1];
395
339
  ::flatbuffers::aligned_runtime::AlignedString<16> METHOD;
@@ -16,6 +16,12 @@
16
16
  //
17
17
  // auto get = sdm_piv::call("http.request", "HttpParams", http, {sdm_piv::raw("url", url)});
18
18
  // std::string_view body = get.raw("body");
19
+ //
20
+ // const sdm_piv::Config config = sdm_piv::get_config(); // plugin.getConfig: one ModuleConfig (MCFG), read in place
21
+ // if (!config.ok()) { config.error_message(); }
22
+ // double lead = config.number("nwp_max_lead", 72);
23
+ // std::string_view provider = config.string("targets.0.provider_id");
24
+ // sdm_piv::Span<double> index = config.numbers("index");
19
25
  #ifndef SDM_PIV_HOSTCALL_HPP
20
26
  #define SDM_PIV_HOSTCALL_HPP
21
27
 
@@ -29,6 +35,7 @@
29
35
 
30
36
  #include "sds/PIV/main_generated.h"
31
37
  #include "HostOps_aligned.h"
38
+ #include "ModuleConfig_generated.h"
32
39
 
33
40
  extern "C" {
34
41
  #if defined(__wasm__)
@@ -196,6 +203,247 @@ inline Response call(const char* op, const char* params_type, const Params& para
196
203
  /// Call an operation without a "params" record (clock.now, plugin.getConfig, filesystem.readFile with raw "path", ...).
197
204
  inline Response call(const char* op, std::initializer_list<Frame> frames = {}) { return call_frames(op, std::vector<Frame>(frames)); }
198
205
 
206
+ // ---- plugin.getConfig: the module's config as one ModuleConfig FlatBuffer (schemas/runtime/ModuleConfig.fbs, MCFG) --
207
+
208
+ /// A read-only run of T in place (C++17 has no std::span).
209
+ template <typename T>
210
+ class Span {
211
+ public:
212
+ Span() = default;
213
+ Span(const T* data, size_t size) : data_(data), size_(size) {}
214
+ const T* data() const { return data_; }
215
+ size_t size() const { return size_; }
216
+ bool empty() const { return size_ == 0; }
217
+ const T& operator[](size_t i) const { return data_[i]; }
218
+ const T* begin() const { return data_; }
219
+ const T* end() const { return data_ + size_; }
220
+
221
+ private:
222
+ const T* data_ = nullptr;
223
+ size_t size_ = 0;
224
+ };
225
+
226
+ using ConfigEntries = flatbuffers::Vector<flatbuffers::Offset<sdn::runtime::ConfigEntry>>;
227
+
228
+ /// A table of config entries (the ModuleConfig root, or one nested ConfigTable of a TableList), sorted by KEY.
229
+ ///
230
+ /// Every lookup takes a key or a dotted path into nested tables and lists, the way the old flattened entries were
231
+ /// named: "targets.0.provider_id" (table 0 of TableList "targets", then its "provider_id"), "targets.provider_id"
232
+ /// (a TableList holding one table - a nested object - needs no index), "widths.1" (item 1 of a NumberList or
233
+ /// StringList). A key that itself contains dots is found too: the longest key that matches is taken first.
234
+ /// A missing key, or a value of another kind, answers the default (or an empty list / table).
235
+ class ConfigTable {
236
+ public:
237
+ ConfigTable() = default;
238
+ explicit ConfigTable(const ConfigEntries* entries) : entries_(entries) {}
239
+
240
+ explicit operator bool() const { return entries_ != nullptr; }
241
+ /// The entries, in place (nullptr for an empty table).
242
+ const ConfigEntries* entries() const { return entries_; }
243
+ size_t size() const { return entries_ ? entries_->size() : 0; }
244
+
245
+ /// The entry a key or dotted path names (nullptr when there is none; a path ending in a list index names the
246
+ /// list's entry - use the typed lookups to read the item).
247
+ const sdn::runtime::ConfigEntry* entry(std::string_view key) const { return find(key).entry; }
248
+ bool has(std::string_view key) const { return find(key).entry != nullptr; }
249
+ /// The value's kind, or `fallback` when the key is missing.
250
+ sdn::runtime::configValueKind kind(std::string_view key, sdn::runtime::configValueKind fallback = sdn::runtime::configValueKind_String) const {
251
+ const Found f = find(key);
252
+ return f.entry ? f.entry->KIND() : fallback;
253
+ }
254
+
255
+ /// A String (or an item of a StringList: "names.2"); the view lives as long as the Config.
256
+ std::string_view string(std::string_view key, std::string_view def = {}) const {
257
+ const Found f = find(key);
258
+ if (!f.entry) return def;
259
+ if (f.index < 0 && f.entry->KIND() == sdn::runtime::configValueKind_String) return view(f.entry->STRING_VALUE());
260
+ if (f.index >= 0 && f.entry->KIND() == sdn::runtime::configValueKind_StringList && f.entry->STRINGS() &&
261
+ static_cast<size_t>(f.index) < f.entry->STRINGS()->size()) {
262
+ return view(f.entry->STRINGS()->Get(static_cast<flatbuffers::uoffset_t>(f.index)));
263
+ }
264
+ return def;
265
+ }
266
+ /// A Number (or an item of a NumberList: "widths.1").
267
+ double number(std::string_view key, double def = 0) const {
268
+ const Found f = find(key);
269
+ if (!f.entry) return def;
270
+ if (f.index < 0 && f.entry->KIND() == sdn::runtime::configValueKind_Number) return f.entry->NUMBER_VALUE();
271
+ if (f.index >= 0 && f.entry->KIND() == sdn::runtime::configValueKind_NumberList && f.entry->NUMBERS() &&
272
+ static_cast<size_t>(f.index) < f.entry->NUMBERS()->size()) {
273
+ return f.entry->NUMBERS()->Get(static_cast<flatbuffers::uoffset_t>(f.index));
274
+ }
275
+ return def;
276
+ }
277
+ bool boolean(std::string_view key, bool def = false) const {
278
+ const Found f = find(key);
279
+ return f.entry && f.index < 0 && f.entry->KIND() == sdn::runtime::configValueKind_Bool ? f.entry->BOOL_VALUE() : def;
280
+ }
281
+ /// A NumberList in place, no copy (empty when missing or another kind).
282
+ Span<double> numbers(std::string_view key) const {
283
+ const Found f = find(key);
284
+ if (!f.entry || f.index >= 0 || f.entry->KIND() != sdn::runtime::configValueKind_NumberList || !f.entry->NUMBERS()) return {};
285
+ return {f.entry->NUMBERS()->data(), f.entry->NUMBERS()->size()};
286
+ }
287
+ /// A StringList's items (views into the config; empty when missing or another kind).
288
+ std::vector<std::string_view> strings(std::string_view key) const {
289
+ std::vector<std::string_view> out;
290
+ const Found f = find(key);
291
+ if (!f.entry || f.index >= 0 || f.entry->KIND() != sdn::runtime::configValueKind_StringList || !f.entry->STRINGS()) return out;
292
+ out.reserve(f.entry->STRINGS()->size());
293
+ for (const auto* s : *f.entry->STRINGS()) out.push_back(view(s));
294
+ return out;
295
+ }
296
+ /// How many items a list holds: a NumberList's numbers, a StringList's strings or a TableList's tables (0 when
297
+ /// the key is missing or not a list). Iterate 0..count-1 instead of probing "key.N" until a lookup fails.
298
+ size_t count(std::string_view key) const {
299
+ const Found f = find(key);
300
+ if (!f.entry || f.index >= 0) return 0;
301
+ switch (f.entry->KIND()) {
302
+ case sdn::runtime::configValueKind_NumberList: return f.entry->NUMBERS() ? f.entry->NUMBERS()->size() : 0;
303
+ case sdn::runtime::configValueKind_StringList: return f.entry->STRINGS() ? f.entry->STRINGS()->size() : 0;
304
+ case sdn::runtime::configValueKind_TableList: return f.entry->TABLES() ? f.entry->TABLES()->size() : 0;
305
+ default: return 0;
306
+ }
307
+ }
308
+ /// How many tables a TableList holds (0 when missing or another kind).
309
+ size_t tables(std::string_view key) const {
310
+ const Found f = find(key);
311
+ return f.entry && f.index < 0 && f.entry->KIND() == sdn::runtime::configValueKind_TableList && f.entry->TABLES() ? f.entry->TABLES()->size() : 0;
312
+ }
313
+ /// Table `i` of a TableList ("targets", 2), or the table a path names ("targets.2"; "settings" for a nested object).
314
+ ConfigTable table(std::string_view key, size_t i) const {
315
+ const Found f = find(key);
316
+ if (!f.entry || f.index >= 0 || f.entry->KIND() != sdn::runtime::configValueKind_TableList || !f.entry->TABLES() || i >= f.entry->TABLES()->size()) return {};
317
+ return ConfigTable(f.entry->TABLES()->Get(static_cast<flatbuffers::uoffset_t>(i))->ENTRIES());
318
+ }
319
+ ConfigTable table(std::string_view key) const {
320
+ const Found f = find(key);
321
+ if (!f.entry || f.entry->KIND() != sdn::runtime::configValueKind_TableList || !f.entry->TABLES()) return {};
322
+ const size_t i = f.index >= 0 ? static_cast<size_t>(f.index) : 0;
323
+ if (i >= f.entry->TABLES()->size() || (f.index < 0 && f.entry->TABLES()->size() != 1)) return {};
324
+ return ConfigTable(f.entry->TABLES()->Get(static_cast<flatbuffers::uoffset_t>(i))->ENTRIES());
325
+ }
326
+
327
+ private:
328
+ struct Found {
329
+ const sdn::runtime::ConfigEntry* entry = nullptr;
330
+ int64_t index = -1; // an item of a list, when the path ends in an index
331
+ };
332
+ static std::string_view view(const flatbuffers::String* s) { return s ? std::string_view(s->c_str(), s->size()) : std::string_view(); }
333
+ static bool index_of(std::string_view s, int64_t& out) {
334
+ if (s.empty() || s.size() > 18) return false;
335
+ int64_t v = 0;
336
+ for (char c : s) { if (c < '0' || c > '9') return false; v = v * 10 + (c - '0'); }
337
+ out = v;
338
+ return true;
339
+ }
340
+ /// The entry with exactly this key: a binary search (the entries are sorted by KEY), then a scan in case a writer
341
+ /// sorted by another collation.
342
+ static const sdn::runtime::ConfigEntry* lookup(const ConfigEntries* entries, std::string_view key) {
343
+ if (!entries) return nullptr;
344
+ size_t lo = 0, hi = entries->size();
345
+ while (lo < hi) {
346
+ const size_t mid = lo + (hi - lo) / 2;
347
+ const auto* e = entries->Get(static_cast<flatbuffers::uoffset_t>(mid));
348
+ const int c = view(e->KEY()).compare(key);
349
+ if (c == 0) return e;
350
+ if (c < 0) lo = mid + 1; else hi = mid;
351
+ }
352
+ for (const auto* e : *entries) if (view(e->KEY()) == key) return e;
353
+ return nullptr;
354
+ }
355
+ static Found resolve(const ConfigEntries* entries, std::string_view path, int depth) {
356
+ if (!entries || path.empty() || depth > 64) return {};
357
+ if (const auto* e = lookup(entries, path)) return {e, -1};
358
+ // the longest key before a dot first, so keys that contain dots win over nesting
359
+ for (size_t dot = path.rfind('.'); dot != std::string_view::npos && dot > 0; dot = path.rfind('.', dot - 1)) {
360
+ const auto* e = lookup(entries, path.substr(0, dot));
361
+ if (!e) continue;
362
+ std::string_view rest = path.substr(dot + 1);
363
+ const size_t next = rest.find('.');
364
+ const std::string_view head = rest.substr(0, next);
365
+ int64_t index = -1;
366
+ const bool indexed = index_of(head, index);
367
+ switch (e->KIND()) {
368
+ case sdn::runtime::configValueKind_NumberList:
369
+ case sdn::runtime::configValueKind_StringList: {
370
+ // an item past the end is missing, like any other missing key (modules find a list's end by probing)
371
+ const size_t n = e->KIND() == sdn::runtime::configValueKind_NumberList ? (e->NUMBERS() ? e->NUMBERS()->size() : 0)
372
+ : (e->STRINGS() ? e->STRINGS()->size() : 0);
373
+ if (indexed && next == std::string_view::npos && static_cast<uint64_t>(index) < n) return {e, index};
374
+ return {};
375
+ }
376
+ case sdn::runtime::configValueKind_TableList: {
377
+ const auto* tables = e->TABLES();
378
+ if (!tables) return {};
379
+ if (indexed) {
380
+ if (static_cast<uint64_t>(index) >= tables->size()) return {};
381
+ if (next == std::string_view::npos) return {e, index}; // "targets.0": the table itself
382
+ return resolve(tables->Get(static_cast<flatbuffers::uoffset_t>(index))->ENTRIES(), rest.substr(next + 1), depth + 1);
383
+ }
384
+ if (tables->size() != 1) return {};
385
+ return resolve(tables->Get(0)->ENTRIES(), rest, depth + 1); // a nested object: one table, no index
386
+ }
387
+ default:
388
+ return {};
389
+ }
390
+ }
391
+ return {};
392
+ }
393
+ Found find(std::string_view key) const { return resolve(entries_, key, 0); }
394
+
395
+ const ConfigEntries* entries_ = nullptr;
396
+ };
397
+
398
+ /// The module's config, as plugin.getConfig answers it: the host's $PIV response kept whole and its raw "config"
399
+ /// frame - one ModuleConfig FlatBuffer (MCFG) - verified and read in place. Lookups are ConfigTable's. Move-only
400
+ /// (the views point into the kept bytes).
401
+ class Config : public ConfigTable {
402
+ public:
403
+ explicit Config(Response response) : response_(std::move(response)) {
404
+ if (!response_.ok()) { error_ = response_.error_message(); code_ = response_.error_code(); return; }
405
+ std::string_view frame = response_.raw("config");
406
+ if (frame.empty()) { fail("plugin.getConfig answered without a \"config\" frame"); return; }
407
+ const uint8_t* data = reinterpret_cast<const uint8_t*>(frame.data());
408
+ if (reinterpret_cast<uintptr_t>(data) % 8) { // the invoke arena aligns frames to 8; copy only if a host did not
409
+ aligned_.resize((frame.size() + 7) / 8);
410
+ std::memcpy(aligned_.data(), frame.data(), frame.size());
411
+ data = reinterpret_cast<const uint8_t*>(aligned_.data());
412
+ }
413
+ flatbuffers::Verifier::Options options;
414
+ options.max_tables = 100000000; // TableList-heavy configs: the buffer size bounds the work anyway
415
+ flatbuffers::Verifier verifier(data, frame.size(), options);
416
+ if (!sdn::runtime::VerifyModuleConfigBuffer(verifier)) { fail("plugin.getConfig: \"config\" is not a ModuleConfig (MCFG) FlatBuffer"); return; }
417
+ root_ = sdn::runtime::GetModuleConfig(data);
418
+ size_ = frame.size();
419
+ static_cast<ConfigTable&>(*this) = ConfigTable(root_->ENTRIES());
420
+ }
421
+ Config(Config&&) = default;
422
+ Config& operator=(Config&&) = default;
423
+ Config(const Config&) = delete;
424
+ Config& operator=(const Config&) = delete;
425
+
426
+ bool ok() const { return root_ != nullptr; }
427
+ std::string error_code() const { return code_; }
428
+ std::string error_message() const { return error_; }
429
+ /// The verified ModuleConfig root (nullptr when the call failed).
430
+ const sdn::runtime::ModuleConfig* root() const { return root_; }
431
+ /// The ModuleConfig bytes' length.
432
+ size_t byte_size() const { return size_; }
433
+
434
+ private:
435
+ void fail(const char* message) { error_ = message; code_ = "failed"; }
436
+ Response response_;
437
+ std::vector<uint64_t> aligned_;
438
+ const sdn::runtime::ModuleConfig* root_ = nullptr;
439
+ size_t size_ = 0;
440
+ std::string error_;
441
+ std::string code_;
442
+ };
443
+
444
+ /// plugin.getConfig: the module's config (no capability needed).
445
+ inline Config get_config() { return Config(call("plugin.getConfig")); }
446
+
199
447
  /// SQL values (raw "params" of storage.flatsql_query_stream / query_sandboxed / flatsql_cache_key, raw "cells" of a
200
448
  /// query_sandboxed rows answer) in FlatSQL's tagged parameter encoding: value after value, a u8 tag, a u32
201
449
  /// little-endian payload length, the payload. Text and bytes of any length.