@yuneta/gobj-js 7.26.12 → 7.26.14

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.
@@ -4336,8 +4336,10 @@ function kw_get_pointer(gobj, kw, path, default_value, flag) {
4336
4336
  /************************************************************
4337
4337
  * As the C kw_get_dict(): a dict found is the answer, and
4338
4338
  * anything else gives the default back AS GIVEN (null stays
4339
- * null). KW_CREATE stores a dict default, never a null one.
4340
- * Before gobj-js 7.25.2 the answer went through Object(), so a
4339
+ * null). KW_CREATE stores a dict default, never a null one, and
4340
+ * replaces a null at the path; over another type it logs and
4341
+ * gives the default back unwritten (C returns NULL there, SDK
4342
+ * 7.26.9). Before gobj-js 7.25.2 the answer went through Object(), so a
4341
4343
  * null default came back as {} and a 0 as a Number object.
4342
4344
  ************************************************************/
4343
4345
  function kw_get_dict(gobj, kw, path, default_value, flag) {
@@ -4358,8 +4360,12 @@ function kw_get_dict(gobj, kw, path, default_value, flag) {
4358
4360
  return default_value;
4359
4361
  }
4360
4362
  if (!is_object(v)) {
4361
- if (required) {
4362
- log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'`);
4363
+ if (create && v === null && is_object(default_value)) {
4364
+ kw_set_dict_value(gobj, kw, path, default_value);
4365
+ return default_value;
4366
+ }
4367
+ if (required || create) {
4368
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'` + (create ? ", cannot create" : ""));
4363
4369
  trace_json_masked(kw);
4364
4370
  }
4365
4371
  return default_value;
@@ -4393,7 +4399,9 @@ function kw_get_dict_value(gobj, kw, path, default_value, flag) {
4393
4399
  /************************************************************
4394
4400
  * As the C kw_get_list(): a list found is the answer, and
4395
4401
  * anything else gives the default back AS GIVEN. KW_CREATE
4396
- * stores a list default, never a null one. Before gobj-js
4402
+ * stores a list default, never a null one, and replaces a null
4403
+ * at the path; over another type it logs and gives the default
4404
+ * back unwritten (C returns NULL there, SDK 7.26.9). Before gobj-js
4397
4405
  * 7.25.2 the answer went through Array(), which WRAPS its
4398
4406
  * argument: a list found came back as [list], a default of []
4399
4407
  * as [[]] and a null one as [null].
@@ -4416,8 +4424,12 @@ function kw_get_list(gobj, kw, path, default_value, flag) {
4416
4424
  return default_value;
4417
4425
  }
4418
4426
  if (!is_array(v)) {
4419
- if (required) {
4420
- log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'`);
4427
+ if (create && v === null && is_array(default_value)) {
4428
+ kw_set_dict_value(gobj, kw, path, default_value);
4429
+ return default_value;
4430
+ }
4431
+ if (required || create) {
4432
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'` + (create ? ", cannot create" : ""));
4421
4433
  trace_json_masked(kw);
4422
4434
  }
4423
4435
  return default_value;
@@ -4442,6 +4454,10 @@ function kw_set_dict_value(gobj, kw, path, value) {
4442
4454
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}kw_set_dict_value(): path empty`);
4443
4455
  return -1;
4444
4456
  }
4457
+ if (path.split("`").includes("")) {
4458
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}kw_set_dict_value(): empty segment in path: '${path}'`);
4459
+ return -1;
4460
+ }
4445
4461
  let ss = path.split("`");
4446
4462
  let len = ss.length;
4447
4463
  for (let i = 0; i < len; i++) {
@@ -5237,6 +5253,17 @@ function jdb_init(jdb, prefix, duplicate) {
5237
5253
  return jdb;
5238
5254
  }
5239
5255
  /********************************************
5256
+ * A value as json text for a log line: the error path must not throw
5257
+ * (a circular record, a BigInt).
5258
+ ********************************************/
5259
+ function safe_json_text(v) {
5260
+ try {
5261
+ return JSON.stringify(v);
5262
+ } catch (e) {
5263
+ return `<${typeof v}: not json (${e.message})>`;
5264
+ }
5265
+ }
5266
+ /********************************************
5240
5267
  *
5241
5268
  ********************************************/
5242
5269
  function jdb_update(jdb, topic_name, path, kw) {
@@ -5246,7 +5273,7 @@ function jdb_update(jdb, topic_name, path, kw) {
5246
5273
  return null;
5247
5274
  }
5248
5275
  if (!is_object(kw) || empty_string(kw["id"])) {
5249
- log_error(`jdb_update: record without id: ${JSON.stringify(kw)}`);
5276
+ log_error(`jdb_update: record without id: ${safe_json_text(kw)}`);
5250
5277
  return null;
5251
5278
  }
5252
5279
  if (!is_string(path)) {
@@ -5299,7 +5326,7 @@ function jdb_delete(jdb, topic_name, path, kw) {
5299
5326
  return null;
5300
5327
  }
5301
5328
  if (!is_object(kw) || empty_string(kw["id"])) {
5302
- log_error(`jdb_delete: record without id: ${JSON.stringify(kw)}`);
5329
+ log_error(`jdb_delete: record without id: ${safe_json_text(kw)}`);
5303
5330
  return null;
5304
5331
  }
5305
5332
  if (!is_string(path)) {