Skip to content

Commit

Permalink
refactor: make json_value methods shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
esynr3z committed Jun 5, 2024
1 parent a97096a commit c6af3d7
Show file tree
Hide file tree
Showing 17 changed files with 234 additions and 234 deletions.
4 changes: 2 additions & 2 deletions docs/modules/ROOT/examples/decoder0.svh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ string recipe_name;
// or `json_value`. First unwrap is required to get `json_value` from
// load result and avoid error handling. Second unwrap is required
// to avoid error handling of possible unsuccessfull cast to `json_object`.
jobject = json_decoder::load_string(data).unwrap().as_json_object().unwrap();
jobject = json_decoder::load_string(data).unwrap().as_object().unwrap();

// Try to get a string for recipe name.
// Unwrap is here to avoid error handling of possible unsuccessfull
// cast to `json_string`.
recipe_name = jobject.get("recipeName").as_json_string().unwrap().get();
recipe_name = jobject.get("recipeName").as_string().unwrap().get();

$display("Recipe name is %s", recipe_name);
2 changes: 1 addition & 1 deletion docs/modules/ROOT/examples/decoder1.svh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ case (1)

load_res.matches_ok(jvalue): begin
json_object jobject;
json_result#(json_object) cast_res = jvalue.as_json_object();
json_result#(json_object) cast_res = jvalue.as_object();

// Traditional if..else can be used as well
if (cast_res.matches_err(jerror)) begin
Expand Down
6 changes: 3 additions & 3 deletions docs/modules/ROOT/pages/user.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ Class `{class-json-value}` is a base abstract class for all JSON values. This cl
|===
| `bit compare(json_value value)` | Perform compare with another instance. Return 1 if instances are equal and 0 otherwise.
| `json_value clone()` | Create a deep copy of an instance.
| `bit is_json_<val>` (e.g. `is_json_object`, `is_json_array`, etc.) | Check if current instance is specified type.
| `json_result#(json_<val>) as_json_<val>()` (e.g. `as_json_object`, `as_json_array`, etc.) | Try to cast to specified concrete class.
| `bit matches_json_<val>(output json_<val> value)` (e.g. `matches_json_object`, `matches_json_array`, etc.) | Another option of trying to cast to specified concrete class. In this case, instance is an `output` argument, and returned result is 1 for success, 0 otherwise.
| `bit is_<val>` (e.g. `is_object`, `is_array`, etc.) | Check if current instance is specified type.
| `json_result#(json_<val>) as_<val>()` (e.g. `as_object`, `as_array`, etc.) | Try to cast to specified concrete class.
| `bit matches_<val>(output json_<val> value)` (e.g. `matches_object`, `matches_array`, etc.) | Another option of trying to cast to specified concrete class. In this case, instance is an `output` argument, and returned result is 1 for success, 0 otherwise.
|===

