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