Skip to content

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

:art: Clean code

Daniel25a75982c9a3
diff --git a/kernel/av/value.go b/kernel/av/value.go
index 1974f09fa0..8ca108d45a 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -25,6 +25,7 @@ import (
"time"
"github.com/88250/gulu"
+ "github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/util"
"golang.org/x/text/language"
"golang.org/x/text/message"
@@ -902,3 +903,56 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) {
}
}
}
+
+func GetAttributeViewDefaultValue(valueID, keyID, blockID string, typ KeyType) (ret *Value) {
+ if "" == valueID {
+ valueID = ast.NewNodeID()
+ }
+
+ ret = &Value{ID: valueID, KeyID: keyID, BlockID: blockID, Type: typ}
+
+ createdStr := valueID[:len("20060102150405")]
+ created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
+ if nil == parseErr {
+ ret.CreatedAt = created.UnixMilli()
+ } else {
+ ret.CreatedAt = time.Now().UnixMilli()
+ }
+ if 0 == ret.UpdatedAt {
+ ret.UpdatedAt = ret.CreatedAt
+ }
+
+ switch typ {
+ case KeyTypeText:
+ ret.Text = &ValueText{}
+ case KeyTypeNumber:
+ ret.Number = &ValueNumber{}
+ case KeyTypeDate:
+ ret.Date = &ValueDate{}
+ case KeyTypeSelect:
+ ret.MSelect = []*ValueSelect{}
+ case KeyTypeMSelect:
+ ret.MSelect = []*ValueSelect{}
+ case KeyTypeURL:
+ ret.URL = &ValueURL{}
+ case KeyTypeEmail:
+ ret.Email = &ValueEmail{}
+ case KeyTypePhone:
+ ret.Phone = &ValuePhone{}
+ case KeyTypeMAsset:
+ ret.MAsset = []*ValueAsset{}
+ case KeyTypeTemplate:
+ ret.Template = &ValueTemplate{}
+ case KeyTypeCreated:
+ ret.Created = &ValueCreated{}
+ case KeyTypeUpdated:
+ ret.Updated = &ValueUpdated{}
+ case KeyTypeCheckbox:
+ ret.Checkbox = &ValueCheckbox{}
+ case KeyTypeRelation:
+ ret.Relation = &ValueRelation{}
+ case KeyTypeRollup:
+ ret.Rollup = &ValueRollup{}
+ }
+ return
+}
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 23140b3700..4e1d805bcc 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -398,7 +398,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
keyValues = append(keyValues, kValues)
} else {
// 如果没有值,那么就补一个默认值
- kValues.Values = append(kValues.Values, treenode.GetAttributeViewDefaultValue(ast.NewNodeID(), kv.Key.ID, blockID, kv.Key.Type))
+ kValues.Values = append(kValues.Values, av.GetAttributeViewDefaultValue(ast.NewNodeID(), kv.Key.ID, blockID, kv.Key.Type))
keyValues = append(keyValues, kValues)
}
}
@@ -426,7 +426,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
destVal := destAv.GetValue(kv.Key.Rollup.KeyID, bID)
if nil == destVal {
if destAv.ExistBlock(bID) { // 数据库中存在行但是列值不存在是数据未初始化,这里补一个默认值
- destVal = treenode.GetAttributeViewDefaultValue(ast.NewNodeID(), kv.Key.Rollup.KeyID, bID, destKey.Type)
+ destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), kv.Key.Rollup.KeyID, bID, destKey.Type)
}
if nil == destVal {
continue
@@ -1118,7 +1118,7 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
if nil == destVal {
if destAv.ExistBlock(blockID) { // 数据库中存在行但是列值不存在是数据未初始化,这里补一个默认值
- destVal = treenode.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
+ destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
}
if nil == destVal {
continue
diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go
index 9829087c62..5fbc14ce17 100644
--- a/kernel/treenode/node.go
+++ b/kernel/treenode/node.go
@@ -765,7 +765,7 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
if nil == destVal {
if destAv.ExistBlock(blockID) { // 数据库中存在行但是列值不存在是数据未初始化,这里补一个默认值
- destVal = GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
+ destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
}
if nil == destVal {
continue
@@ -870,7 +870,7 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
func FillAttributeViewTableCellNilValue(tableCell *av.TableCell, rowID, colID string) {
if nil == tableCell.Value {
- tableCell.Value = GetAttributeViewDefaultValue(tableCell.ID, colID, rowID, tableCell.ValueType)
+ tableCell.Value = av.GetAttributeViewDefaultValue(tableCell.ID, colID, rowID, tableCell.ValueType)
return
}
@@ -939,59 +939,6 @@ func FillAttributeViewTableCellNilValue(tableCell *av.TableCell, rowID, colID st
}
}
-func GetAttributeViewDefaultValue(valueID, keyID, blockID string, typ av.KeyType) (ret *av.Value) {
- if "" == valueID {
- valueID = ast.NewNodeID()
- }
-
- ret = &av.Value{ID: valueID, KeyID: keyID, BlockID: blockID, Type: typ}
-
- createdStr := valueID[:len("20060102150405")]
- created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
- if nil == parseErr {
- ret.CreatedAt = created.UnixMilli()
- } else {
- ret.CreatedAt = time.Now().UnixMilli()
- }
- if 0 == ret.UpdatedAt {
- ret.UpdatedAt = ret.CreatedAt
- }
-
- switch typ {
- case av.KeyTypeText:
- ret.Text = &av.ValueText{}
- case av.KeyTypeNumber:
- ret.Number = &av.ValueNumber{}
- case av.KeyTypeDate:
- ret.Date = &av.ValueDate{}
- case av.KeyTypeSelect:
- ret.MSelect = []*av.ValueSelect{}
- case av.KeyTypeMSelect:
- ret.MSelect = []*av.ValueSelect{}
- case av.KeyTypeURL:
- ret.URL = &av.ValueURL{}
- case av.KeyTypeEmail:
- ret.Email = &av.ValueEmail{}
- case av.KeyTypePhone:
- ret.Phone = &av.ValuePhone{}
- case av.KeyTypeMAsset:
- ret.MAsset = []*av.ValueAsset{}
- case av.KeyTypeTemplate:
- ret.Template = &av.ValueTemplate{}
- case av.KeyTypeCreated:
- ret.Created = &av.ValueCreated{}
- case av.KeyTypeUpdated:
- ret.Updated = &av.ValueUpdated{}
- case av.KeyTypeCheckbox:
- ret.Checkbox = &av.ValueCheckbox{}
- case av.KeyTypeRelation:
- ret.Relation = &av.ValueRelation{}
- case av.KeyTypeRollup:
- ret.Rollup = &av.ValueRollup{}
- }
- return
-}
-
func renderTemplateCol(ial map[string]string, rowValues []*av.KeyValues, tplContent string) string {
if "" == ial["id"] {
block := getRowBlockValue(rowValues)