=== Object
Expand Down
2 changes: 1 addition & 1 deletion src/json_decoder.sv
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function json_decoder::parser_result json_decoder::parse_object(
result.matches_err(error): return result;

result.matches_ok(parsed): begin
key = parsed.value.as_json_string().unwrap().value;
key = parsed.value.as_string().unwrap().value;
curr_pos = parsed.end_pos + 1; // move from last string token
state = EXPECT_COLON;
end
Expand Down
2 changes: 1 addition & 1 deletion src/values/json_array.sv
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function bit json_array::compare(json_value value);
return 0;
end

casted = value.as_json_array();
casted = value.as_array();
case (1)
casted.matches_err(err): return 0;
casted.matches_ok(rhs): begin
Expand Down
2 changes: 1 addition & 1 deletion src/values/json_bool.sv
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function bit json_bool::compare(json_value value);
return 0;
end

casted = value.as_json_bool();
casted = value.as_bool();
case (1)
casted.matches_err(err): return 0;
casted.matches_ok(rhs): return get() == rhs.get();
Expand Down
2 changes: 1 addition & 1 deletion src/values/json_int.sv
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function bit json_int::compare(json_value value);
return 0;
end

casted = value.as_json_int();
casted = value.as_int();
case (1)
casted.matches_err(err): return 0;
casted.matches_ok(rhs): return get() == rhs.get();
Expand Down
2 changes: 1 addition & 1 deletion src/values/json_object.sv
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function bit json_object::compare(json_value value);
return 0;
end

casted = value.as_json_object();
casted = value.as_object();
case (1)
casted.matches_err(err): return 0;
casted.matches_ok(rhs): begin
Expand Down
2 changes: 1 addition & 1 deletion src/values/json_real.sv
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function bit json_real::compare(json_value value);
return 0;
end

casted = value.as_json_real();
casted = value.as_real();
case (1)
casted.matches_err(err): return 0;
casted.matches_ok(rhs): return get() == rhs.get();
Expand Down
2 changes: 1 addition & 1 deletion src/values/json_string.sv
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function bit json_string::compare(json_value value);
return 0;
end

casted = value.as_json_string();
casted = value.as_string();
case (1)
casted.matches_err(err): return 0;
casted.matches_ok(rhs): return get() == rhs.get();
Expand Down
132 changes: 66 additions & 66 deletions src/values/json_value.sv
Original file line number Diff line number Diff line change
Expand Up @@ -7,182 +7,182 @@ virtual class json_value implements json_value_encodable;
pure virtual function bit compare(json_value value);

// Try to cast current value to `json_object`
extern virtual function bit matches_json_object(output json_object value);
extern virtual function bit matches_object(output json_object value);

// Try to cast current value to `json_array`
extern virtual function bit matches_json_array(output json_array value);
extern virtual function bit matches_array(output json_array value);

// Try to cast current value to `json_string`
extern virtual function bit matches_json_string(output json_string value);
extern virtual function bit matches_string(output json_string value);

// Try to cast current value to `json_int`
extern virtual function bit matches_json_int(output json_int value);
extern virtual function bit matches_int(output json_int value);

// Try to cast current value to `json_real`
extern virtual function bit matches_json_real(output json_real value);
extern virtual function bit matches_real(output json_real value);

// Try to cast current value to `json_bool`
extern virtual function bit matches_json_bool(output json_bool value);
extern virtual function bit matches_bool(output json_bool value);

// Try to represent current value as `json_object`
extern virtual function json_result#(json_object) as_json_object();
extern virtual function json_result#(json_object) as_object();

// Try to represent current value as `json_array`
extern virtual function json_result#(json_array) as_json_array();
extern virtual function json_result#(json_array) as_array();

// Try to represent current value as `json_string`
extern virtual function json_result#(json_string) as_json_string();
extern virtual function json_result#(json_string) as_string();

// Try to represent current value as `json_int`
extern virtual function json_result#(json_int) as_json_int();
extern virtual function json_result#(json_int) as_int();

// Try to represent current value as `json_real`
extern virtual function json_result#(json_real) as_json_real();
extern virtual function json_result#(json_real) as_real();

// Try to represent current value as `json_bool`
extern virtual function json_result#(json_bool) as_json_bool();
extern virtual function json_result#(json_bool) as_bool();

// Check if current value is `json_object`
extern virtual function bit is_json_object();
extern virtual function bit is_object();

// Check if current value is `json_array`
extern virtual function bit is_json_array();
extern virtual function bit is_array();

// Check if current value is `json_string`
extern virtual function bit is_json_string();
extern virtual function bit is_string();

// Check if current value is `json_int`
extern virtual function bit is_json_int();
extern virtual function bit is_int();

// Check if current value is `json_real`
extern virtual function bit is_json_real();
extern virtual function bit is_real();

// Check if current value is `json_bool`
extern virtual function bit is_json_bool();
extern virtual function bit is_bool();
endclass : json_value


function bit json_value::matches_json_object(output json_object value);
function bit json_value::matches_object(output json_object value);
return $cast(value, this);
endfunction : matches_json_object
endfunction : matches_object


function bit json_value::matches_json_array(output json_array value);
function bit json_value::matches_array(output json_array value);
return $cast(value, this);
endfunction : matches_json_array
endfunction : matches_array


function bit json_value::matches_json_string(output json_string value);
function bit json_value::matches_string(output json_string value);
return $cast(value, this);
endfunction : matches_json_string
endfunction : matches_string


function bit json_value::matches_json_int(output json_int value);
function bit json_value::matches_int(output json_int value);
return $cast(value, this);
endfunction : matches_json_int
endfunction : matches_int


function bit json_value::matches_json_real(output json_real value);
function bit json_value::matches_real(output json_real value);
return $cast(value, this);
endfunction : matches_json_real
endfunction : matches_real


function bit json_value::matches_json_bool(output json_bool value);
function bit json_value::matches_bool(output json_bool value);
return $cast(value, this);
endfunction : matches_json_bool
endfunction : matches_bool


function json_result#(json_object) json_value::as_json_object();
function json_result#(json_object) json_value::as_object();
json_object value;
if (this.matches_json_object(value)) begin
if (this.matches_object(value)) begin
return json_result#(json_object)::ok(value);
end else begin
return json_result#(json_object)::err(json_error::create(json_error::TYPE_CONVERSION));
end
endfunction : as_json_object
endfunction : as_object


function json_result#(json_array) json_value::as_json_array();
function json_result#(json_array) json_value::as_array();
json_array value;
if (this.matches_json_array(value)) begin
if (this.matches_array(value)) begin
return json_result#(json_array)::ok(value);
end else begin
return json_result#(json_array)::err(json_error::create(json_error::TYPE_CONVERSION));
end
endfunction : as_json_array
endfunction : as_array


function json_result#(json_string) json_value::as_json_string();
function json_result#(json_string) json_value::as_string();
json_string value;
if (this.matches_json_string(value)) begin
if (this.matches_string(value)) begin
return json_result#(json_string)::ok(value);
end else begin
return json_result#(json_string)::err(json_error::create(json_error::TYPE_CONVERSION));
end
endfunction : as_json_string
endfunction : as_string


function json_result#(json_int) json_value::as_json_int();
function json_result#(json_int) json_value::as_int();
json_int value;
if (this.matches_json_int(value)) begin
if (this.matches_int(value)) begin
return json_result#(json_int)::ok(value);
end else begin
return json_result#(json_int)::err(json_error::create(json_error::TYPE_CONVERSION));
end
endfunction : as_json_int
endfunction : as_int


function json_result#(json_real) json_value::as_json_real();
function json_result#(json_real) json_value::as_real();
json_real value;
if (this.matches_json_real(value)) begin
if (this.matches_real(value)) begin
return json_result#(json_real)::ok(value);
end else begin
return json_result#(json_real)::err(json_error::create(json_error::TYPE_CONVERSION));
end
endfunction : as_json_real
endfunction : as_real


function json_result#(json_bool) json_value::as_json_bool();
function json_result#(json_bool) json_value::as_bool();
json_bool value;
if (this.matches_json_bool(value)) begin
if (this.matches_bool(value)) begin
return json_result#(json_bool)::ok(value);
end else begin
return json_result#(json_bool)::err(json_error::create(json_error::TYPE_CONVERSION));
end
endfunction : as_json_bool
endfunction : as_bool


function bit json_value::is_json_object();
function bit json_value::is_object();
json_object value;
return this.matches_json_object(value);
endfunction : is_json_object
return this.matches_object(value);
endfunction : is_object


function bit json_value::is_json_array();
function bit json_value::is_array();
json_array value;
return this.matches_json_array(value);
endfunction : is_json_array
return this.matches_array(value);
endfunction : is_array


function bit json_value::is_json_string();
function bit json_value::is_string();
json_string value;
return this.matches_json_string(value);
endfunction : is_json_string
return this.matches_string(value);
endfunction : is_string


function bit json_value::is_json_int();
function bit json_value::is_int();
json_int value;
return this.matches_json_int(value);
endfunction : is_json_int
return this.matches_int(value);
endfunction : is_int


function bit json_value::is_json_real();
function bit json_value::is_real();
json_real value;
return this.matches_json_real(value);
endfunction : is_json_real
return this.matches_real(value);
endfunction : is_real


function bit json_value::is_json_bool();
function bit json_value::is_bool();
json_bool value;
return this.matches_json_bool(value);
endfunction : is_json_bool
return this.matches_bool(value);
endfunction : is_bool
Loading

0 comments on commit c6af3d7

Please sign in to comment.