File: path.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (106 lines) | stat: -rw-r--r-- 2,057 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT

package path

import (
	"fmt"
	"strings"

	"code.forgejo.org/f3/gof3/v3/id"
)

type Implementation []PathElement

func (o Implementation) PathString() PathString {
	elements := NewPathString()
	for i, e := range o {
		eid := e.GetID()
		// i == 0 is root and intentionally empty
		if i > 0 && eid == id.NilID {
			eid = id.NewNodeID("nothing")
		}
		elements.Append(eid.String())
	}
	return elements
}

func (o Implementation) PathMappedString() PathString {
	elements := NewPathString()
	for _, e := range o {
		elements.Append(e.GetMappedID().String())
	}
	return elements
}

func (o Implementation) String() string {
	return o.PathString().Join()
}

var replacer = strings.NewReplacer(
	"{", "%7B",
	"}", "%7D",
)

func (o Implementation) ReadablePathString() PathString {
	elements := NewPathString()
	if o.Length() > 0 {
		elements.Append("")
		for _, e := range o[1:] {
			element := e.GetID().String()
			if f := e.ToFormat(); f != nil {
				name := f.GetName()
				if element != name {
					element = fmt.Sprintf("{%s/%s}", replacer.Replace(name), element)
				}
			}
			elements.Append(element)
		}
	}
	return elements
}

func (o Implementation) ReadableString() string {
	return o.ReadablePathString().Join()
}

func (o Implementation) Length() int {
	return len(o)
}

func (o Implementation) Append(child PathElement) Path {
	return append(o, child)
}

func (o Implementation) PopFirst() (PathElement, Path) {
	return o.First(), o.RemoveFirst()
}

func (o Implementation) RemoveFirst() Path {
	return o[1:]
}

func (o Implementation) Pop() (PathElement, Path) {
	return o.Last(), o.RemoveLast()
}

func (o Implementation) RemoveLast() Path {
	return o[:len(o)-1]
}

func (o Implementation) Empty() bool {
	return len(o) == 0
}

func (o Implementation) Last() PathElement {
	return o[len(o)-1]
}

func (o Implementation) First() PathElement {
	return o[0]
}

func (o Implementation) All() []PathElement {
	return o
}