From d648f1289ab181dfc0c519d3a00c677efbde3e40 Mon Sep 17 00:00:00 2001 From: peefy Date: Mon, 23 Oct 2023 14:31:08 +0800 Subject: [PATCH] feat: add without insert index check for dict attributes Signed-off-by: peefy --- kclvm/runtime/src/value/api.rs | 6 ++++-- kclvm/runtime/src/value/val_dict.rs | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/kclvm/runtime/src/value/api.rs b/kclvm/runtime/src/value/api.rs index 2a0c6d2c1..8a48de1ef 100644 --- a/kclvm/runtime/src/value/api.rs +++ b/kclvm/runtime/src/value/api.rs @@ -1058,10 +1058,12 @@ pub unsafe extern "C" fn kclvm_dict_is_override_attr( ) -> kclvm_bool_t { let p = ptr_as_ref(p); let key = c2str(key); - matches!( + let is_override_op = matches!( p.dict_get_attr_operator(key), Some(ConfigEntryOperationKind::Override) - ) as kclvm_bool_t + ); + let without_index = matches!(p.dict_get_insert_index(key), Some(-1) | None); + (is_override_op && without_index) as kclvm_bool_t } #[no_mangle] diff --git a/kclvm/runtime/src/value/val_dict.rs b/kclvm/runtime/src/value/val_dict.rs index dea9cd89e..be76d75bc 100644 --- a/kclvm/runtime/src/value/val_dict.rs +++ b/kclvm/runtime/src/value/val_dict.rs @@ -125,6 +125,17 @@ impl ValueRef { } } + /// Dict get value e.g., {k1 = v1, k2 = v2}.get_attr_operator(k1) == Some(ConfigEntryOperationKind::Override) + pub fn dict_get_insert_index(&self, key: &str) -> Option { + match &*self.rc.borrow() { + Value::dict_value(ref dict) => Some(*dict.insert_indexs.get(key).unwrap_or(&-1)), + Value::schema_value(ref schema) => { + Some(*schema.config.insert_indexs.get(key).unwrap_or(&-1)) + } + _ => None, + } + } + /// Dict get entry e.g., {k1: v1, k2, v2}.get_entry(k1) == {k1: v1} pub fn dict_get_entry(&self, key: &str) -> Option { match &*self.rc.borrow() {