123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package main
- import (
- "fmt"
- "log"
- "strconv"
- "strings"
- "golang.org/x/exp/slices"
- )
- type Node struct {
- Tags []string
- Line string
- Parent *Node `json:"-"`
- Children []*Node
- }
- func (n *Node) HasTag(tag string) bool {
- return slices.Contains(n.Tags, tag)
- }
- func (n *Node) AddTag(tag string) {
- if !n.HasTag(tag) {
- n.Tags = append(n.Tags, strings.ToLower(tag))
- }
- }
- func (n *Node) RmTag(tag string) {
- if n.HasTag(tag) {
- tags := []string{}
- for _, t := range n.Tags {
- if t != tag {
- tags = append(tags, t)
- }
- }
- n.Tags = tags
- }
- }
- func (n *Node) Depth() int {
- if n.Parent == nil {
- return 0
- }
- depth := 0
- at := n
- for at.Parent != nil {
- at = at.Parent
- depth += 1
- }
- if at.HasTag("root") {
- depth -= 1
- }
- return depth
- }
- func (n *Node) String() string {
- return fmt.Sprintf("%s%s", strings.Repeat("\t", n.Depth()), n.Line)
- }
- func (n *Node) StringAll() string {
- out := strings.Builder{}
- out.WriteString(n.String() + "\n")
- if n.Len() != 0 {
-
- for _, kid := range n.Children {
- out.WriteString(kid.StringAll())
- }
- }
- return out.String()
- }
- func (n *Node) AddChild(child *Node) {
- child.Parent = n
- n.Children = append(n.Children, child)
- }
- func (n *Node) NewChild() *Node {
- kid := Node{
- Parent: n,
- }
- n.AddChild(&kid)
- return &kid
- }
- func (n *Node) RmChild(index int) error {
- if index > n.Len() || index < 0 {
- return fmt.Errorf("invalid index %d (min=0, max=%d)", index, n.Len())
- }
- var kids []*Node
- for idx, k := range n.Children {
- if idx != index {
- kids = append(kids, k)
- } else {
- k.Parent = nil
- }
- }
- n.Children = kids
- return nil
- }
- func (n *Node) RmAllChildren() {
- for _, k := range n.Children {
- k.Parent = nil
- }
- n.Children = []*Node{}
- }
- func (n *Node) DetachFromParent() (*Node, error) {
- if n.Parent != nil {
- p := n.Parent
- index := -1
- for idx, k := range p.Children {
- if k.Line == n.Line && k.Depth() == n.Depth() && slices.Equal(k.Tags, n.Tags) && k.Len() == n.Len() {
- index = idx
- break
- }
- }
- if index != -1 {
- return p.SplitChild(index)
- } else {
- return nil, fmt.Errorf("failed detaching, didn't find ourselves in parent")
- }
- }
- return nil, fmt.Errorf("failed detaching, invalid parent")
- }
- func (n *Node) Child(index int) (*Node, error) {
- if index > n.Len() || index < 0 {
- return nil, fmt.Errorf("invalid index %d (min=0, max=%d)", index, n.Len())
- }
- return n.Children[index], nil
- }
- func (n *Node) ReplaceChild(index int, node *Node) error {
- if index > n.Len() || index < 0 {
- return fmt.Errorf("invalid index %d (min=0, max=%d)", index, n.Len())
- }
- n.Children[index].Parent = nil
- node.Parent = n
- n.Children[index] = node
- return nil
- }
- func (n *Node) SplitChild(index int) (*Node, error) {
- if index > n.Len() || index < 0 {
- return nil, fmt.Errorf("invalid index %d (min=0, max=%d)", index, n.Len())
- }
- k := n.Children[index]
- k.Parent = nil
- err := n.RmChild(index)
- if err != nil {
- k.Parent = n
- return nil, err
- }
- return k, nil
- }
- func (n *Node) Len() int {
- return len(n.Children)
- }
- func (n *Node) Key() string {
- parts := strings.Split(n.Line, " ")
- if strings.Contains(parts[0], "\"") {
- pref := ""
- for _, part := range parts {
- if strings.HasSuffix(part, "\"") {
- if pref != "" {
- pref += " "
- }
- pref += part
- break
- }
- if pref != "" {
- pref += " "
- }
- pref += part
- }
- pref = strings.ReplaceAll(pref, "\"", "")
- return pref
- }
- return parts[0]
- }
- func (n *Node) Value() string {
- key := n.Key()
- val := strings.Replace(n.Line, n.Format(key)+" ", "", 1)
- val = strings.ReplaceAll(val, "\"", "")
- return val
- }
- func (n *Node) ValueInt() int {
- v, err := strconv.Atoi(n.Value())
- if err != nil {
- log.Printf("ValueInt('%s') => %v", n.Value(), err)
- return 0
- }
- return v
- }
- func (n *Node) ValueFloat64() float64 {
- v, err := strconv.ParseFloat(n.Value(), 64)
- if err != nil {
- log.Printf("ValueFloat64('%s') => %v", n.Value(), err)
- return 0.0
- }
- return v
- }
- func (n *Node) ValueFloat32() float32 {
- v, err := strconv.ParseFloat(n.Value(), 32)
- if err != nil {
- log.Printf("ValueFloat32('%s') => %v", n.Value(), err)
- return 0.0
- }
- return float32(v)
- }
- func (n *Node) Format(value string) string {
- if strings.Contains(value, " ") {
- return "\"" + value + "\""
- }
- return value
- }
- func (n *Node) SetValue(val interface{}) {
- if val == nil {
- n.Line = n.Format(n.Key())
- return
- }
- str_val := fmt.Sprintf("%v", val)
- n.Line = fmt.Sprintf("%s %s", n.Format(n.Key()), n.Format(str_val))
- }
- func (n *Node) SetKey(key string) {
- val := n.Value()
- if val != "" {
- n.Line = fmt.Sprintf("%s %s", n.Format(key), n.Format(val))
- } else {
- n.Line = n.Format(key)
- }
- }
- func (n *Node) Set(key string, val interface{}) {
- if val == nil {
- n.Line = n.Format(key)
- return
- }
- str_val := fmt.Sprintf("%v", val)
- n.Line = fmt.Sprintf("%s %s", n.Format(key), n.Format(str_val))
- }
|