@yuneta/gobj-js 7.26.9 → 7.26.11

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++) {
@@ -5231,7 +5235,7 @@
5231
5235
  let schema = kw_get_dict_value(null, jdb, "schema", {}, kw_flag_t.KW_CREATE);
5232
5236
  let topics = kw_get_dict_value(null, jdb, "topics", {}, kw_flag_t.KW_CREATE);
5233
5237
  let walk = function(obj, key, value, full_path) {
5234
- if (key.substring(0, 2) !== "__") kw_get_dict_value(null, topics, full_path, json_deep_copy(type), kw_flag_t.KW_CREATE);
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);
5235
5239
  };
5236
5240
  _traverse_dict(schema, walk, prefix);
5237
5241
  return jdb;
@@ -5259,19 +5263,31 @@
5259
5263
  let v = topic;
5260
5264
  while (id = ids.shift()) {
5261
5265
  if (empty_string(id) || !v) break;
5262
- if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5266
+ if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, null, 0);
5263
5267
  v = _jdb_get(v, null, id, false);
5264
5268
  }
5265
- if (ids.length === 0 && v) {
5266
- if (is_array(v)) v.push(kw);
5267
- else if (v["id"] === kw["id"]) json_object_update(v, kw);
5268
- else {
5269
- let v_ = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5270
- let v__ = _jdb_get(v_, jdb.hook, kw["id"], false);
5271
- if (v__) json_object_update(v__, kw);
5272
- else v_.push(kw);
5269
+ if (!v || ids.length !== 0) {
5270
+ log_error(`jdb_update: path not found in topic '${topic_name}': '${path}'`);
5271
+ return null;
5272
+ }
5273
+ let list;
5274
+ if (is_array(v)) list = v;
5275
+ else if (v["id"] === kw["id"]) {
5276
+ json_object_update(v, kw);
5277
+ return topic;
5278
+ } else {
5279
+ list = kw_get_dict_value(null, v, jdb.hook, null, 0);
5280
+ if (list === null || list === void 0) {
5281
+ list = [];
5282
+ json_object_set(v, jdb.hook, list);
5283
+ } else if (!is_array(list)) {
5284
+ log_error(`jdb_update: the hook '${jdb.hook}' of '${path}' is not a list in topic '${topic_name}'`);
5285
+ return null;
5273
5286
  }
5274
5287
  }
5288
+ let found = _jdb_get(list, jdb.hook, kw["id"], false);
5289
+ if (found) json_object_update(found, kw);
5290
+ else list.push(kw);
5275
5291
  return topic;
5276
5292
  }
5277
5293
  /********************************************
@@ -5300,29 +5316,30 @@
5300
5316
  if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, null, 0);
5301
5317
  v = _jdb_get(v, null, id, false);
5302
5318
  }
5303
- if (ids.length === 0 && v) {
5304
- if (is_array(v)) {
5305
- let idx = id_index_in_obj_list(v, kw["id"]);
5306
- if (idx >= 0) v.splice(idx, 1);
5307
- } else {
5308
- let v_ = kw_get_dict_value(null, v, jdb.hook, null, 0);
5309
- let idx = id_index_in_obj_list(v_, kw["id"]);
5310
- if (idx >= 0) v_.splice(idx, 1);
5311
- }
5319
+ if (!v || ids.length !== 0) {
5320
+ log_error(`jdb_delete: path not found in topic '${topic_name}': '${path}'`);
5321
+ return null;
5322
+ }
5323
+ let list = is_array(v) ? v : kw_get_dict_value(null, v, jdb.hook, null, 0);
5324
+ let idx = id_index_in_obj_list(list, kw["id"]);
5325
+ if (idx < 0) {
5326
+ log_error(`jdb_delete: record not found in topic '${topic_name}': '${path}' '${kw["id"]}'`);
5327
+ return null;
5312
5328
  }
5329
+ list.splice(idx, 1);
5313
5330
  return topic;
5314
5331
  }
5315
5332
  /********************************************
5316
5333
  *
5317
5334
  ********************************************/
5318
5335
  function jdb_get_topic(jdb, topic_name) {
5319
- let topics = kw_get_dict_value(null, jdb, "topics", null, 0, false);
5336
+ let topics = kw_get_dict_value(null, jdb, "topics", null, 0);
5320
5337
  if (!topics) {
5321
5338
  log_error("jdb topics section not found");
5322
5339
  trace_msg(jdb);
5323
5340
  return null;
5324
5341
  }
5325
- let topic = kw_get_dict_value(null, topics, topic_name, null, 0, false);
5342
+ let topic = kw_get_dict_value(null, topics, topic_name, null, 0);
5326
5343
  if (!topic) {
5327
5344
  log_error("jdb topic not found: " + topic_name);
5328
5345
  trace_msg(topics);