space-data-module-sdk 0.9.1 → 0.9.2
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 +1 -1
- package/schemas/runtime/HostOps.fbs +6 -12
- package/src/generated/runtime/aligned/HostOps_aligned.js +0 -59
- package/src/generated/runtime/aligned/HostOps_aligned.ts +0 -70
- package/src/generated/runtime/aligned/HostOps_enums.js +0 -2
- package/src/generated/runtime/aligned/HostOps_enums.ts +0 -1
- package/src/generated/runtime/cpp/HostOps_aligned.h +0 -56
- package/src/host/cpp/typedHostcall.hpp +232 -0
- package/src/host/hostWorkerBundleSources.js +3 -3
- package/src/host/typedDispatch.js +27 -22
- package/src/standards/runtimeContracts.js +1 -1
package/package.json
CHANGED
|
@@ -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,
|
|
13
|
-
// streams (size-prefixed frames concatenated),
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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,231 @@ 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 tables a TableList holds (0 when missing or another kind).
|
|
297
|
+
size_t tables(std::string_view key) const {
|
|
298
|
+
const Found f = find(key);
|
|
299
|
+
return f.entry && f.index < 0 && f.entry->KIND() == sdn::runtime::configValueKind_TableList && f.entry->TABLES() ? f.entry->TABLES()->size() : 0;
|
|
300
|
+
}
|
|
301
|
+
/// Table `i` of a TableList ("targets", 2), or the table a path names ("targets.2"; "settings" for a nested object).
|
|
302
|
+
ConfigTable table(std::string_view key, size_t i) const {
|
|
303
|
+
const Found f = find(key);
|
|
304
|
+
if (!f.entry || f.index >= 0 || f.entry->KIND() != sdn::runtime::configValueKind_TableList || !f.entry->TABLES() || i >= f.entry->TABLES()->size()) return {};
|
|
305
|
+
return ConfigTable(f.entry->TABLES()->Get(static_cast<flatbuffers::uoffset_t>(i))->ENTRIES());
|
|
306
|
+
}
|
|
307
|
+
ConfigTable table(std::string_view key) const {
|
|
308
|
+
const Found f = find(key);
|
|
309
|
+
if (!f.entry || f.entry->KIND() != sdn::runtime::configValueKind_TableList || !f.entry->TABLES()) return {};
|
|
310
|
+
const size_t i = f.index >= 0 ? static_cast<size_t>(f.index) : 0;
|
|
311
|
+
if (i >= f.entry->TABLES()->size() || (f.index < 0 && f.entry->TABLES()->size() != 1)) return {};
|
|
312
|
+
return ConfigTable(f.entry->TABLES()->Get(static_cast<flatbuffers::uoffset_t>(i))->ENTRIES());
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private:
|
|
316
|
+
struct Found {
|
|
317
|
+
const sdn::runtime::ConfigEntry* entry = nullptr;
|
|
318
|
+
int64_t index = -1; // an item of a list, when the path ends in an index
|
|
319
|
+
};
|
|
320
|
+
static std::string_view view(const flatbuffers::String* s) { return s ? std::string_view(s->c_str(), s->size()) : std::string_view(); }
|
|
321
|
+
static bool index_of(std::string_view s, int64_t& out) {
|
|
322
|
+
if (s.empty() || s.size() > 18) return false;
|
|
323
|
+
int64_t v = 0;
|
|
324
|
+
for (char c : s) { if (c < '0' || c > '9') return false; v = v * 10 + (c - '0'); }
|
|
325
|
+
out = v;
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
/// The entry with exactly this key: a binary search (the entries are sorted by KEY), then a scan in case a writer
|
|
329
|
+
/// sorted by another collation.
|
|
330
|
+
static const sdn::runtime::ConfigEntry* lookup(const ConfigEntries* entries, std::string_view key) {
|
|
331
|
+
if (!entries) return nullptr;
|
|
332
|
+
size_t lo = 0, hi = entries->size();
|
|
333
|
+
while (lo < hi) {
|
|
334
|
+
const size_t mid = lo + (hi - lo) / 2;
|
|
335
|
+
const auto* e = entries->Get(static_cast<flatbuffers::uoffset_t>(mid));
|
|
336
|
+
const int c = view(e->KEY()).compare(key);
|
|
337
|
+
if (c == 0) return e;
|
|
338
|
+
if (c < 0) lo = mid + 1; else hi = mid;
|
|
339
|
+
}
|
|
340
|
+
for (const auto* e : *entries) if (view(e->KEY()) == key) return e;
|
|
341
|
+
return nullptr;
|
|
342
|
+
}
|
|
343
|
+
static Found resolve(const ConfigEntries* entries, std::string_view path, int depth) {
|
|
344
|
+
if (!entries || path.empty() || depth > 64) return {};
|
|
345
|
+
if (const auto* e = lookup(entries, path)) return {e, -1};
|
|
346
|
+
// the longest key before a dot first, so keys that contain dots win over nesting
|
|
347
|
+
for (size_t dot = path.rfind('.'); dot != std::string_view::npos && dot > 0; dot = path.rfind('.', dot - 1)) {
|
|
348
|
+
const auto* e = lookup(entries, path.substr(0, dot));
|
|
349
|
+
if (!e) continue;
|
|
350
|
+
std::string_view rest = path.substr(dot + 1);
|
|
351
|
+
const size_t next = rest.find('.');
|
|
352
|
+
const std::string_view head = rest.substr(0, next);
|
|
353
|
+
int64_t index = -1;
|
|
354
|
+
const bool indexed = index_of(head, index);
|
|
355
|
+
switch (e->KIND()) {
|
|
356
|
+
case sdn::runtime::configValueKind_NumberList:
|
|
357
|
+
case sdn::runtime::configValueKind_StringList:
|
|
358
|
+
if (indexed && next == std::string_view::npos) return {e, index};
|
|
359
|
+
return {};
|
|
360
|
+
case sdn::runtime::configValueKind_TableList: {
|
|
361
|
+
const auto* tables = e->TABLES();
|
|
362
|
+
if (!tables) return {};
|
|
363
|
+
if (indexed) {
|
|
364
|
+
if (static_cast<uint64_t>(index) >= tables->size()) return {};
|
|
365
|
+
if (next == std::string_view::npos) return {e, index}; // "targets.0": the table itself
|
|
366
|
+
return resolve(tables->Get(static_cast<flatbuffers::uoffset_t>(index))->ENTRIES(), rest.substr(next + 1), depth + 1);
|
|
367
|
+
}
|
|
368
|
+
if (tables->size() != 1) return {};
|
|
369
|
+
return resolve(tables->Get(0)->ENTRIES(), rest, depth + 1); // a nested object: one table, no index
|
|
370
|
+
}
|
|
371
|
+
default:
|
|
372
|
+
return {};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return {};
|
|
376
|
+
}
|
|
377
|
+
Found find(std::string_view key) const { return resolve(entries_, key, 0); }
|
|
378
|
+
|
|
379
|
+
const ConfigEntries* entries_ = nullptr;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/// The module's config, as plugin.getConfig answers it: the host's $PIV response kept whole and its raw "config"
|
|
383
|
+
/// frame - one ModuleConfig FlatBuffer (MCFG) - verified and read in place. Lookups are ConfigTable's. Move-only
|
|
384
|
+
/// (the views point into the kept bytes).
|
|
385
|
+
class Config : public ConfigTable {
|
|
386
|
+
public:
|
|
387
|
+
explicit Config(Response response) : response_(std::move(response)) {
|
|
388
|
+
if (!response_.ok()) { error_ = response_.error_message(); code_ = response_.error_code(); return; }
|
|
389
|
+
std::string_view frame = response_.raw("config");
|
|
390
|
+
if (frame.empty()) { fail("plugin.getConfig answered without a \"config\" frame"); return; }
|
|
391
|
+
const uint8_t* data = reinterpret_cast<const uint8_t*>(frame.data());
|
|
392
|
+
if (reinterpret_cast<uintptr_t>(data) % 8) { // the invoke arena aligns frames to 8; copy only if a host did not
|
|
393
|
+
aligned_.resize((frame.size() + 7) / 8);
|
|
394
|
+
std::memcpy(aligned_.data(), frame.data(), frame.size());
|
|
395
|
+
data = reinterpret_cast<const uint8_t*>(aligned_.data());
|
|
396
|
+
}
|
|
397
|
+
flatbuffers::Verifier::Options options;
|
|
398
|
+
options.max_tables = 100000000; // TableList-heavy configs: the buffer size bounds the work anyway
|
|
399
|
+
flatbuffers::Verifier verifier(data, frame.size(), options);
|
|
400
|
+
if (!sdn::runtime::VerifyModuleConfigBuffer(verifier)) { fail("plugin.getConfig: \"config\" is not a ModuleConfig (MCFG) FlatBuffer"); return; }
|
|
401
|
+
root_ = sdn::runtime::GetModuleConfig(data);
|
|
402
|
+
size_ = frame.size();
|
|
403
|
+
static_cast<ConfigTable&>(*this) = ConfigTable(root_->ENTRIES());
|
|
404
|
+
}
|
|
405
|
+
Config(Config&&) = default;
|
|
406
|
+
Config& operator=(Config&&) = default;
|
|
407
|
+
Config(const Config&) = delete;
|
|
408
|
+
Config& operator=(const Config&) = delete;
|
|
409
|
+
|
|
410
|
+
bool ok() const { return root_ != nullptr; }
|
|
411
|
+
std::string error_code() const { return code_; }
|
|
412
|
+
std::string error_message() const { return error_; }
|
|
413
|
+
/// The verified ModuleConfig root (nullptr when the call failed).
|
|
414
|
+
const sdn::runtime::ModuleConfig* root() const { return root_; }
|
|
415
|
+
/// The ModuleConfig bytes' length.
|
|
416
|
+
size_t byte_size() const { return size_; }
|
|
417
|
+
|
|
418
|
+
private:
|
|
419
|
+
void fail(const char* message) { error_ = message; code_ = "failed"; }
|
|
420
|
+
Response response_;
|
|
421
|
+
std::vector<uint64_t> aligned_;
|
|
422
|
+
const sdn::runtime::ModuleConfig* root_ = nullptr;
|
|
423
|
+
size_t size_ = 0;
|
|
424
|
+
std::string error_;
|
|
425
|
+
std::string code_;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
/// plugin.getConfig: the module's config (no capability needed).
|
|
429
|
+
inline Config get_config() { return Config(call("plugin.getConfig")); }
|
|
430
|
+
|
|
199
431
|
/// SQL values (raw "params" of storage.flatsql_query_stream / query_sandboxed / flatsql_cache_key, raw "cells" of a
|
|
200
432
|
/// query_sandboxed rows answer) in FlatSQL's tagged parameter encoding: value after value, a u8 tag, a u32
|
|
201
433
|
/// little-endian payload length, the payload. Text and bytes of any length.
|