Skip to content

Commit

Permalink
Rename identify --relative-from/-f to --relative-to/-t
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jul 30, 2024
1 parent e564d9f commit 458b22d
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docs/identify.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Identify
========

```sh
jsonschema identify <schema.json> [--relative-from/-f <uri>] [--verbose/-v]
jsonschema identify <schema.json> [--relative-to/-t <uri>] [--verbose/-v]
```

A schema may be associated with a URI through the use of keywords like `$id` or
Expand Down Expand Up @@ -31,7 +31,7 @@ We can interact with the identifier URI of this schema as follows:
$ jsonschema identify schema.json
https://example.com/foo/bar

$ jsonschema identify schema.json --relative-from "https://example.com"
$ jsonschema identify schema.json --relative-to "https://example.com"
/foo/bar
```

Expand All @@ -44,5 +44,5 @@ jsonschema identify path/to/my/schema.json
### Identify a JSON Schema resolving the URI relative to a given base

```sh
jsonschema identify path/to/my/schema.json --relative-from "https://example.com"
jsonschema identify path/to/my/schema.json --relative-to "https://example.com"
```
25 changes: 12 additions & 13 deletions src/command_identify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
#include "command.h"
#include "utils.h"

static auto find_relative_from(
const std::map<std::string, std::vector<std::string>> &options)
static auto
find_relative_to(const std::map<std::string, std::vector<std::string>> &options)
-> std::optional<std::string> {
if (options.contains("relative-from") &&
!options.at("relative-from").empty()) {
return options.at("relative-from").front();
} else if (options.contains("f") && !options.at("f").empty()) {
return options.at("f").front();
if (options.contains("relative-to") && !options.at("relative-to").empty()) {
return options.at("relative-to").front();
} else if (options.contains("t") && !options.at("t").empty()) {
return options.at("t").front();
}

return std::nullopt;
Expand Down Expand Up @@ -87,19 +86,19 @@ auto intelligence::jsonschema::cli::identify(
return EXIT_FAILURE;
}

const auto relative_from{find_relative_from(options)};
if (relative_from.has_value()) {
const auto relative_to{find_relative_to(options)};
if (relative_to.has_value()) {
log_verbose(options) << "Resolving identifier against: "
<< relative_from.value() << "\n";
<< relative_to.value() << "\n";
std::string base;

try {
base = sourcemeta::jsontoolkit::URI{relative_from.value()}
base = sourcemeta::jsontoolkit::URI{relative_to.value()}
.canonicalize()
.recompose();
} catch (const sourcemeta::jsontoolkit::URIParseError &error) {
std::cerr << "error: Invalid base URI at column " << error.column()
<< "\n " << relative_from.value() << "\n";
<< "\n " << relative_to.value() << "\n";
return EXIT_FAILURE;
}

Expand All @@ -113,7 +112,7 @@ auto intelligence::jsonschema::cli::identify(
return EXIT_FAILURE;
}

// TODO: We should have a `relative_from` function in the URI module
// TODO: We should have a `relative_to` function in the URI module
// instead
std::cout << result.substr(base.size()) << "\n";
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Global Options:
Pre-process a JSON Schema into JSON Toolkit's low-level JSON-based
compiled form for faster evaluation.
identify <schema.json> [--relative-from/-f <uri>]
identify <schema.json> [--relative-to/-t <uri>]
Print the URI of the given schema to standard output, optionally
relative to a given base URI.
Expand Down
2 changes: 1 addition & 1 deletion test/identify/fail_invalid_base_uri.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cat << 'EOF' > "$TMP/schema.json"
EOF

"$1" identify "$TMP/schema.json" \
--relative-from "111https://////" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
--relative-to "111https://////" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Expand Down
2 changes: 1 addition & 1 deletion test/identify/fail_resolve_from_equal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cat << 'EOF' > "$TMP/schema.json"
EOF

"$1" identify "$TMP/schema.json" \
--relative-from "https://foo.com" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
--relative-to "https://foo.com" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Expand Down
2 changes: 1 addition & 1 deletion test/identify/fail_resolve_from_no_match.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cat << 'EOF' > "$TMP/schema.json"
EOF

"$1" identify "$TMP/schema.json" \
--relative-from "https://bar.com" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
--relative-to "https://bar.com" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Expand Down
2 changes: 1 addition & 1 deletion test/identify/pass_with_resolve_from.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cat << 'EOF' > "$TMP/schema.json"
EOF

"$1" identify "$TMP/schema.json" \
--relative-from "https://example.com/foo" > "$TMP/result.txt" 2>&1
--relative-to "https://example.com/foo" > "$TMP/result.txt" 2>&1

cat << 'EOF' > "$TMP/expected.txt"
/bar/baz
Expand Down
2 changes: 1 addition & 1 deletion test/identify/pass_with_resolve_from_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ EOF

"$1" identify "$TMP/schema.json" \
--verbose \
--relative-from "https://example.com/foo" > "$TMP/result.txt" 2>&1
--relative-to "https://example.com/foo" > "$TMP/result.txt" 2>&1

cat << 'EOF' > "$TMP/expected.txt"
Resolving identifier against: https://example.com/foo
Expand Down

0 comments on commit 458b22d

Please sign in to comment.