Skip to content

Commit

Permalink
fix: evaluator schema config resolve with parent schemas (#1490)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Jul 12, 2024
1 parent 1057311 commit e765674
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test-centos7-amd64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: kcl-centos7-amd64
path: _build/dist/centos/kclvm
path: _build/kclvm-centos-latest.tar.gz
2 changes: 1 addition & 1 deletion .github/workflows/build-test-ubuntu-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: kcl-linux-arm64
path: _build/dist/ubuntu/kclvm
path: _build/kclvm-ubuntu-latest.tar.gz
6 changes: 5 additions & 1 deletion .github/workflows/macos_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./kclvm/.kclvm_cov/lcov.info

- name: Release
run: export PATH=$PATH:$PWD/../_build/dist/Darwin/kclvm/bin:/usr/local/opt/llvm@12/bin && make release
shell: bash

- uses: actions/upload-artifact@v3
with:
name: kcl-darwin-amd64
path: _build/dist/Darwin/kclvm
path: _build/kclvm-Darwin-latest.tar.gz
24 changes: 18 additions & 6 deletions kclvm/evaluator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,15 @@ pub(crate) fn schema_body(
init_lazy_scope(s, &mut ctx.borrow_mut());
// Schema self value or parent schema value;
let mut schema_ctx_value = if let Some(parent_name) = &ctx.borrow().node.parent_name {
let base_constructor_func = s
.walk_identifier_with_ctx(&parent_name.node, &ast::ExprContext::Load, None)
.expect(kcl_error::RUNTIME_ERROR_MSG);
let base_constructor_func = s.load_global_value(
&parent_name.node.pkgpath,
&parent_name
.node
.names
.iter()
.map(|n| n.node.as_str())
.collect::<Vec<&str>>(),
);
// Call base schema function
call_schema_body(s, &base_constructor_func, args, kwargs, ctx)
} else {
Expand Down Expand Up @@ -442,9 +448,15 @@ pub(crate) fn schema_body(
{
let ctx_ref = ctx.borrow();
for mixin in &ctx_ref.node.mixins {
let mixin_func = s
.walk_identifier_with_ctx(&mixin.node, &ast::ExprContext::Load, None)
.expect(kcl_error::RUNTIME_ERROR_MSG);
let mixin_func = s.load_global_value(
&mixin.node.pkgpath,
&mixin
.node
.names
.iter()
.map(|n| n.node.as_str())
.collect::<Vec<&str>>(),
);
schema_ctx_value = call_schema_body(s, &mixin_func, args, kwargs, ctx);
}
}
Expand Down
26 changes: 26 additions & 0 deletions kclvm/evaluator/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,30 @@ impl<'ctx> Evaluator<'ctx> {
value
}
}

/// Load global value from name.
pub fn load_global_value(&self, pkgpath: &str, names: &[&str]) -> ValueRef {
if names.is_empty() {
return self.undefined_value();
}
let name = names[0];
if names.len() == 1 {
self.get_variable(name)
} else {
let mut value = if pkgpath.is_empty() {
self.get_variable(name)
} else {
self.undefined_value()
};
for i in 0..names.len() - 1 {
let attr = names[i + 1];
if i == 0 && !pkgpath.is_empty() {
value = self.get_variable_in_pkgpath(attr, pkgpath);
} else {
value = value.load_attr(attr)
}
}
value
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: evaluator/src/tests.rs
expression: "format!(\"{}\", evaluator.run().unwrap().1)"
---
VALUES_MAP:
'1':
attr1: foobar
'2':
attr2: bar
config:
provider: '1'
values:
attr1: foobar
provider_values:
attr1: foobar
27 changes: 27 additions & 0 deletions kclvm/evaluator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,34 @@ bob: Person {
age: 18
}
"#}
evaluator_snapshot! {schema_2, r#"
VALUES_MAP = {
"1": Values1{
attr1 = "foo"
}
"2": Values2 {
attr2 = "bar"
}
}
schema Config:
provider: "1" | "2"
values = VALUES_MAP[provider]
provider_values: Values1 | Values2 = values
schema CommonValues:
schema Values1(CommonValues):
attr1: str
schema Values2(CommonValues):
attr2: str
config: Config {
provider = "1"
provider_values.attr1 = "foobar"
}
"#}
evaluator_snapshot! {lazy_scope_0, r#"
b = a + c
a = 1
Expand Down

0 comments on commit e765674

Please sign in to comment.