Skip to content

Commit 27f16e9

Browse files
authored
Simplify JSON conversion for ValueNone and ValueRegex (#466)
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 00769a2 commit 27f16e9

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/compiler/compile_json.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22

33
#include <variant> // std::visit
44

5-
namespace sourcemeta::core {
6-
template <typename T>
7-
requires std::is_same_v<T, sourcemeta::blaze::ValueNone>
8-
auto to_json(const T &) -> JSON {
9-
return JSON{nullptr};
10-
}
11-
template <typename T>
12-
requires std::is_same_v<T, typename sourcemeta::blaze::ValueRegex::first_type>
13-
auto to_json(const T &) -> JSON {
14-
return JSON{nullptr};
15-
}
16-
} // namespace sourcemeta::core
17-
185
namespace sourcemeta::blaze {
196

207
auto to_json(const Instruction &instruction) -> sourcemeta::core::JSON {

src/evaluator/include/sourcemeta/blaze/evaluator_value.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ namespace sourcemeta::blaze {
1919

2020
/// @ingroup evaluator
2121
/// @brief Represents a compiler step empty value
22-
struct ValueNone {};
22+
struct ValueNone {
23+
auto to_json() const -> sourcemeta::core::JSON {
24+
return sourcemeta::core::JSON{nullptr};
25+
}
26+
};
2327

2428
/// @ingroup evaluator
2529
/// Represents a compiler step JSON value
@@ -62,7 +66,16 @@ using ValueType = sourcemeta::core::JSON::Type;
6266
/// original string and the regular expression as standard regular expressions
6367
/// do not keep a copy of their original value (which we need for serialization
6468
/// purposes)
65-
using ValueRegex = std::pair<sourcemeta::core::Regex<ValueString>, std::string>;
69+
struct ValueRegex {
70+
using second_type = ValueString;
71+
using first_type = sourcemeta::core::Regex<second_type>;
72+
const first_type first;
73+
const second_type second;
74+
75+
auto to_json() const -> sourcemeta::core::JSON {
76+
return sourcemeta::core::to_json(this->second);
77+
}
78+
};
6679

6780
/// @ingroup evaluator
6881
/// Represents a compiler step JSON unsigned integer value

test/compiler/compiler_json_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ TEST(Compiler_JSON, example_3) {
128128
"r": 0,
129129
"v": {
130130
"t": 9,
131-
"v": [ null, "^f" ]
131+
"v": "^f"
132132
},
133133
"c": []
134134
}

0 commit comments

Comments
 (0)