Skip to content

Commit

Permalink
support more json-legacy-path
Browse files Browse the repository at this point in the history
  • Loading branch information
mapleFU committed Oct 16, 2023
1 parent 69ddf73 commit 592d348
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/types/json_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ StatusOr<JsonPath> JsonPath::BuildJsonPath(std::string_view path) {
}

// https://redis.io/docs/data-types/json/path/#legacy-path-syntax
std::optional<std::string_view> JsonPath::tryConvertLegacyToJsonPath(std::string_view path) {
// TODO(mwish): currently I just handle the simplest logic,
// port from RedisJson JsonPathParser::parse later.
if (path == ".") {
return "$";
std::optional<std::string> JsonPath::tryConvertLegacyToJsonPath(std::string_view path) {
if (path.empty()) {
return std::nullopt;
}
return std::nullopt;
if (path[0] == '$') {
return std::nullopt;
}
if (path[0] == '.') {
if (path.size() == 1) {
return "$";
}
return std::string("$") + std::string(path);
}
return std::string("$.") + std::string(path);
}
5 changes: 4 additions & 1 deletion src/types/json_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class JsonPath {
return origin_;
}

std::string_view OriginPath() const { return origin_; }

bool IsRootPath() const { return Path() == ROOT_PATH; }

JsonType EvalQueryExpression(const JsonType& json_value) const {
Expand All @@ -63,12 +65,13 @@ class JsonPath {
}

private:
static std::optional<std::string_view> tryConvertLegacyToJsonPath(std::string_view path);
static std::optional<std::string> tryConvertLegacyToJsonPath(std::string_view path);

JsonPath(std::string path, std::string fixed_path, JsonPathExpression path_expression)
: origin_(std::move(path)), fixed_path_(std::move(fixed_path)), expression_(std::move(path_expression)) {}

std::string origin_;
std::string fixed_path_;
// Pre-build the expression to avoid built it multiple times.
JsonPathExpression expression_;
};
2 changes: 1 addition & 1 deletion src/types/redis_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ rocksdb::Status Json::Get(const std::string &user_key, const std::vector<std::st
if (!json_path_result) return rocksdb::Status::InvalidArgument(json_path_result.Msg());
auto get_res = json_val.Get(json_path_result.GetValue());
if (!get_res) return rocksdb::Status::InvalidArgument(get_res.Msg());
res.value.insert_or_assign(json_path_result->Path(), std::move(get_res->value));
res.value.insert_or_assign(json_path_result->OriginPath(), std::move(get_res->value));
}
}

Expand Down

0 comments on commit 592d348

Please sign in to comment.