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