@yuneta/gobj-js 7.26.7 → 7.26.9

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.
@@ -4466,6 +4466,26 @@ var gobjJs = (function(exports) {
4466
4466
  return 0;
4467
4467
  }
4468
4468
  /************************************************************
4469
+ * Whether walking `path` meets a value that is neither a dict nor
4470
+ * a list BEFORE its last segment: kw_find_path() logs that one
4471
+ * even when it is not verbose, so a writer that goes on to
4472
+ * kw_set_dict_value() would log it a second time.
4473
+ ************************************************************/
4474
+ function path_crosses_a_scalar(kw, path) {
4475
+ let ss = path.split("`");
4476
+ let v = kw;
4477
+ for (let i = 0; i < ss.length - 1; i++) {
4478
+ if (is_array(v)) {
4479
+ const idx = list_segment_index(v, ss[i]);
4480
+ if (idx < 0) return false;
4481
+ v = v[idx];
4482
+ } else v = Object.prototype.hasOwnProperty.call(v, ss[i]) ? v[ss[i]] : void 0;
4483
+ if (v === void 0 || v === null) return false;
4484
+ if (!(is_object(v) || is_array(v))) return true;
4485
+ }
4486
+ return false;
4487
+ }
4488
+ /************************************************************
4469
4489
  *
4470
4490
  ************************************************************/
4471
4491
  function kw_set_subdict_value(gobj, kw, path, key, value) {
@@ -4474,9 +4494,14 @@ var gobjJs = (function(exports) {
4474
4494
  log_error("kw is not an object");
4475
4495
  return -1;
4476
4496
  }
4477
- let subdict = kw_get_dict(gobj, kw, path, {}, kw_flag_t.KW_CREATE);
4497
+ let subdict = kw_find_path(gobj, kw, path, false);
4498
+ if (subdict === void 0) {
4499
+ if (!is_string(path) || path_crosses_a_scalar(kw, path)) return -1;
4500
+ subdict = {};
4501
+ if (kw_set_dict_value(gobj, kw, path, subdict) < 0) return -1;
4502
+ }
4478
4503
  if (!is_object(subdict)) {
4479
- log_error("subdict is not an object");
4504
+ log_error(`subdict is not an object: '${path}'`);
4480
4505
  return -1;
4481
4506
  }
4482
4507
  set_own_key(subdict, key, value);
@@ -5199,12 +5224,12 @@ var gobjJs = (function(exports) {
5199
5224
  ************************************************************/
5200
5225
  function jdb_init(jdb, prefix, duplicate) {
5201
5226
  if (duplicate) jdb = json_deep_copy(jdb);
5202
- let type = kw_get_dict_value(null, jdb, "type", [], 1);
5203
- kw_get_str(null, jdb, "hook", "data", 1);
5204
- let schema = kw_get_dict_value(null, jdb, "schema", {}, 1);
5205
- let topics = kw_get_dict_value(null, jdb, "topics", {}, 1);
5227
+ let type = kw_get_dict_value(null, jdb, "type", [], kw_flag_t.KW_CREATE);
5228
+ kw_get_str(null, jdb, "hook", "data", kw_flag_t.KW_CREATE);
5229
+ let schema = kw_get_dict_value(null, jdb, "schema", {}, kw_flag_t.KW_CREATE);
5230
+ let topics = kw_get_dict_value(null, jdb, "topics", {}, kw_flag_t.KW_CREATE);
5206
5231
  let walk = function(obj, key, value, full_path) {
5207
- if (key.substring(0, 2) !== "__") kw_get_dict_value(null, topics, full_path, json_deep_copy(type), 1);
5232
+ if (key.substring(0, 2) !== "__") kw_get_dict_value(null, topics, full_path, json_deep_copy(type), kw_flag_t.KW_CREATE);
5208
5233
  };
5209
5234
  _traverse_dict(schema, walk, prefix);
5210
5235
  return jdb;
@@ -5223,19 +5248,23 @@ var gobjJs = (function(exports) {
5223
5248
  trace_msg(kw);
5224
5249
  return null;
5225
5250
  }
5251
+ if (!is_string(path)) {
5252
+ log_error(`jdb_update: path must be a string: ${String(path)}`);
5253
+ return null;
5254
+ }
5226
5255
  let ids = path.split("`");
5227
5256
  let id;
5228
5257
  let v = topic;
5229
5258
  while (id = ids.shift()) {
5230
5259
  if (empty_string(id) || !v) break;
5231
- if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, [], 1);
5260
+ if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5232
5261
  v = _jdb_get(v, null, id, false);
5233
5262
  }
5234
5263
  if (ids.length === 0 && v) {
5235
5264
  if (is_array(v)) v.push(kw);
5236
5265
  else if (v["id"] === kw["id"]) json_object_update(v, kw);
5237
5266
  else {
5238
- let v_ = kw_get_dict_value(null, v, jdb.hook, [], 1);
5267
+ let v_ = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5239
5268
  let v__ = _jdb_get(v_, jdb.hook, kw["id"], false);
5240
5269
  if (v__) json_object_update(v__, kw);
5241
5270
  else v_.push(kw);
@@ -5257,6 +5286,10 @@ var gobjJs = (function(exports) {
5257
5286
  trace_msg(kw);
5258
5287
  return null;
5259
5288
  }
5289
+ if (!is_string(path)) {
5290
+ log_error(`jdb_delete: path must be a string: ${String(path)}`);
5291
+ return null;
5292
+ }
5260
5293
  let ids = path.split("`");
5261
5294
  let id;
5262
5295
  let v = topic;