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