notea-de-me/spike/review/reinvent/ezmded/server/tree.go

127 lines
2.3 KiB
Go

package main
import (
"errors"
"io/ioutil"
"os"
"time"
yaml "gopkg.in/yaml.v2"
)
type Tree struct {
path string
}
type Branch struct {
Title string
Deleted bool
Updated time.Time
PID string
}
type Pretty struct {
Children map[string]*Pretty
Title string
}
func NewTree(path string) *Tree {
return &Tree{
path: path,
}
}
func (tree *Tree) GetPretty() (map[string]*Pretty, error) {
branches, err := tree.Get()
if err != nil {
return nil, err
}
_ = branches
realpretty := map[string]*Pretty{}
lookuppretty := map[string]*Pretty{}
timesZero := 0
for len(branches) > 0 && timesZero < 3 {
topop := []string{}
for id, branch := range branches {
if branch.Deleted {
} else if branch.PID == "" {
realpretty[id] = &Pretty{
Title: branch.Title,
Children: map[string]*Pretty{},
}
lookuppretty[id] = realpretty[id]
} else if ptr, ok := lookuppretty[branch.PID]; ok {
ptr.Children[id] = &Pretty{
Title: branch.Title,
Children: map[string]*Pretty{},
}
lookuppretty[id] = ptr.Children[id]
} else {
continue
}
topop = append(topop, id)
}
for _, id := range topop {
delete(branches, id)
}
if len(topop) == 0 {
timesZero++
} else {
timesZero = 0
}
}
return realpretty, nil
}
func (tree *Tree) Get() (map[string]Branch, error) {
b, err := ioutil.ReadFile(tree.path)
if os.IsNotExist(err) {
return map[string]Branch{}, nil
}
if err != nil {
return nil, err
}
var m map[string]Branch
err = yaml.Unmarshal(b, &m)
return m, err
}
func (tree *Tree) Set(m map[string]Branch) error {
b, err := yaml.Marshal(m)
if err != nil {
return err
}
return ioutil.WriteFile(tree.path, b, os.ModePerm)
}
func (tree *Tree) Del(id string) error {
m, err := tree.Get()
if err != nil {
return err
}
branch, ok := m[id]
if !ok {
return nil
}
branch.Updated = time.Now().UTC()
branch.Deleted = true
m[id] = branch
return tree.Set(m)
}
func (tree *Tree) Put(id string, branch Branch) error {
branch.Updated = time.Now().UTC()
if branch.Title == "" {
branch.Title = "Untitled (" + time.Now().UTC().String() + ")"
}
m, err := tree.Get()
if err != nil {
return err
}
if _, ok := m[branch.PID]; !ok && branch.PID != "" {
return errors.New("PID does not exist")
}
m[id] = branch
return tree.Set(m)
}