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: enhance full schema type path in the exec result #1043

Merged
merged 1 commit into from
Feb 18, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"json_result": "[{\"alice\": {\"age\": 18, \"_type\": \"Person\"}}]",
"yaml_result": "alice:\n age: 18\n _type: Person"
"json_result": "[{\"alice\": {\"age\": 18, \"_type\": \"__main__.Person\"}}]",
"yaml_result": "alice:\n age: 18\n _type: __main__.Person"
}
29 changes: 21 additions & 8 deletions kclvm/runtime/src/value/val_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn handle_schema(ctx: &Context, value: &ValueRef, opts: &PlanOptions) -> (Vec<Va
if v.is_config() {
v.dict_update_key_value(
SCHEMA_TYPE_META_ATTR,
ValueRef::str(&value_type_path(value)),
ValueRef::str(&value_type_path(value, true)),
);
}
}
Expand Down Expand Up @@ -190,23 +190,36 @@ fn handle_schema(ctx: &Context, value: &ValueRef, opts: &PlanOptions) -> (Vec<Va
}

/// Returns the type path of the runtime value `v`.
fn value_type_path(v: &ValueRef) -> String {
fn value_type_path(v: &ValueRef, full_name: bool) -> String {
let path = format!("{SCHEMA_SETTINGS_ATTR_NAME}.{SETTINGS_SCHEMA_TYPE_KEY}");
match v.get_by_path(&path) {
Some(type_path) => match &*type_path.rc.borrow() {
Value::str_value(ty_str) => {
let parts: Vec<&str> = ty_str.rsplit('.').collect();
match parts.first() {
Some(v) => v.to_string(),
None => v.type_str(),
if full_name {
match ty_str.strip_prefix("@") {
Some(ty_str) => ty_str.to_string(),
None => ty_str.to_string(),
}
} else {
let parts: Vec<&str> = ty_str.rsplit('.').collect();
match parts.first() {
Some(v) => v.to_string(),
None => type_of(v, full_name),
}
}
}
_ => v.type_str(),
_ => type_of(v, full_name),
},
None => v.type_str(),
None => type_of(v, full_name),
}
}

/// Returns the type path of the runtime value `v`.
#[inline]
fn type_of(v: &ValueRef, full_name: bool) -> String {
builtin::type_of(v, &ValueRef::bool(full_name)).as_str()
}

impl ValueRef {
fn is_planned_empty(&self) -> bool {
(self.is_dict() && !self.is_truthy()) || self.is_undefined()
Expand Down
Loading