siyuan-note/siyuan · error

new item templates config is nil

Error message

new item templates config is nil

What it means

SetNewItemTemplates validates and replaces the database's new-item template configuration. A nil config argument is rejected outright because there is nothing valid to install — clearing templates must use an explicit empty config instead.

Solutions

  1. Pass a non-nil &NewItemTemplatesConfig{}, e.g. with Templates: []*NewItemTemplate{} to clear all templates.
  2. Check the pointer for nil at the call site and convert it to an empty config.
  3. When reading config from a request, default an absent object to an empty struct before calling.

Example fix

// before
av.SetNewItemTemplates(nil)
// after
av.SetNewItemTemplates(&av.NewItemTemplatesConfig{Templates: []*av.NewItemTemplate{}})
Defensive patterns

Strategy: validation

Validate before calling

if cfg == nil {
	cfg = &av.NewItemTemplatesConfig{}
}

Type guard

func validTemplatesConfig(c *av.NewItemTemplatesConfig) bool { return c != nil }

Prevention

When it happens

Trigger: Calling av.AttributeView.SetNewItemTemplates(nil), typically when the caller builds the config from optional JSON fields that were all absent.

Common situations: API/plugin payload missing the templates object; deserializing an empty JSON body into a typed pointer yielding nil; refactored code passing a possibly-nil pointer without a check.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/258982342a32c48c. Report an issue: GitHub.

Appendix: source

Thrown at kernel/av/new_item_template.go:40

	"sort"
	"strings"

	"github.com/88250/lute/ast"

	"github.com/siyuan-note/siyuan/kernel/util"
)

// PrunedNewItemTemplateOption 描述创建条目前从模板字段值中移除的无效选项。
type PrunedNewItemTemplateOption struct {
	TemplateID string
	KeyID      string
	Values     []string
}

// SetNewItemTemplates 校验并替换数据库的新增条目模板配置。
func (av *AttributeView) SetNewItemTemplates(config *NewItemTemplatesConfig) error {
	if nil == config {
		return errors.New("new item templates config is nil")
	}

	templates := make([]*NewItemTemplate, 0, len(config.Templates))
	templateIDs := map[string]bool{}
	defaultTemplateID := config.DefaultTemplateID
	for _, itemTemplate := range config.Templates {
		if nil == itemTemplate {
			continue
		}
		itemTemplate = cloneNewItemTemplate(itemTemplate)
		if nil == itemTemplate {
			return errors.New("clone new item template failed")
		}
		itemTemplate.Name = strings.TrimSpace(itemTemplate.Name)
		itemTemplate.Icon = strings.TrimSpace(itemTemplate.Icon)
		if filteredIcon, valid := util.FilterIconValue(itemTemplate.Icon); valid {
			itemTemplate.Icon = filteredIcon
		} else {

View on GitHub (pinned to 9f775e8a12)