@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.
@@ -4340,8 +4340,10 @@
4340
4340
  /************************************************************
4341
4341
  * As the C kw_get_dict(): a dict found is the answer, and
4342
4342
  * anything else gives the default back AS GIVEN (null stays
4343
- * null). KW_CREATE stores a dict default, never a null one.
4344
- * Before gobj-js 7.25.2 the answer went through Object(), so a
4343
+ * null). KW_CREATE stores a dict default, never a null one, and
4344
+ * replaces a null at the path; over another type it logs and
4345
+ * gives the default back unwritten (C returns NULL there, SDK
4346
+ * 7.26.9). Before gobj-js 7.25.2 the answer went through Object(), so a
4345
4347
  * null default came back as {} and a 0 as a Number object.
4346
4348
  ************************************************************/
4347
4349
  function kw_get_dict(gobj, kw, path, default_value, flag) {
@@ -4362,8 +4364,12 @@
4362
4364
  return default_value;
4363
4365
  }
4364
4366
  if (!is_object(v)) {
4365
- if (required) {
4366
- log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'`);
4367
+ if (create && v === null && is_object(default_value)) {
4368
+ kw_set_dict_value(gobj, kw, path, default_value);
4369
+ return default_value;
4370
+ }
4371
+ if (required || create) {
4372
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json dict: '${path}'` + (create ? ", cannot create" : ""));
4367
4373
  trace_json_masked(kw);
4368
4374
  }
4369
4375
  return default_value;
@@ -4397,7 +4403,9 @@
4397
4403
  /************************************************************
4398
4404
  * As the C kw_get_list(): a list found is the answer, and
4399
4405
  * anything else gives the default back AS GIVEN. KW_CREATE
4400
- * stores a list default, never a null one. Before gobj-js
4406
+ * stores a list default, never a null one, and replaces a null
4407
+ * at the path; over another type it logs and gives the default
4408
+ * back unwritten (C returns NULL there, SDK 7.26.9). Before gobj-js
4401
4409
  * 7.25.2 the answer went through Array(), which WRAPS its
4402
4410
  * argument: a list found came back as [list], a default of []
4403
4411
  * as [[]] and a null one as [null].
@@ -4420,8 +4428,12 @@
4420
4428
  return default_value;
4421
4429
  }
4422
4430
  if (!is_array(v)) {
4423
- if (required) {
4424
- log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'`);
4431
+ if (create && v === null && is_array(default_value)) {
4432
+ kw_set_dict_value(gobj, kw, path, default_value);
4433
+ return default_value;
4434
+ }
4435
+ if (required || create) {
4436
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}path MUST BE a json list: '${path}'` + (create ? ", cannot create" : ""));
4425
4437
  trace_json_masked(kw);
4426
4438
  }
4427
4439
  return default_value;
@@ -4446,6 +4458,10 @@
4446
4458
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}kw_set_dict_value(): path empty`);
4447
4459
  return -1;
4448
4460
  }
4461
+ if (path.split("`").includes("")) {
4462
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}kw_set_dict_value(): empty segment in path: '${path}'`);
4463
+ return -1;
4464
+ }
4449
4465
  let ss = path.split("`");
4450
4466
  let len = ss.length;
4451
4467
  for (let i = 0; i < len; i++) {
@@ -5241,6 +5257,17 @@
5241
5257
  return jdb;
5242
5258
  }
5243
5259
  /********************************************
5260
+ * A value as json text for a log line: the error path must not throw
5261
+ * (a circular record, a BigInt).
5262
+ ********************************************/
5263
+ function safe_json_text(v) {
5264
+ try {
5265
+ return JSON.stringify(v);
5266
+ } catch (e) {
5267
+ return `<${typeof v}: not json (${e.message})>`;
5268
+ }
5269
+ }
5270
+ /********************************************
5244
5271
  *
5245
5272
  ********************************************/
5246
5273
  function jdb_update(jdb, topic_name, path, kw) {
@@ -5250,7 +5277,7 @@
5250
5277
  return null;
5251
5278
  }
5252
5279
  if (!is_object(kw) || empty_string(kw["id"])) {
5253
- log_error(`jdb_update: record without id: ${JSON.stringify(kw)}`);
5280
+ log_error(`jdb_update: record without id: ${safe_json_text(kw)}`);
5254
5281
  return null;
5255
5282
  }
5256
5283
  if (!is_string(path)) {
@@ -5303,7 +5330,7 @@
5303
5330
  return null;
5304
5331
  }
5305
5332
  if (!is_object(kw) || empty_string(kw["id"])) {
5306
- log_error(`jdb_delete: record without id: ${JSON.stringify(kw)}`);
5333
+ log_error(`jdb_delete: record without id: ${safe_json_text(kw)}`);
5307
5334
  return null;
5308
5335
  }
5309
5336
  if (!is_string(path)) {