Skip to content

Commit

Permalink
feat: enhance full schema type path in the exec result
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Feb 18, 2024
1 parent a735a37 commit e1b4598
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
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

0 comments on commit e1b4598

Please sign in to comment.