Skip to content

Commit

Permalink
fix dump to file arguments and add more test cases (#1082)
Browse files Browse the repository at this point in the history
* fix: filename argument for the dump to file func

Signed-off-by: peefy <xpf6677@163.com>

* fix: dump_to_file function arguments and add more test cases

Signed-off-by: peefy <xpf6677@163.com>

---------

Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Feb 24, 2024
1 parent f626d23 commit d0c32f3
Show file tree
Hide file tree
Showing 43 changed files with 221 additions and 24 deletions.
14 changes: 9 additions & 5 deletions kclvm/runtime/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,26 @@ pub extern "C" fn kclvm_json_validate(
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_json_dump_to_file(
_ctx: *mut kclvm_context_t,
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 kwargs = ptr_as_ref(kwargs);

if let Some(data) = args.arg_i(0) {
if let Some(filename) = args.arg_i(0) {
let data = args.arg_i(0).or(kwargs.get_by_key("data"));
let filename = args.arg_i(1).or(kwargs.get_by_key("filename"));
match (data, filename) {
(Some(data), Some(filename)) => {
let filename = filename.as_str();
let json = data.to_json_string_with_options(&kwargs_to_opts(kwargs));
std::fs::write(&filename, json)
.unwrap_or_else(|e| panic!("Unable to write file '{}': {}", filename, e));
kclvm_value_Undefined(ctx)
}
_ => {
panic!("dump_to_file() missing 2 required positional arguments: 'data' and 'filename'")
}
}
panic!("dump_to_file() missing 2 required positional arguments: 'data' and 'filename'")
}

fn kwargs_to_opts(kwargs: &ValueRef) -> JsonEncodeOptions {
Expand Down
29 changes: 20 additions & 9 deletions kclvm/runtime/src/yaml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,38 +94,44 @@ pub extern "C" fn kclvm_yaml_decode_all(
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_yaml_dump_to_file(
_ctx: *mut kclvm_context_t,
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 kwargs = ptr_as_ref(kwargs);

if let Some(data) = args.arg_i(0) {
if let Some(filename) = args.arg_i(0) {
let data = args.arg_i(0).or(kwargs.get_by_key("data"));
let filename = args.arg_i(1).or(kwargs.get_by_key("filename"));
match (data, filename) {
(Some(data), Some(filename)) => {
let filename = filename.as_str();

let yaml = data.to_yaml_string_with_options(&kwargs_to_opts(kwargs));
std::fs::write(&filename, yaml)
.unwrap_or_else(|e| panic!("Unable to write file '{}': {}", filename, e));
kclvm_value_Undefined(ctx)
}
_ => {
panic!("dump_to_file() missing 2 required positional arguments: 'data' and 'filename'")
}
}
panic!("dump_to_file() missing 2 required positional arguments: 'data' and 'filename'")
}

/// dump_all_to_file(data, sort_keys=False, ignore_private=False, ignore_none=False)
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_yaml_dump_all_to_file(
_ctx: *mut kclvm_context_t,
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 kwargs = ptr_as_ref(kwargs);

if let Some(data) = args.arg_i(0) {
if let Some(filename) = args.arg_i(0) {
let data = args.arg_i(0).or(kwargs.get_by_key("data"));
let filename = args.arg_i(1).or(kwargs.get_by_key("filename"));
match (data, filename) {
(Some(data), Some(filename)) => {
let filename = filename.as_str();
let opts = kwargs_to_opts(kwargs);
let results = data
Expand All @@ -136,9 +142,14 @@ pub extern "C" fn kclvm_yaml_dump_all_to_file(
.collect::<Vec<String>>();

std::fs::write(filename, results.join(YAML_STREAM_SEP)).expect("Unable to write file");
kclvm_value_Undefined(ctx)
}
_ => {
panic!(
"dump_all_to_file() missing 2 required positional arguments: 'data' and 'filename'"
)
}
}
panic!("dump_all_to_file() missing 2 required positional arguments: 'data' and 'filename'")
}

/// validate(value: str) -> bool
Expand Down
8 changes: 8 additions & 0 deletions out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Alice",
"age": 18,
"data": [
1,
2
]
}
5 changes: 5 additions & 0 deletions out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Alice
age: 18
data:
- 1
- 2
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_0/0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"key: value"
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_0/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"- 1\n- 2\n- 3"
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_0/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"1"
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_0/3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"1.1"
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_0/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"null"
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_0/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"true"
11 changes: 11 additions & 0 deletions test/grammar/builtins/json/dump_to_file_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import json

jsonStrList = [
'key: value',
'- 1\n- 2\n- 3',
'1',
'1.1',
'null',
'true',
]
_ = [json.dump_to_file(s, "${i}.json") for i, s in jsonStrList]
10 changes: 10 additions & 0 deletions test/grammar/builtins/json/dump_to_file_0/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
jsonStrList:
- 'key: value'
- |-
- 1
- 2
- 3
- '1'
- '1.1'
- 'null'
- 'true'
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_1/0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'key: value'
4 changes: 4 additions & 0 deletions test/grammar/builtins/json/dump_to_file_1/1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
|-
- 1
- 2
- 3
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_1/2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'1'
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_1/3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'1.1'
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_1/4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'null'
1 change: 1 addition & 0 deletions test/grammar/builtins/json/dump_to_file_1/5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'true'
11 changes: 11 additions & 0 deletions test/grammar/builtins/json/dump_to_file_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import yaml

yamlStrList = [
'key: value',
'- 1\n- 2\n- 3',
'1',
'1.1',
'null',
'true',
]
_ = [yaml.dump_to_file(s, filename="${i}.yaml") for i, s in yamlStrList]
10 changes: 10 additions & 0 deletions test/grammar/builtins/json/dump_to_file_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
yamlStrList:
- 'key: value'
- |-
- 1
- 2
- 3
- '1'
- '1.1'
- 'null'
- 'true'
7 changes: 3 additions & 4 deletions test/grammar/builtins/json/output_1/main.k
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ schema Person:
school?: str
data?: [int] = [1, 2, None]

_person = Person {
person = Person {
name: "Alice"
age: 18
}
_filename = "out.json"
print("JSON output is in {}".format(_filename))
json.dump_to_file(_person, _filename, indent=4, ignore_private=True, ignore_none=True)
filename = "out.json"
json.dump_to_file(person, filename, indent=4, ignore_private=True, ignore_none=True)
9 changes: 8 additions & 1 deletion test/grammar/builtins/json/output_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
JSON output is in out.json
person:
name: Alice
age: 18
data:
- 1
- 2
- null
filename: out.json
19 changes: 19 additions & 0 deletions test/grammar/builtins/yaml/dump_all_to_file_0/0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'key: value'

---
|-
- 1
- 2
- 3

---
'1'

---
'1.1'

---
'null'

---
'true'
11 changes: 11 additions & 0 deletions test/grammar/builtins/yaml/dump_all_to_file_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import yaml

yamlStrList = [
'key: value',
'- 1\n- 2\n- 3',
'1',
'1.1',
'null',
'true',
]
yaml.dump_all_to_file(yamlStrList, "0.yaml")
10 changes: 10 additions & 0 deletions test/grammar/builtins/yaml/dump_all_to_file_0/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
yamlStrList:
- 'key: value'
- |-
- 1
- 2
- 3
- '1'
- '1.1'
- 'null'
- 'true'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'key: value'
4 changes: 4 additions & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
|-
- 1
- 2
- 3
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'1'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'1.1'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'null'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'true'
11 changes: 11 additions & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import yaml

yamlStrList = [
'key: value',
'- 1\n- 2\n- 3',
'1',
'1.1',
'null',
'true',
]
_ = [yaml.dump_to_file(s, "${i}.yaml") for i, s in yamlStrList]
10 changes: 10 additions & 0 deletions test/grammar/builtins/yaml/dump_to_file_0/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
yamlStrList:
- 'key: value'
- |-
- 1
- 2
- 3
- '1'
- '1.1'
- 'null'
- 'true'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'key: value'
4 changes: 4 additions & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
|-
- 1
- 2
- 3
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'1'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'1.1'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'null'
1 change: 1 addition & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'true'
11 changes: 11 additions & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import yaml

yamlStrList = [
'key: value',
'- 1\n- 2\n- 3',
'1',
'1.1',
'null',
'true',
]
_ = [yaml.dump_to_file(s, filename="${i}.yaml") for i, s in yamlStrList]
10 changes: 10 additions & 0 deletions test/grammar/builtins/yaml/dump_to_file_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
yamlStrList:
- 'key: value'
- |-
- 1
- 2
- 3
- '1'
- '1.1'
- 'null'
- 'true'
7 changes: 3 additions & 4 deletions test/grammar/builtins/yaml/output_1/main.k
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ schema Person:
school?: str
data?: [int] = [1, 2, None]

_person = Person {
person = Person {
name: "Alice"
age: 18
}
_filename = "out.yaml"
print("YAML output is in {}".format(_filename))
yaml.dump_to_file(_person, _filename, ignore_private=True, ignore_none=True)
filename = "out.yaml"
yaml.dump_to_file(person, filename, ignore_private=True, ignore_none=True)
9 changes: 8 additions & 1 deletion test/grammar/builtins/yaml/output_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
YAML output is in out.yaml
person:
name: Alice
age: 18
data:
- 1
- 2
- null
filename: out.yaml

0 comments on commit d0c32f3

Please sign in to comment.