@yuneta/gobj-js 7.26.8 → 7.26.10

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.
@@ -4442,6 +4442,10 @@
4442
4442
  log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}kw_set_dict_value(): path must be a string: ${String(path)}`);
4443
4443
  return -1;
4444
4444
  }
4445
+ if (path === "") {
4446
+ log_error(`${gobj ? gobj_short_name(gobj) + ": " : ""}kw_set_dict_value(): path empty`);
4447
+ return -1;
4448
+ }
4445
4449
  let ss = path.split("`");
4446
4450
  let len = ss.length;
4447
4451
  for (let i = 0; i < len; i++) {
@@ -4468,6 +4472,26 @@
4468
4472
  return 0;
4469
4473
  }
4470
4474
  /************************************************************
4475
+ * Whether walking `path` meets a value that is neither a dict nor
4476
+ * a list BEFORE its last segment: kw_find_path() logs that one
4477
+ * even when it is not verbose, so a writer that goes on to
4478
+ * kw_set_dict_value() would log it a second time.
4479
+ ************************************************************/
4480
+ function path_crosses_a_scalar(kw, path) {
4481
+ let ss = path.split("`");
4482
+ let v = kw;
4483
+ for (let i = 0; i < ss.length - 1; i++) {
4484
+ if (is_array(v)) {
4485
+ const idx = list_segment_index(v, ss[i]);
4486
+ if (idx < 0) return false;
4487
+ v = v[idx];
4488
+ } else v = Object.prototype.hasOwnProperty.call(v, ss[i]) ? v[ss[i]] : void 0;
4489
+ if (v === void 0 || v === null) return false;
4490
+ if (!(is_object(v) || is_array(v))) return true;
4491
+ }
4492
+ return false;
4493
+ }
4494
+ /************************************************************
4471
4495
  *
4472
4496
  ************************************************************/
4473
4497
  function kw_set_subdict_value(gobj, kw, path, key, value) {
@@ -4478,7 +4502,7 @@
4478
4502
  }
4479
4503
  let subdict = kw_find_path(gobj, kw, path, false);
4480
4504
  if (subdict === void 0) {
4481
- if (!is_string(path)) return -1;
4505
+ if (!is_string(path) || path_crosses_a_scalar(kw, path)) return -1;
4482
4506
  subdict = {};
4483
4507
  if (kw_set_dict_value(gobj, kw, path, subdict) < 0) return -1;
4484
4508
  }
@@ -5206,12 +5230,12 @@
5206
5230
  ************************************************************/
5207
5231
  function jdb_init(jdb, prefix, duplicate) {
5208
5232
  if (duplicate) jdb = json_deep_copy(jdb);
5209
- let type = kw_get_dict_value(null, jdb, "type", [], 1);
5210
- kw_get_str(null, jdb, "hook", "data", 1);
5211
- let schema = kw_get_dict_value(null, jdb, "schema", {}, 1);
5212
- let topics = kw_get_dict_value(null, jdb, "topics", {}, 1);
5233
+ let type = kw_get_dict_value(null, jdb, "type", [], kw_flag_t.KW_CREATE);
5234
+ kw_get_str(null, jdb, "hook", "data", kw_flag_t.KW_CREATE);
5235
+ let schema = kw_get_dict_value(null, jdb, "schema", {}, kw_flag_t.KW_CREATE);
5236
+ let topics = kw_get_dict_value(null, jdb, "topics", {}, kw_flag_t.KW_CREATE);
5213
5237
  let walk = function(obj, key, value, full_path) {
5214
- if (key.substring(0, 2) !== "__") kw_get_dict_value(null, topics, full_path, json_deep_copy(type), 1);
5238
+ if (key.substring(0, 2) !== "__" && !is_object(value)) kw_get_dict_value(null, topics, full_path, json_deep_copy(type), kw_flag_t.KW_CREATE);
5215
5239
  };
5216
5240
  _traverse_dict(schema, walk, prefix);
5217
5241
  return jdb;
@@ -5239,14 +5263,18 @@
5239
5263
  let v = topic;
5240
5264
  while (id = ids.shift()) {
5241
5265
  if (empty_string(id) || !v) break;
5242
- if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, [], 1);
5266
+ if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5243
5267
  v = _jdb_get(v, null, id, false);
5244
5268
  }
5269
+ if (!v || ids.length !== 0) {
5270
+ log_error(`jdb_update: path not found in topic '${topic_name}': '${path}'`);
5271
+ return null;
5272
+ }
5245
5273
  if (ids.length === 0 && v) {
5246
5274
  if (is_array(v)) v.push(kw);
5247
5275
  else if (v["id"] === kw["id"]) json_object_update(v, kw);
5248
5276
  else {
5249
- let v_ = kw_get_dict_value(null, v, jdb.hook, [], 1);
5277
+ let v_ = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5250
5278
  let v__ = _jdb_get(v_, jdb.hook, kw["id"], false);
5251
5279
  if (v__) json_object_update(v__, kw);
5252
5280
  else v_.push(kw);
@@ -5280,29 +5308,30 @@
5280
5308
  if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, null, 0);
5281
5309
  v = _jdb_get(v, null, id, false);
5282
5310
  }
5283
- if (ids.length === 0 && v) {
5284
- if (is_array(v)) {
5285
- let idx = id_index_in_obj_list(v, kw["id"]);
5286
- if (idx >= 0) v.splice(idx, 1);
5287
- } else {
5288
- let v_ = kw_get_dict_value(null, v, jdb.hook, null, 0);
5289
- let idx = id_index_in_obj_list(v_, kw["id"]);
5290
- if (idx >= 0) v_.splice(idx, 1);
5291
- }
5311
+ if (!v || ids.length !== 0) {
5312
+ log_error(`jdb_delete: path not found in topic '${topic_name}': '${path}'`);
5313
+ return null;
5314
+ }
5315
+ let list = is_array(v) ? v : kw_get_dict_value(null, v, jdb.hook, null, 0);
5316
+ let idx = id_index_in_obj_list(list, kw["id"]);
5317
+ if (idx < 0) {
5318
+ log_error(`jdb_delete: record not found in topic '${topic_name}': '${path}' '${kw["id"]}'`);
5319
+ return null;
5292
5320
  }
5321
+ list.splice(idx, 1);
5293
5322
  return topic;
5294
5323
  }
5295
5324
  /********************************************
5296
5325
  *
5297
5326
  ********************************************/
5298
5327
  function jdb_get_topic(jdb, topic_name) {
5299
- let topics = kw_get_dict_value(null, jdb, "topics", null, 0, false);
5328
+ let topics = kw_get_dict_value(null, jdb, "topics", null, 0);
5300
5329
  if (!topics) {
5301
5330
  log_error("jdb topics section not found");
5302
5331
  trace_msg(jdb);
5303
5332
  return null;
5304
5333
  }
5305
- let topic = kw_get_dict_value(null, topics, topic_name, null, 0, false);
5334
+ let topic = kw_get_dict_value(null, topics, topic_name, null, 0);
5306
5335
  if (!topic) {
5307
5336
  log_error("jdb topic not found: " + topic_name);
5308
5337
  trace_msg(topics);