Skip to content

Commit

Permalink
refactor: runtime ty check and add more test cases. (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Jun 5, 2023
1 parent 3d67bc4 commit a83255f
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 6 deletions.
5 changes: 4 additions & 1 deletion kclvm/runtime/src/api/kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,13 @@ pub struct ContextConfig {
pub debug_mode: bool,

pub strict_range_check: bool,
pub disable_none: bool,
pub disable_schema_check: bool,

pub list_option_mode: bool,
// Whether to emit none value in the plan process.
pub disable_none: bool,
// Whether to output empty list in the plan process.
pub plan_empty_list: bool,
}

#[derive(PartialEq, Eq, Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/stdlib/builtin_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub unsafe extern "C" fn kclvm_builtin_option(
return ValueRef::none();
}

panic!("unknonwn type '{typ}'");
panic!("unknown type '{typ}'");
}

if let Some(arg0) = args.arg_0() {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/value/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2394,7 +2394,7 @@ pub unsafe extern "C" fn kclvm_convert_collection_value(
) -> *const kclvm_value_ref_t {
let value = ptr_as_ref(value);
let tpe = c2str(tpe);
let value = convert_collection_value(value, tpe);
let value = type_pack_and_check(value, vec![tpe]);
value.into_raw()
}

Expand Down
5 changes: 5 additions & 0 deletions kclvm/runtime/src/value/val_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ fn filter_results(key_values: &ValueRef) -> Vec<ValueRef> {
}
let schema_in_list_count = ignore_schema_count + standalone_list.len();
let value = &value.as_list_ref().values;
// Plan empty list to values.
if value.is_empty() && ctx.cfg.plan_empty_list {
let result = results.get_mut(0).unwrap();
result.dict_update_key_value(key.as_str(), ValueRef::list(None));
}
if schema_in_list_count < value.len() {
let result = results.get_mut(0).unwrap();
let filtered_list: Vec<&ValueRef> = filtered_list.iter().collect();
Expand Down
4 changes: 2 additions & 2 deletions kclvm/version/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021 The KCL Authors. All rights reserved.

pub const VERSION: &str = "0.5.0-alpha.1";
pub const CHECK_SUM: &str = "eb2f4d1aabc2a287c298bc35c5fcfec1";
pub const VERSION: &str = "0.5.0-alpha.2";
pub const CHECK_SUM: &str = "51289ff122a3ea8eea7f8149b8956cd1";

/// Get kCL full version string with the format `{version}-{check_sum}`.
#[inline]
Expand Down
7 changes: 7 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json

schema Person:
name: str
age: int

person: Person = json.decode('{"name": "Alice", "age": 18}')
3 changes: 3 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_0/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
person:
name: Alice
age: 18
7 changes: 7 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json

schema Person:
name: str
age: int

persons: [Person] = json.decode('[{"name": "Alice", "age": 18}, {"name": "Bob", "age": 10}]')
5 changes: 5 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
persons:
- name: Alice
age: 18
- name: Bob
age: 10
7 changes: 7 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_err_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json

schema Person:
name: str
age: int

person: Person = json.decode('{"err_name": "Alice", "age": 18}')
19 changes: 19 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_err_0/stderr.golden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys
import kclvm.kcl.error as kcl_error
import os

cwd = os.path.dirname(os.path.realpath(__file__))

kcl_error.print_kcl_error_message(
kcl_error.get_exception(
err_type=kcl_error.ErrType.CompileError_TYPE,
file_msgs=[
kcl_error.ErrFileMsg(
filename=cwd + "/main.k",
line_no=5,
)
],
arg_msg="err_name: No such member in the schema 'Person'"
),
file=sys.stdout
)
7 changes: 7 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_err_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json

schema Person:
name: str
age: int

persons: [Person] = json.decode('[{"name": "Alice", "age": 18}, {"err_name": "Bob", "age": 10}]')
19 changes: 19 additions & 0 deletions test/grammar/types/runtime_ty/runtime_ty_err_1/stderr.golden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys
import kclvm.kcl.error as kcl_error
import os

cwd = os.path.dirname(os.path.realpath(__file__))

kcl_error.print_kcl_error_message(
kcl_error.get_exception(
err_type=kcl_error.ErrType.CompileError_TYPE,
file_msgs=[
kcl_error.ErrFileMsg(
filename=cwd + "/main.k",
line_no=5,
)
],
arg_msg="err_name: No such member in the schema 'Person'"
),
file=sys.stdout
)

0 comments on commit a83255f

Please sign in to comment.