refactor: simplify Node struct with public Tags/Relations fields

This commit is contained in:
2026-04-01 00:13:43 +02:00
parent 4020e5dab3
commit 77d3205e12
3 changed files with 40 additions and 103 deletions

View File

@@ -1,82 +1,30 @@
package models package models
import ( import (
"encoding/json"
"fmt"
"strings" "strings"
) )
var PropertyPrefixes = []string{"_type::", "_status::", "_prio::"}
type Node struct { type Node struct {
ID string `json:"id"` ID string `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Content string `json:"content,omitempty"` Content string `json:"content,omitempty"`
DueDate string `json:"due_date,omitempty"` DueDate string `json:"due_date,omitempty"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
tags []string Tags []string `json:"tags,omitempty"`
relations map[string][]string Relations map[string][]string `json:"relations,omitempty"`
} }
func NewNode() *Node { func NewNode() *Node {
return &Node{ return &Node{
relations: make(map[string][]string), Relations: make(map[string][]string),
} }
} }
var PropertyPrefixes = []string{"_type::", "_status::", "_prio::"}
func (n *Node) MarshalJSON() ([]byte, error) {
type Alias Node
return json.Marshal(&struct {
*Alias
Tags []string `json:"tags,omitempty"`
Relations map[string][]string `json:"relations,omitempty"`
}{
Alias: (*Alias)(n),
Tags: n.tags,
Relations: n.relations,
})
}
func (n *Node) UnmarshalJSON(data []byte) error {
type Alias Node
aux := &struct {
*Alias
Tags []string `json:"tags,omitempty"`
Relations map[string][]string `json:"relations,omitempty"`
}{
Alias: (*Alias)(n),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
n.tags = aux.Tags
if aux.Relations != nil {
n.relations = aux.Relations
} else {
n.relations = make(map[string][]string)
}
return nil
}
func (n *Node) Tags() []string {
res := make([]string, len(n.tags))
copy(res, n.tags)
return res
}
func (n *Node) Relations() map[string][]string {
res := make(map[string][]string)
for k, v := range n.relations {
cv := make([]string, len(v))
copy(cv, v)
res[k] = cv
}
return res
}
func (n *Node) HasTag(tag string) bool { func (n *Node) HasTag(tag string) bool {
for _, t := range n.tags { for _, t := range n.Tags {
if t == tag { if t == tag {
return true return true
} }
@@ -88,41 +36,35 @@ func (n *Node) AddTag(tag string) {
if tag == "" { if tag == "" {
return return
} }
// check if it's a property tag
for _, prefix := range PropertyPrefixes { for _, prefix := range PropertyPrefixes {
if strings.HasPrefix(tag, prefix) { if strings.HasPrefix(tag, prefix) {
// remove existing tags with this prefix
var newTags []string var newTags []string
for _, t := range n.tags { for _, t := range n.Tags {
if !strings.HasPrefix(t, prefix) { if !strings.HasPrefix(t, prefix) {
newTags = append(newTags, t) newTags = append(newTags, t)
} }
} }
n.tags = newTags n.Tags = newTags
break break
} }
} }
if !n.HasTag(tag) { if !n.HasTag(tag) {
n.tags = append(n.tags, tag) n.Tags = append(n.Tags, tag)
} }
} }
func (n *Node) RemoveTag(tag string) error { func (n *Node) RemoveTag(tag string) {
if strings.HasPrefix(tag, "_type::") {
return fmt.Errorf("cannot remove _type tag")
}
var newTags []string var newTags []string
for _, t := range n.tags { for _, t := range n.Tags {
if t != tag { if t != tag {
newTags = append(newTags, t) newTags = append(newTags, t)
} }
} }
n.tags = newTags n.Tags = newTags
return nil
} }
func (n *Node) HasRelation(relType RelType, target string) bool { func (n *Node) HasRelation(relType RelType, target string) bool {
for _, tgt := range n.relations[string(relType)] { for _, tgt := range n.Relations[string(relType)] {
if tgt == target { if tgt == target {
return true return true
} }
@@ -131,41 +73,37 @@ func (n *Node) HasRelation(relType RelType, target string) bool {
} }
func (n *Node) AddRelation(relType RelType, target string) { func (n *Node) AddRelation(relType RelType, target string) {
if n.relations == nil { if n.Relations == nil {
n.relations = make(map[string][]string) n.Relations = make(map[string][]string)
} }
if relType == RelAssignee || relType == RelCreated || relType == RelInNamespace { if relType == RelAssignee || relType == RelCreated || relType == RelInNamespace {
n.relations[string(relType)] = []string{target} n.Relations[string(relType)] = []string{target}
return return
} }
if !n.HasRelation(relType, target) { if !n.HasRelation(relType, target) {
n.relations[string(relType)] = append(n.relations[string(relType)], target) n.Relations[string(relType)] = append(n.Relations[string(relType)], target)
} }
} }
func (n *Node) RemoveRelation(relType RelType, target string) error { func (n *Node) RemoveRelation(relType RelType, target string) {
if relType == RelCreated { if n.Relations == nil {
return fmt.Errorf("cannot remove created relation") return
}
if n.relations == nil {
return nil
} }
var newTgts []string var newTgts []string
for _, tgt := range n.relations[string(relType)] { for _, tgt := range n.Relations[string(relType)] {
if tgt != target { if tgt != target {
newTgts = append(newTgts, tgt) newTgts = append(newTgts, tgt)
} }
} }
if len(newTgts) == 0 { if len(newTgts) == 0 {
delete(n.relations, string(relType)) delete(n.Relations, string(relType))
} else { } else {
n.relations[string(relType)] = newTgts n.Relations[string(relType)] = newTgts
} }
return nil
} }
func (n *Node) GetProperty(k string) string { func (n *Node) GetProperty(k string) string {
for _, t := range n.tags { for _, t := range n.Tags {
if strings.HasPrefix(t, "_") { if strings.HasPrefix(t, "_") {
if p := strings.SplitN(t[1:], "::", 2); len(p) == 2 && p[0] == k { if p := strings.SplitN(t[1:], "::", 2); len(p) == 2 && p[0] == k {
return p[1] return p[1]
@@ -177,7 +115,7 @@ func (n *Node) GetProperty(k string) string {
func (n *Node) GetDisplayTags() []string { func (n *Node) GetDisplayTags() []string {
var tags []string var tags []string
for _, t := range n.tags { for _, t := range n.Tags {
if !strings.HasPrefix(t, "_") { if !strings.HasPrefix(t, "_") {
tags = append(tags, t) tags = append(tags, t)
} }

View File

@@ -77,7 +77,7 @@ func PrintNodes(w io.Writer, svc service.NodeService, nodes []*models.Node, json
}) })
for _, n := range nodes { for _, n := range nodes {
n_rels := n.Relations() n_rels := n.Relations
ns_rel_node_ids := n_rels[string(models.RelInNamespace)] ns_rel_node_ids := n_rels[string(models.RelInNamespace)]
ns_rel_node_titles := make([]string, 0, len(ns_rel_node_ids)) ns_rel_node_titles := make([]string, 0, len(ns_rel_node_ids))
for _, id := range ns_rel_node_ids { for _, id := range ns_rel_node_ids {
@@ -125,7 +125,7 @@ func PrintNode(w io.Writer, svc service.NodeService, n *models.Node, jsonOut boo
fmt.Fprintf(w, "\n tags: %s\n", cPrimary.Sprint(strings.Join(tags, " • "))) fmt.Fprintf(w, "\n tags: %s\n", cPrimary.Sprint(strings.Join(tags, " • ")))
} }
n_rels := n.Relations() n_rels := n.Relations
for relType := range n_rels { for relType := range n_rels {
rel_node_ids := n_rels[string(relType)] rel_node_ids := n_rels[string(relType)]
if len(rel_node_ids) > 0 { if len(rel_node_ids) > 0 {

View File

@@ -70,7 +70,7 @@ func (s *nodeServiceImpl) getAccessContext() (*accessContext, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
rels := userNode.Relations() rels := userNode.Relations
for _, nsID := range rels[string(models.RelHasWriteAccess)] { for _, nsID := range rels[string(models.RelHasWriteAccess)] {
ctx.writable[nsID] = true ctx.writable[nsID] = true
ctx.readable[nsID] = true ctx.readable[nsID] = true
@@ -83,7 +83,7 @@ func (s *nodeServiceImpl) getAccessContext() (*accessContext, error) {
// nodeNamespaceID returns the first in_namespace target of n, or "" if none. // nodeNamespaceID returns the first in_namespace target of n, or "" if none.
func (s *nodeServiceImpl) nodeNamespaceID(n *models.Node) string { func (s *nodeServiceImpl) nodeNamespaceID(n *models.Node) string {
ids := n.Relations()[string(models.RelInNamespace)] ids := n.Relations[string(models.RelInNamespace)]
if len(ids) == 0 { if len(ids) == 0 {
return "" return ""
} }
@@ -375,18 +375,17 @@ func (s *nodeServiceImpl) Update(id string, input UpdateInput) (*models.Node, er
// Compute new tag set using the model's AddTag/RemoveTag to preserve // Compute new tag set using the model's AddTag/RemoveTag to preserve
// property-prefix replacement semantics. // property-prefix replacement semantics.
tmp := models.NewNode() tmp := models.NewNode()
for _, t := range current.Tags() { for _, t := range current.Tags {
tmp.AddTag(t) tmp.AddTag(t)
} }
for _, t := range input.AddTags { for _, t := range input.AddTags {
tmp.AddTag(t) tmp.AddTag(t)
} }
for _, t := range input.RemoveTags { for _, t := range input.RemoveTags {
tmp.RemoveTag(t) //nolint: the error is only for _type:: removal, which is intentionally prevented tmp.RemoveTag(t)
} }
// Sync tags to store. currentTags, newTags := current.Tags, tmp.Tags
currentTags, newTags := current.Tags(), tmp.Tags()
for _, t := range currentTags { for _, t := range currentTags {
if !slices.Contains(newTags, t) { if !slices.Contains(newTags, t) {
if err := st.RemoveTag(id, t); err != nil { if err := st.RemoveTag(id, t); err != nil {
@@ -409,7 +408,7 @@ func (s *nodeServiceImpl) Update(id string, input UpdateInput) (*models.Node, er
} }
} }
currentRels := current.Relations() currentRels := current.Relations
for _, ri := range input.AddRels { for _, ri := range input.AddRels {
resolved, err := s.resolveRelTarget(st, ri) resolved, err := s.resolveRelTarget(st, ri)
if err != nil { if err != nil {
@@ -502,7 +501,7 @@ func (s *nodeServiceImpl) checkBlockers(id string) error {
func (s *nodeServiceImpl) syncMentions(st store.Store, id string, current *models.Node, newTitle, newContent string) error { func (s *nodeServiceImpl) syncMentions(st store.Store, id string, current *models.Node, newTitle, newContent string) error {
existingMentionIDs := make(map[string]bool) existingMentionIDs := make(map[string]bool)
for _, uid := range current.Relations()[string(models.RelMentions)] { for _, uid := range current.Relations[string(models.RelMentions)] {
existingMentionIDs[uid] = true existingMentionIDs[uid] = true
} }
mentionedUserIDs := make(map[string]bool) mentionedUserIDs := make(map[string]bool)