@yuneta/gobj-js 7.26.13 → 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;
@@ -5241,6 +5253,17 @@ function jdb_init(jdb, prefix, duplicate) {
5241
5253
  return jdb;
5242
5254
  }
5243
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
+ /********************************************
5244
5267
  *
5245
5268
  ********************************************/
5246
5269
  function jdb_update(jdb, topic_name, path, kw) {
@@ -5250,7 +5273,7 @@ function jdb_update(jdb, topic_name, path, kw) {
5250
5273
  return null;
5251
5274
  }
5252
5275
  if (!is_object(kw) || empty_string(kw["id"])) {
5253
- log_error(`jdb_update: record without id: ${JSON.stringify(kw)}`);
5276
+ log_error(`jdb_update: record without id: ${safe_json_text(kw)}`);
5254
5277
  return null;
5255
5278
  }
5256
5279
  if (!is_string(path)) {
@@ -5303,7 +5326,7 @@ function jdb_delete(jdb, topic_name, path, kw) {
5303
5326
  return null;
5304
5327
  }
5305
5328
  if (!is_object(kw) || empty_string(kw["id"])) {
5306
- log_error(`jdb_delete: record without id: ${JSON.stringify(kw)}`);
5329
+ log_error(`jdb_delete: record without id: ${safe_json_text(kw)}`);
5307
5330
  return null;
5308
5331
  }
5309
5332
  if (!is_string(path)) {