Size: 2230 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// cs/net/proto/db/db.cc
#include "cs/net/proto/db/db.hh"

#include <sstream>
#include <string>

#include "cs/fs/fs.hh"
#include "cs/net/json/object.hh"
#include "cs/net/json/parsers.hh"
#include "cs/parsers/parsers.hh"
#include "cs/result.hh"

namespace {  // use_usings
using ::cs::Error;
using ::cs::InvalidArgument;
using ::cs::Ok;
using ::cs::Result;
using ::cs::ResultOr;
using ::cs::net::json::Object;
using ::cs::net::json::parsers::ParseObject;
}  // namespace

namespace {
struct ResourceName {
  std::string file_path;
};

ResultOr<ResourceName> ParseResourceName(std::string str) {
  if (str.empty()) {
    return TRACE(
        InvalidArgument("Empty resource name string"));
  }
  unsigned int n = str.size();
  if (n >= 64) {
    return TRACE(InvalidArgument(FMT(
        "Resource name must be less than 64 chars, not %u",
        n)));
  }
  unsigned int i;
  for (i = 0; i < n; i++) {
    char c = str[i];
    if ('a' <= c && c <= 'z') {
      continue;
    }
    if ('A' <= c && c <= 'Z') {
      continue;
    }
    if ('0' <= c && c <= '9') {
      continue;
    }
    if ('-' == c) {
      continue;
    }
    return TRACE(
        InvalidArgument(FMT("str[%u] = %c, invalid. must "
                            "be a-z, A-Z, 0-9, or -.",
                            i, c)));
  }

  ResourceName name;
  name.file_path = str + ".json";
  return name;
}
}  // namespace

namespace cs::net::proto::db {

ResultOr<Object> LocalJsonDatabase::get(
    std::string resource_name) {
  SET_OR_RET(ResourceName name,
             ParseResourceName(resource_name));
  SET_OR_RET(std::string json,
             cs::fs::read(cs::fs::Join(data_abs_dir_,
                                       name.file_path)));

  Object node;
  SET_OR_RET(node, ParseObject(json));
  return node;
}

Result LocalJsonDatabase::set(std::string path,
                              Object obj) {
  OK_OR_RET(cs::fs::mkdir(data_abs_dir_));
  std::string enclosing_folder_path =
      path.substr(0, path.find_last_of('/'));
  OK_OR_RET(cs::fs::mkdir(
      cs::fs::Join(data_abs_dir_, enclosing_folder_path)));
  return cs::fs::write(cs::fs::Join(data_abs_dir_, path),
                       obj.str());
}

}  // namespace cs::net::proto::db
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.