Skip to content

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

:recycle: Use standard slice operations in kernel helpers https://github.com/siyuan-note/siyuan/issues/20245

Daniel51397a248e8f
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 4dd88170ad..a6c27d1a0d 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -896,7 +896,7 @@ func sortAttributeViewGroup(avID, blockID, previousGroupID, groupID string) (err
break
}
}
- view.Groups = util.InsertElem(view.Groups, previousIndex, groupView)
+ view.Groups = slices.Insert(view.Groups, previousIndex, groupView)
for i, g := range view.Groups {
g.GroupSort = i
@@ -4825,7 +4825,7 @@ func (tx *Transaction) doSortAttrViewView(operation *Operation) (ret *TxErr) {
break
}
}
- attrView.Views = util.InsertElem(attrView.Views, previousIndex, view)
+ attrView.Views = slices.Insert(attrView.Views, previousIndex, view)
if err = avSaveView(attrView, operation.BlockID); err != nil {
logging.LogErrorf("save attribute view [%s] failed: %s", avID, err)
@@ -6812,7 +6812,7 @@ func duplicateAttributeViewKey(operation *Operation) (err error) {
view.Calendar != nil && layout == view.Calendar.LayoutTable && av.LayoutTypeCalendar != view.LayoutType {
cloned.Hidden = true
}
- layout.Columns = util.InsertElem(layout.Columns, i+1, cloned)
+ layout.Columns = slices.Insert(layout.Columns, i+1, cloned)
break
}
}
@@ -6826,7 +6826,7 @@ func duplicateAttributeViewKey(operation *Operation) (err error) {
return
}
cloned.ID = copyKey.ID
- view.Gallery.CardFields = util.InsertElem(view.Gallery.CardFields, i+1, cloned)
+ view.Gallery.CardFields = slices.Insert(view.Gallery.CardFields, i+1, cloned)
break
}
}
@@ -6840,7 +6840,7 @@ func duplicateAttributeViewKey(operation *Operation) (err error) {
return
}
cloned.ID = copyKey.ID
- view.Kanban.Fields = util.InsertElem(view.Kanban.Fields, i+1, cloned)
+ view.Kanban.Fields = slices.Insert(view.Kanban.Fields, i+1, cloned)
break
}
}
@@ -7304,7 +7304,7 @@ func sortAttributeViewRow(operation *Operation) (err error) {
break
}
}
- targetGroupView.GroupItemIDs = util.InsertElem(targetGroupView.GroupItemIDs, previousIndex, itemID)
+ targetGroupView.GroupItemIDs = slices.Insert(targetGroupView.GroupItemIDs, previousIndex, itemID)
}
regenAttrViewGroups(attrView)
@@ -7315,7 +7315,7 @@ func sortAttributeViewRow(operation *Operation) (err error) {
break
}
}
- groupView.GroupItemIDs = util.InsertElem(groupView.GroupItemIDs, previousIndex, itemID)
+ groupView.GroupItemIDs = slices.Insert(groupView.GroupItemIDs, previousIndex, itemID)
}
}
} else {
@@ -7339,7 +7339,7 @@ func sortAttributeViewRow(operation *Operation) (err error) {
break
}
}
- view.ItemIDs = util.InsertElem(view.ItemIDs, previousIndex, itemID)
+ view.ItemIDs = slices.Insert(view.ItemIDs, previousIndex, itemID)
}
err = av.SaveAttributeView(attrView)
@@ -7392,7 +7392,7 @@ func SortAttributeViewViewKey(avID, blockID, keyID, previousKeyID string) (err e
break
}
}
- view.GetTableLayout().Columns = util.InsertElem(view.GetTableLayout().Columns, previousIndex, col)
+ view.GetTableLayout().Columns = slices.Insert(view.GetTableLayout().Columns, previousIndex, col)
case av.LayoutTypeGallery:
var field *av.ViewGalleryCardField
for i, cardField := range view.Gallery.CardFields {
@@ -7413,7 +7413,7 @@ func SortAttributeViewViewKey(avID, blockID, keyID, previousKeyID string) (err e
break
}
}
- view.Gallery.CardFields = util.InsertElem(view.Gallery.CardFields, previousIndex, field)
+ view.Gallery.CardFields = slices.Insert(view.Gallery.CardFields, previousIndex, field)
case av.LayoutTypeKanban:
var field *av.ViewKanbanField
for i, kanbanField := range view.Kanban.Fields {
@@ -7434,7 +7434,7 @@ func SortAttributeViewViewKey(avID, blockID, keyID, previousKeyID string) (err e
break
}
}
- view.Kanban.Fields = util.InsertElem(view.Kanban.Fields, previousIndex, field)
+ view.Kanban.Fields = slices.Insert(view.Kanban.Fields, previousIndex, field)
}
err = av.SaveAttributeView(attrView)
@@ -7482,7 +7482,7 @@ func SortAttributeViewKey(avID, keyID, previousKeyID string) (err error) {
break
}
}
- attrView.KeyIDs = util.InsertElem(attrView.KeyIDs, previousIndex, currentKeyID)
+ attrView.KeyIDs = slices.Insert(attrView.KeyIDs, previousIndex, currentKeyID)
err = av.SaveAttributeView(attrView)
return
@@ -7595,7 +7595,7 @@ func addAttributeViewKey(attrView *av.AttributeView, currentView *av.View, key *
break
}
}
- view.Calendar.Columns = util.InsertElem(view.Calendar.Columns, index, column)
+ view.Calendar.Columns = slices.Insert(view.Calendar.Columns, index, column)
}
if nil != view.List {
hidden := nil == currentView || currentView.ID != view.ID || av.LayoutTypeList != currentView.LayoutType
@@ -7607,7 +7607,7 @@ func addAttributeViewKey(attrView *av.AttributeView, currentView *av.View, key *
break
}
}
- view.List.Columns = util.InsertElem(view.List.Columns, index, column)
+ view.List.Columns = slices.Insert(view.List.Columns, index, column)
}
if nil != view.Table {
newField := &av.BaseField{ID: key.ID, Wrap: view.Table.WrapField}
@@ -8697,7 +8697,7 @@ func sortAttrViewBindingIDs(avIDs []string, avID, previousAvID string) (ret []st
}
}
- ret = util.InsertElem(ret, previousIndex+1, avID)
+ ret = slices.Insert(ret, previousIndex+1, avID)
return
}
diff --git a/kernel/model/attribute_view_option_undo.go b/kernel/model/attribute_view_option_undo.go
index 7386b5f96c..4034d132b3 100644
--- a/kernel/model/attribute_view_option_undo.go
+++ b/kernel/model/attribute_view_option_undo.go
@@ -19,11 +19,11 @@ package model
import (
"encoding/json"
"errors"
+ "slices"
"strings"
"github.com/88250/lute/parse"
"github.com/siyuan-note/siyuan/kernel/av"
- "github.com/siyuan-note/siyuan/kernel/util"
)
// 删除和合并选项共用字段变化快照,恢复实际改变的值、筛选条件、模板默认值和分组状态。
@@ -301,7 +301,7 @@ func updateAttributeViewColumnOptionValues(attrView *av.AttributeView, operation
}
}
if 0 <= idx {
- value.MSelect = util.RemoveElem(value.MSelect, idx)
+ value.MSelect = slices.Delete(value.MSelect, idx, idx+1)
}
} else {
for i, opt := range value.MSelect {
diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go
index 8fc4a30417..3223f34a90 100644
--- a/kernel/model/attribute_view_render.go
+++ b/kernel/model/attribute_view_render.go
@@ -878,7 +878,7 @@ func sortGroupViews(attrView *av.AttributeView, view *av.View) {
startIdx = 0
}
for _, g := range lastNext30Days {
- relativeDateGroups = util.InsertElem(relativeDateGroups, startIdx, g)
+ relativeDateGroups = slices.Insert(relativeDateGroups, startIdx, g)
}
if av.GroupOrderDesc == view.Group.Order {
diff --git a/kernel/model/oidc_provider/provider.go b/kernel/model/oidc_provider/provider.go
index e183828641..b812b51b6e 100644
--- a/kernel/model/oidc_provider/provider.go
+++ b/kernel/model/oidc_provider/provider.go
@@ -15,6 +15,7 @@ import (
"fmt"
"io"
"net/http"
+ "slices"
"strings"
"github.com/coreos/go-oidc/v3/oidc"
@@ -65,7 +66,7 @@ func New(ctx context.Context, config *conf.OIDC, redirectURL string) (*Provider,
return nil, fmt.Errorf("discover OIDC provider failed: %w", err)
}
scopes := append([]string{}, config.Scopes...)
- if !contains(scopes, oidc.ScopeOpenID) {
+ if !slices.Contains(scopes, oidc.ScopeOpenID) {
scopes = append([]string{oidc.ScopeOpenID}, scopes...)
}
return &Provider{
@@ -126,10 +127,10 @@ func newGitHub(config *conf.OIDC, redirectURL string) *Provider {
}
}
scopes = filtered
- if !contains(scopes, "read:user") {
+ if !slices.Contains(scopes, "read:user") {
scopes = append([]string{"read:user"}, scopes...)
}
- if !contains(scopes, "user:email") {
+ if !slices.Contains(scopes, "user:email") {
scopes = append(scopes, "user:email")
}
}
@@ -152,7 +153,7 @@ func isDefaultOIDCScopes(scopes []string) bool {
if len(scopes) != 3 {
return false
}
- return contains(scopes, oidc.ScopeOpenID) && contains(scopes, "profile") && contains(scopes, "email")
+ return slices.Contains(scopes, oidc.ScopeOpenID) && slices.Contains(scopes, "profile") && slices.Contains(scopes, "email")
}
func exchangeGitHubClaims(ctx context.Context, token *oauth2.Token) (map[string]any, error) {
@@ -207,12 +208,3 @@ func getGitHubJSON(ctx context.Context, client *http.Client, endpoint string, ta
decoder.UseNumber()
return decoder.Decode(target)
}
-
-func contains(values []string, target string) bool {
- for _, value := range values {
- if value == target {
- return true
- }
- }
- return false
-}
diff --git a/kernel/util/misc.go b/kernel/util/misc.go
index baff792df6..416758b85f 100644
--- a/kernel/util/misc.go
+++ b/kernel/util/misc.go
@@ -101,23 +101,6 @@ func RandString(length int) string {
return string(b)
}
-// InsertElem inserts value at index into s.
-// 0 <= index <= len(s)
-func InsertElem[T any](s []T, index int, value T) []T {
- if len(s) == index { // nil or empty slice or after last element
- return append(s, value)
- }
-
- s = append(s[:index+1], s[index:]...) // index < len(s)
- s[index] = value
- return s
-}
-
-// RemoveElem removes the element at index i from s.
-func RemoveElem[T any](s []T, index int) []T {
- return append(s[:index], s[index+1:]...)
-}
-
func EscapeHTML(s string) (ret string) {
ret = s
if "" == strings.TrimSpace(ret) {