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