Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Render database item template fields correctly https://github.com/siyuan-note/siyuan/issues/18984

Danielb76bbe03ff5b
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index ff38f4d1c9..da0e27c0cd 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -3597,24 +3597,12 @@ func GetAttributeViewItemKeys(avID, itemID, valueID string) (ret []*BlockAttribu
if nil != err {
return
}
- sql.RenderView(attrView, view, "", false)
-
- var keyValues []*av.KeyValues
- for _, kv := range attrView.KeyValues {
- if av.KeyTypeLineNumber == kv.Key.Type {
- continue
- }
-
- itemKeyValues := &av.KeyValues{Key: kv.Key}
- for _, value := range kv.Values {
- if value.BlockID == itemID {
- itemKeyValues.Values = append(itemKeyValues.Values, value)
- }
- }
- if 0 < len(itemKeyValues.Values) {
- keyValues = append(keyValues, itemKeyValues)
- }
+ viewable := sql.RenderView(attrView, view, "", false)
+ collection, ok := viewable.(av.Collection)
+ if !ok {
+ return
}
+ keyValues := getAttributeViewItemKeyValues(attrView, collection, itemID, false)
refreshAttrViewKeyIDs(attrView, true)
sorts := map[string]int{}
@@ -3678,26 +3666,12 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
continue
}
- // 渲染填充 attrView.KeyValues
- sql.RenderView(attrView, view, "", false)
-
- var keyValues []*av.KeyValues
- for _, kv := range attrView.KeyValues {
- if av.KeyTypeLineNumber == kv.Key.Type {
- // 属性面板中不显示行号字段
- // The line number field no longer appears in the database attribute panel https://github.com/siyuan-note/siyuan/issues/11319
- continue
- }
-
- kValues := &av.KeyValues{Key: kv.Key}
- for _, v := range kv.Values {
- if v.BlockID == itemID {
- kValues.Values = append(kValues.Values, v)
- }
- }
-
- keyValues = append(keyValues, kValues)
+ viewable := sql.RenderView(attrView, view, "", false)
+ collection, ok := viewable.(av.Collection)
+ if !ok {
+ continue
}
+ keyValues := getAttributeViewItemKeyValues(attrView, collection, itemID, true)
// 字段排序
refreshAttrViewKeyIDs(attrView, true)
@@ -3763,6 +3737,30 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
return
}
+func getAttributeViewItemKeyValues(attrView *av.AttributeView, collection av.Collection, itemID string,
+ includeEmpty bool) (ret []*av.KeyValues) {
+ for _, keyValues := range attrView.KeyValues {
+ if av.KeyTypeLineNumber == keyValues.Key.Type {
+ continue
+ }
+
+ itemKeyValues := &av.KeyValues{Key: keyValues.Key}
+ if value := collection.GetValue(itemID, keyValues.Key.ID); nil != value {
+ itemKeyValues.Values = append(itemKeyValues.Values, value)
+ } else {
+ for _, value := range keyValues.Values {
+ if value.BlockID == itemID {
+ itemKeyValues.Values = append(itemKeyValues.Values, value)
+ }
+ }
+ }
+ if includeEmpty || 0 < len(itemKeyValues.Values) {
+ ret = append(ret, itemKeyValues)
+ }
+ }
+ return
+}
+
func genAttrViewGroups(view *av.View, attrView *av.AttributeView) {
if !view.IsGroupView() {
return
diff --git a/kernel/model/attribute_view_item_keys_test.go b/kernel/model/attribute_view_item_keys_test.go
new file mode 100644
index 0000000000..da0ef94441
--- /dev/null
+++ b/kernel/model/attribute_view_item_keys_test.go
@@ -0,0 +1,64 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/av"
+)
+
+func TestGetAttributeViewItemKeyValuesPrefersRenderedValue(t *testing.T) {
+ const (
+ itemID = "20260825000000-item001"
+ templateKeyID = "20260825000001-template"
+ textKeyID = "20260825000002-textkey1"
+ lineKeyID = "20260825000003-linekey1"
+ )
+ staleTemplate := &av.Value{
+ ID: "20260825000004-template", KeyID: templateKeyID, BlockID: itemID, Type: av.KeyTypeTemplate,
+ Template: &av.ValueTemplate{Content: ".action{old}"},
+ }
+ storedText := &av.Value{
+ ID: "20260825000005-textval1", KeyID: textKeyID, BlockID: itemID, Type: av.KeyTypeText,
+ Text: &av.ValueText{Content: "stored"},
+ }
+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{
+ {Key: &av.Key{ID: templateKeyID, Type: av.KeyTypeTemplate}, Values: []*av.Value{staleTemplate}},
+ {Key: &av.Key{ID: textKeyID, Type: av.KeyTypeText}, Values: []*av.Value{storedText}},
+ {Key: &av.Key{ID: lineKeyID, Type: av.KeyTypeLineNumber}},
+ }}
+ renderedTemplate := &av.Value{
+ ID: staleTemplate.ID, KeyID: templateKeyID, BlockID: itemID, Type: av.KeyTypeTemplate,
+ Template: &av.ValueTemplate{Content: "rendered"},
+ }
+ table := &av.Table{Rows: []*av.TableRow{{
+ ID: itemID,
+ Cells: []*av.TableCell{{BaseValue: &av.BaseValue{Value: renderedTemplate}}},
+ }}}
+
+ keyValues := getAttributeViewItemKeyValues(attrView, table, itemID, false)
+ if 2 != len(keyValues) {
+ t.Fatalf("unexpected key values count: %d", len(keyValues))
+ }
+ if renderedTemplate != keyValues[0].Values[0] {
+ t.Fatalf("rendered template value was not preferred: %+v", keyValues[0].Values[0])
+ }
+ if storedText != keyValues[1].Values[0] {
+ t.Fatalf("stored text fallback was not used: %+v", keyValues[1].Values[0])
+ }
+}
diff --git a/kernel/sql/av.go b/kernel/sql/av.go
index 627e477eaa..4d4d5778e9 100644
--- a/kernel/sql/av.go
+++ b/kernel/sql/av.go
@@ -1032,6 +1032,23 @@ func fillAttributeViewKeyValues(attrView *av.AttributeView, collection av.Collec
logging.LogWarnf("attribute view [%s] key [%s] not found while filling values", attrView.ID, keyID)
continue
}
+ if av.KeyTypeTemplate == keyValues.Key.Type || av.KeyTypeCreated == keyValues.Key.Type || av.KeyTypeUpdated == keyValues.Key.Type {
+ renderedBlockIDs := map[string]bool{}
+ for _, val := range values {
+ val.IsRenderAutoFill = true
+ renderedBlockIDs[val.BlockID] = true
+ }
+
+ retained := keyValues.Values[:0]
+ for _, val := range keyValues.Values {
+ if !renderedBlockIDs[val.BlockID] {
+ retained = append(retained, val)
+ }
+ }
+ keyValues.Values = append(retained, values...)
+ continue
+ }
+
existingIDs := map[string]bool{}
for _, kv := range keyValues.Values {
existingIDs[kv.ID] = true
@@ -1040,6 +1057,7 @@ func fillAttributeViewKeyValues(attrView *av.AttributeView, collection av.Collec
if !existingIDs[val.ID] {
val.IsRenderAutoFill = true
keyValues.Values = append(keyValues.Values, val)
+ existingIDs[val.ID] = true
}
}
}
diff --git a/kernel/sql/av_value_test.go b/kernel/sql/av_value_test.go
index b2a76b3b9b..4bb8a76035 100644
--- a/kernel/sql/av_value_test.go
+++ b/kernel/sql/av_value_test.go
@@ -47,6 +47,76 @@ func TestFillAttributeViewKeyValuesSkipsMissingKey(t *testing.T) {
}
}
+func TestFillAttributeViewKeyValuesReplacesRenderedValues(t *testing.T) {
+ const (
+ itemID = "20260825000000-item001"
+ templateKeyID = "20260825000001-template"
+ textKeyID = "20260825000002-textkey1"
+ valueID = "20260825000003-value001"
+ createdKeyID = "20260825000006-created1"
+ updatedKeyID = "20260825000007-updated1"
+ )
+ staleTemplate := &av.Value{
+ ID: valueID, KeyID: templateKeyID, BlockID: itemID, Type: av.KeyTypeTemplate,
+ Template: &av.ValueTemplate{Content: ".action{old}"},
+ }
+ duplicateTemplate := staleTemplate.Clone()
+ storedText := &av.Value{
+ ID: "20260825000004-textval1", KeyID: textKeyID, BlockID: itemID, Type: av.KeyTypeText,
+ Text: &av.ValueText{Content: "stored"},
+ }
+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{
+ {Key: &av.Key{ID: templateKeyID, Type: av.KeyTypeTemplate}, Values: []*av.Value{staleTemplate, duplicateTemplate}},
+ {Key: &av.Key{ID: createdKeyID, Type: av.KeyTypeCreated}, Values: []*av.Value{{
+ ID: "20260825000008-createdv", KeyID: createdKeyID, BlockID: itemID, Type: av.KeyTypeCreated,
+ }}},
+ {Key: &av.Key{ID: updatedKeyID, Type: av.KeyTypeUpdated}, Values: []*av.Value{{
+ ID: "20260825000009-updatedv", KeyID: updatedKeyID, BlockID: itemID, Type: av.KeyTypeUpdated,
+ }}},
+ {Key: &av.Key{ID: textKeyID, Type: av.KeyTypeText}, Values: []*av.Value{storedText}},
+ }}
+ renderedTemplate := &av.Value{
+ ID: valueID, KeyID: templateKeyID, BlockID: itemID, Type: av.KeyTypeTemplate,
+ Template: &av.ValueTemplate{Content: ".action{current}"},
+ }
+ renderedText := &av.Value{
+ ID: storedText.ID, KeyID: textKeyID, BlockID: itemID, Type: av.KeyTypeText,
+ Text: &av.ValueText{Content: "rendered"},
+ }
+ renderedCreated := &av.Value{
+ ID: "20260825000008-createdv", KeyID: createdKeyID, BlockID: itemID, Type: av.KeyTypeCreated,
+ }
+ renderedUpdated := &av.Value{
+ ID: "20260825000009-updatedv", KeyID: updatedKeyID, BlockID: itemID, Type: av.KeyTypeUpdated,
+ }
+ table := &av.Table{Rows: []*av.TableRow{{
+ ID: itemID,
+ Cells: []*av.TableCell{
+ {BaseValue: &av.BaseValue{Value: renderedTemplate}},
+ {BaseValue: &av.BaseValue{Value: renderedCreated}},
+ {BaseValue: &av.BaseValue{Value: renderedUpdated}},
+ {BaseValue: &av.BaseValue{Value: renderedText}},
+ },
+ }}}
+
+ fillAttributeViewKeyValues(attrView, table)
+ if 1 != len(attrView.KeyValues[0].Values) || renderedTemplate != attrView.KeyValues[0].Values[0] {
+ t.Fatalf("rendered template value was not used: %+v", attrView.KeyValues[0].Values)
+ }
+ if !renderedTemplate.IsRenderAutoFill {
+ t.Fatal("rendered template value was not marked as auto-filled")
+ }
+ if renderedCreated != attrView.KeyValues[1].Values[0] || !renderedCreated.IsRenderAutoFill {
+ t.Fatalf("rendered created value was not used: %+v", attrView.KeyValues[1].Values[0])
+ }
+ if renderedUpdated != attrView.KeyValues[2].Values[0] || !renderedUpdated.IsRenderAutoFill {
+ t.Fatalf("rendered updated value was not used: %+v", attrView.KeyValues[2].Values[0])
+ }
+ if storedText != attrView.KeyValues[3].Values[0] || "stored" != attrView.KeyValues[3].Values[0].Text.Content {
+ t.Fatalf("stored text value was replaced: %+v", attrView.KeyValues[3].Values[0])
+ }
+}
+
func TestFillAttributeViewBlockRefSubtypes(t *testing.T) {
const avID = "20260814000000-avtest1"
dynamic := &av.Value{