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

fix: the type of field in ExecProgramArgs has been changed to support serialization from json str #569

Merged
merged 5 commits into from
Jun 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
6 changes: 5 additions & 1 deletion kclvm/api/src/testdata/exec-program-with-external-pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"external_pkgs": [
{
"pkg_name": "external",
"pkg_path": "./src/testdata/external"
"pkg_path": "./src/testdata_external/external"
},
{
"pkg_name": "external_1",
"pkg_path": "./src/testdata_external/external_1"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"json_result": "[{\"a\": \"Hello External World!\"}]",
"yaml_result": "a: Hello External World!",
"json_result": "[{\"a\": \"Hello External World!\", \"a1\": \"Hello External_1 World!\"}]",
"yaml_result": "a: Hello External World!\na1: Hello External_1 World!",
"escaped_time": "0.002061128616333008"
}
5 changes: 4 additions & 1 deletion kclvm/api/src/testdata/hello_import.k
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import external as ext
a = ext.a
import external_1 as ext_1

a = ext.a
a1 = ext_1.a
6 changes: 6 additions & 0 deletions kclvm/api/src/testdata_external/external_1/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "external_1"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
1 change: 1 addition & 0 deletions kclvm/api/src/testdata_external/external_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 'Hello External_1 World!'
7 changes: 7 additions & 0 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ impl TryInto<Node<SchemaExpr>> for Node<Expr> {
/// AST node type T
pub type NodeRef<T> = Box<Node<T>>;

/// KCL command line argument spec, e.g. `kcl main.k -E pkg_name=pkg_path`
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct CmdExternalPkgSpec {
pub pkg_name: String,
pub pkg_path: String,
}

/// KCL command line argument spec, e.g. `kcl main.k -D name=value`
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct CmdArgSpec {
Expand Down
7 changes: 1 addition & 6 deletions kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,7 @@ impl Loader {
pathbuf.push(s);
}

let pkgpath: String = pathbuf.as_path().to_str().unwrap().to_string();
let abspath: String = std::path::Path::new(&pkgroot)
.join(pkgpath)
.to_str()
.unwrap()
.to_string();
let abspath: String = pathbuf.as_path().to_str().unwrap().to_string();

if std::path::Path::new(abspath.as_str()).exists() {
return self.get_dir_files(abspath.as_str());
Expand Down
31 changes: 27 additions & 4 deletions kclvm/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ pub struct ExecProgramArgs {
pub work_dir: Option<String>,
pub k_filename_list: Vec<String>,
// -E key=value
#[serde(skip)]
pub package_maps: HashMap<String, String>,
pub external_pkgs: Vec<ast::CmdExternalPkgSpec>,
pub k_code_list: Vec<String>,
// -D key=value
pub args: Vec<ast::CmdArgSpec>,
Expand Down Expand Up @@ -56,6 +55,28 @@ pub struct ExecProgramArgs {
pub plugin_agent: u64,
}

impl ExecProgramArgs {
/// [`get_package_maps_from_external_pkg`] gets the package name to package path mapping.
pub fn get_package_maps_from_external_pkg(&self) -> HashMap<String, String> {
let mut package_maps = HashMap::new();
for external_pkg in &self.external_pkgs {
package_maps.insert(external_pkg.pkg_name.clone(), external_pkg.pkg_path.clone());
}
package_maps
}

/// [`set_external_pkg_from_package_maps`] sets the package name to package path mapping.
pub fn set_external_pkg_from_package_maps(&mut self, package_maps: HashMap<String, String>) {
self.external_pkgs = package_maps
.iter()
.map(|(pkg_name, pkg_path)| ast::CmdExternalPkgSpec {
pkg_name: pkg_name.clone(),
pkg_path: pkg_path.clone(),
})
.collect();
}
}

/// ExecProgramResult denotes the running result of the KCL program.
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct ExecProgramResult {
Expand Down Expand Up @@ -85,7 +106,7 @@ impl ExecProgramArgs {
kclvm_parser::LoadProgramOptions {
work_dir: self.work_dir.clone().unwrap_or_default(),
vendor_dirs: vec![get_vendor_home()],
package_maps: self.package_maps.clone(),
package_maps: self.get_package_maps_from_external_pkg(),
k_code_list: self.k_code_list.clone(),
cmd_args: self.args.clone(),
cmd_overrides: self.overrides.clone(),
Expand Down Expand Up @@ -113,7 +134,9 @@ impl TryFrom<SettingsFile> for ExecProgramArgs {
args.overrides.push(parse_override_spec(override_str)?);
}
args.path_selector = cli_configs.path_selector.unwrap_or_default();
args.package_maps = cli_configs.package_maps.unwrap_or(HashMap::default())
args.set_external_pkg_from_package_maps(
cli_configs.package_maps.unwrap_or(HashMap::default()),
)
}
if let Some(options) = settings.kcl_options {
args.args = options
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runner/src/test_datas/exec_prog_args/default.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"work_dir":null,"k_filename_list":[],"k_code_list":[],"args":[],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false,"include_schema_type_path":false}
{"work_dir":null,"k_filename_list":[],"external_pkgs":[],"k_code_list":[],"args":[],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false,"include_schema_type_path":false}
2 changes: 1 addition & 1 deletion kclvm/runner/src/test_datas/settings_file/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"work_dir":null,"k_filename_list":["../main.k","./before/base.k","./main.k","./sub/sub.k"],"k_code_list":[],"args":[{"name":"app-name","value":"\"kclvm\""},{"name":"image","value":"\"kclvm:v0.0.1\""}],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false,"include_schema_type_path":false}
{"work_dir":null,"k_filename_list":["../main.k","./before/base.k","./main.k","./sub/sub.k"],"external_pkgs":[],"k_code_list":[],"args":[{"name":"app-name","value":"\"kclvm\""},{"name":"image","value":"\"kclvm:v0.0.1\""}],"overrides":[],"disable_yaml_result":false,"print_override_ast":false,"strict_range_check":false,"disable_none":false,"verbose":0,"debug":0,"sort_keys":false,"include_schema_type_path":false}