-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add build-in method 'file.read' to load file (#1034)
* feat: add build-in method 'file.read' to load file Signed-off-by: zongz <[email protected]> * fix: fix unit test Signed-off-by: zongz <[email protected]> * fix: fix test case Signed-off-by: zongz <[email protected]> * fix: fix CR comments Signed-off-by: zongz <[email protected]> --------- Signed-off-by: zongz <[email protected]>
- Loading branch information
Showing
20 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::fs; | ||
|
||
use crate::*; | ||
|
||
#[no_mangle] | ||
#[runtime_fn] | ||
pub extern "C" fn kclvm_file_read( | ||
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(x) = args.arg_i_str(0, None) { | ||
let contents = | ||
fs::read_to_string(&x).expect(&format!("failed to access the file in {}", x)); | ||
|
||
let s = ValueRef::str(contents.as_ref()); | ||
return s.into_raw(ctx); | ||
} | ||
|
||
panic!("read() takes exactly one argument (0 given)"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1092,6 +1092,7 @@ mod tests { | |
"crypto", | ||
"base64", | ||
"units", | ||
"file", | ||
] | ||
.iter() | ||
.map(|name| KCLCompletionItem { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import json | ||
import file | ||
|
||
data_string = json.decode(file.read("test.json")) | ||
|
||
key1 = data_string.key1 | ||
key2 = data_string.key2 | ||
data = data_string.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
data_string: | ||
key1: value1 | ||
key2: value2 | ||
data: | ||
- 1 | ||
- 2 | ||
- 3 | ||
key1: value1 | ||
key2: value2 | ||
data: | ||
- 1 | ||
- 2 | ||
- 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"data": [1, 2, 3] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import file | ||
|
||
a = file.read("test.txt") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a: Helloworld |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Helloworld |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import yaml | ||
import file | ||
|
||
data_string = yaml.decode(file.read("test.yaml")) | ||
|
||
key1 = data_string.key1 | ||
key2 = data_string.key2 | ||
data = data_string.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
data_string: | ||
key1: value1 | ||
key2: value2 | ||
data: | ||
- 1 | ||
- 2 | ||
- 3 | ||
key1: value1 | ||
key2: value2 | ||
data: | ||
- 1 | ||
- 2 | ||
- 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
key1: "value1" | ||
key2: "value2" | ||
data: [1, 2, 3] |