From e1b45983de83c31dfb2d6ba9579e0de58fb0471a Mon Sep 17 00:00:00 2001 From: peefy Date: Sun, 18 Feb 2024 09:50:33 +0800 Subject: [PATCH] feat: enhance full schema type path in the exec result Signed-off-by: peefy --- ...ith-include-schema-type-path.response.json | 4 +-- kclvm/runtime/src/value/val_plan.rs | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/kclvm/api/src/testdata/exec-program-with-include-schema-type-path.response.json b/kclvm/api/src/testdata/exec-program-with-include-schema-type-path.response.json index b2ea29f7a..0615b2449 100644 --- a/kclvm/api/src/testdata/exec-program-with-include-schema-type-path.response.json +++ b/kclvm/api/src/testdata/exec-program-with-include-schema-type-path.response.json @@ -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" } diff --git a/kclvm/runtime/src/value/val_plan.rs b/kclvm/runtime/src/value/val_plan.rs index 82c37cf3e..7a7268d1a 100644 --- a/kclvm/runtime/src/value/val_plan.rs +++ b/kclvm/runtime/src/value/val_plan.rs @@ -162,7 +162,7 @@ fn handle_schema(ctx: &Context, value: &ValueRef, opts: &PlanOptions) -> (Vec (Vec 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()