Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validate functions for json and yaml modules #856

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified kclvm/runtime/src/_kclvm.bc
Binary file not shown.
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ kclvm_value_ref_t* kclvm_json_dump_to_file(kclvm_context_t* _ctx, kclvm_value_re

kclvm_value_ref_t* kclvm_json_encode(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_json_validate(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);

void kclvm_list_append(kclvm_value_ref_t* p, kclvm_value_ref_t* v);

void kclvm_list_append_bool(kclvm_value_ref_t* p, kclvm_bool_t v);
Expand Down Expand Up @@ -662,6 +664,8 @@ kclvm_value_ref_t* kclvm_yaml_encode(kclvm_context_t* ctx, kclvm_value_ref_t* ar

kclvm_value_ref_t* kclvm_yaml_encode_all(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_yaml_validate(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm.ll
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ declare %kclvm_value_ref_t* @kclvm_json_dump_to_file(%kclvm_context_t* %_ctx, %k

declare %kclvm_value_ref_t* @kclvm_json_encode(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_json_validate(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

declare void @kclvm_list_append(%kclvm_value_ref_t* %p, %kclvm_value_ref_t* %v);

declare void @kclvm_list_append_bool(%kclvm_value_ref_t* %p, %kclvm_bool_t %v);
Expand Down Expand Up @@ -610,6 +612,8 @@ declare %kclvm_value_ref_t* @kclvm_yaml_encode(%kclvm_context_t* %ctx, %kclvm_va

declare %kclvm_value_ref_t* @kclvm_yaml_encode_all(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_yaml_validate(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

define void @__kcl_keep_link_runtime(%kclvm_value_ref_t* %_a, %kclvm_context_t* %_b) {
call %kclvm_value_ref_t* @kclvm_value_None(%kclvm_context_t* %_b)
ret void
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub enum ApiFunc {
kclvm_json_decode,
kclvm_json_dump_to_file,
kclvm_json_encode,
kclvm_json_validate,
kclvm_list_append,
kclvm_list_append_bool,
kclvm_list_append_float,
Expand Down Expand Up @@ -317,6 +318,7 @@ pub enum ApiFunc {
kclvm_yaml_dump_to_file,
kclvm_yaml_encode,
kclvm_yaml_encode_all,
kclvm_yaml_validate,
}

impl std::fmt::Display for ApiFunc {
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_json_decode" => crate::kclvm_json_decode as *const () as u64,
"kclvm_json_dump_to_file" => crate::kclvm_json_dump_to_file as *const () as u64,
"kclvm_json_encode" => crate::kclvm_json_encode as *const () as u64,
"kclvm_json_validate" => crate::kclvm_json_validate as *const () as u64,
"kclvm_list_append" => crate::kclvm_list_append as *const () as u64,
"kclvm_list_append_bool" => crate::kclvm_list_append_bool as *const () as u64,
"kclvm_list_append_float" => crate::kclvm_list_append_float as *const () as u64,
Expand Down Expand Up @@ -346,6 +347,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_yaml_dump_to_file" => crate::kclvm_yaml_dump_to_file as *const () as u64,
"kclvm_yaml_encode" => crate::kclvm_yaml_encode as *const () as u64,
"kclvm_yaml_encode_all" => crate::kclvm_yaml_encode_all as *const () as u64,
"kclvm_yaml_validate" => crate::kclvm_yaml_validate as *const () as u64,
_ => panic!("unknown {name}"),
}
}
8 changes: 8 additions & 0 deletions kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,10 @@
// api-spec(c): kclvm_value_ref_t* kclvm_json_decode(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_json_decode(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

// api-spec: kclvm_json_validate
// api-spec(c): kclvm_value_ref_t* kclvm_json_validate(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_json_validate(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

// api-spec: kclvm_json_dump_to_file
// api-spec(c): kclvm_value_ref_t* kclvm_json_dump_to_file(kclvm_context_t* _ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_json_dump_to_file(%kclvm_context_t* %_ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);
Expand Down Expand Up @@ -1166,3 +1170,7 @@
// api-spec(c): kclvm_value_ref_t* kclvm_yaml_dump_all_to_file(kclvm_context_t* _ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_yaml_dump_all_to_file(%kclvm_context_t* %_ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

// api-spec: kclvm_yaml_validate
// api-spec(c): kclvm_value_ref_t* kclvm_yaml_validate(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_yaml_validate(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

19 changes: 19 additions & 0 deletions kclvm/runtime/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ pub extern "C" fn kclvm_json_decode(
panic!("decode() missing 1 required positional argument: 'value'")
}

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_json_validate(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let ctx = mut_ptr_as_ref(ctx);

if let Some(arg0) = args.arg_i(0) {
match ValueRef::from_json(ctx, arg0.as_str().as_ref()) {
Ok(_) => return kclvm_value_True(ctx),
Err(_) => return kclvm_value_False(ctx),
}
}
panic!("validate() missing 1 required positional argument: 'value'")
}

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_json_dump_to_file(
Expand Down
20 changes: 20 additions & 0 deletions kclvm/runtime/src/yaml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ pub extern "C" fn kclvm_yaml_dump_all_to_file(
panic!("dump_all_to_file() missing 2 required positional arguments: 'data' and 'filename'")
}

/// validate(value: str) -> bool
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_yaml_validate(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

let ctx = mut_ptr_as_ref(ctx);
if let Some(arg0) = args.arg_i(0) {
match ValueRef::from_yaml_stream(ctx, arg0.as_str().as_ref()) {
Ok(_) => return kclvm_value_True(ctx),
Err(_) => return kclvm_value_False(ctx),
}
}
panic!("validate() missing 1 required positional argument: 'value'")
}

fn kwargs_to_opts(kwargs: &ValueRef) -> YamlEncodeOptions {
let mut opts = YamlEncodeOptions::default();
if let Some(sort_keys) = kwargs.kwarg_bool("sort_keys", None) {
Expand Down
31 changes: 30 additions & 1 deletion kclvm/sema/src/builtin/system_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ pub const YAML_FUNCTION_NAMES: &[&str] = &[
"decode_all",
"dump_to_file",
"dump_all_to_file",
"validate",
];
macro_rules! register_yaml_member {
($($name:ident => $ty:expr)*) => (
Expand Down Expand Up @@ -1014,14 +1015,28 @@ register_yaml_member! {
false,
Some(2),
)
validate => Type::function(
None,
Type::bool_ref(),
&[
Parameter {
name: "value".to_string(),
ty: Type::str_ref(),
has_default: false,
},
],
r#"Validate whether the given string is a valid YAML or YAML stream document."#,
false,
None,
)
}

// ------------------------------
// json system package
// ------------------------------

pub const JSON: &str = "json";
pub const JSON_FUNCTION_NAMES: &[&str] = &["encode", "decode", "dump_to_file"];
pub const JSON_FUNCTION_NAMES: &[&str] = &["encode", "decode", "dump_to_file", "validate"];
macro_rules! register_json_member {
($($name:ident => $ty:expr)*) => (
pub const JSON_FUNCTION_TYPES: Lazy<IndexMap<String, Type>> = Lazy::new(|| {
Expand Down Expand Up @@ -1119,6 +1134,20 @@ register_json_member! {
false,
Some(2),
)
validate => Type::function(
None,
Type::bool_ref(),
&[
Parameter {
name: "value".to_string(),
ty: Type::str_ref(),
has_default: false,
},
],
r#"Validate whether the given string is a valid JSON"#,
false,
None,
)
}

// ------------------------------
Expand Down
19 changes: 19 additions & 0 deletions test/grammar/builtins/json/validate/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json

# Right cases

resultRight1: bool = json.validate("1")
resultRight2: bool = json.validate("true")
resultRight3: bool = json.validate("1.20")
resultRight4: bool = json.validate("null")
resultRight5: bool = json.validate("[0, 1, 2]")
resultRight6: bool = json.validate('{"key": "value"}')

# Wrong cases

resultWrong1: bool = json.validate("1@")
resultWrong2: bool = json.validate("True")
resultWrong3: bool = json.validate("1.20.23+1")
resultWrong4: bool = json.validate("None")
resultWrong5: bool = json.validate("[0, 1, 2,]")
resultWrong6: bool = json.validate(r'''{"key": 'value'}''')
12 changes: 12 additions & 0 deletions test/grammar/builtins/json/validate/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resultRight1: true
resultRight2: true
resultRight3: true
resultRight4: true
resultRight5: true
resultRight6: true
resultWrong1: false
resultWrong2: false
resultWrong3: false
resultWrong4: false
resultWrong5: false
resultWrong6: false
21 changes: 21 additions & 0 deletions test/grammar/builtins/yaml/validate/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import yaml

# Right cases

resultRight1: bool = yaml.validate("1")
resultRight2: bool = yaml.validate("true")
resultRight3: bool = yaml.validate("1.20")
resultRight4: bool = yaml.validate("null")
resultRight5: bool = yaml.validate("[0, 1, 2]")
resultRight6: bool = yaml.validate('{"key": "value"}')
resultRight7: bool = yaml.validate('a:1\n---\nb:2')

# Wrong cases

resultWrong1: bool = yaml.validate("a:\n1")
resultWrong2: bool = yaml.validate("a:\n1\n - 2")
resultWrong3: bool = yaml.validate("a:\n-1")
resultWrong4: bool = yaml.validate("1a : \n1")
resultWrong5: bool = yaml.validate("a:\n- 1\n-----\na:\n- 1")
resultWrong6: bool = yaml.validate(r'''{"key" + 'value'}''')
resultWrong7: bool = yaml.validate("a:1\n-----\nb:\n-2")
14 changes: 14 additions & 0 deletions test/grammar/builtins/yaml/validate/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resultRight1: true
resultRight2: true
resultRight3: true
resultRight4: true
resultRight5: true
resultRight6: true
resultRight7: true
resultWrong1: false
resultWrong2: false
resultWrong3: false
resultWrong4: false
resultWrong5: false
resultWrong6: false
resultWrong7: false
Loading