-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
135 lines (124 loc) · 2.91 KB
/
path.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package finger
import (
"fmt"
)
// Path represents a finger-protocol path.
//
// For example, in this finger-protocol request:
//
// "dariush/img/[email protected]@something.social\r\n"
//
// “/img/ani.gif” is a path.
//
// ⁂
//
// For debugging, one can see the value (or lack of a value) of a Path with code like
// the following:
//
// var path finger.Path
//
// // ...
//
// fmt.Printf("finger path: %#v", path)
//
// Notice that the verb being used is “%#v” (and not just “%v”).
//
// ⁂
//
// To get a value out of a Path, do something like the following:
//
// var path finger.Path
//
// // ...
//
// value, something := path.Unwrap()
// if something {
// // a value was set for the finger.Path
// } else {
// // no value was set for the finger.Path
// }
//
// Note that this is unwrapping the finger.Path optional-type.
type Path struct {
value string
something bool
}
// EmptyPath is used to create a finger.Path with nothing in it.
func EmptyPath() Path {
return Path{}
}
// CreatePath is used to create a finger.Path with something in it.
func CreatePath(value string) Path {
return Path{
value:value,
something:true,
}
}
// GoString makes it so that when the fmt.Fprintf(), fmt.Printf(), and fmt.Sprintf() family of functions
// renders this type with the %#v verb, that it will be easier to understand.
//
// For example:
//
// var path finger.Path = finger.CreatePath("/path/to/file.ext")
//
// // ...
//
// fmt.Printf("path = %#v", path)
//
// // Output:
// // path = finger.CreatePath("/path/to/file.ext")
//
// Also, for example:
//
// var path finger.Path = finger.EmptyPath()
//
// // ...
//
// fmt.Printf("path = %#v", path)
//
// // Output:
// // path = finger.EmptyPath()
func (receiver Path) GoString() string {
if !receiver.something {
return "finger.EmptyPath()"
}
return fmt.Sprintf("finger.CreatePath(%#v)", receiver.value)
}
// Set sets the value of a finger.Path.
//
// Set mainly exists so that finger.Path can be as a flag.Value, and thus be used with functions
// such as flag.Var().
func (receiver *Path) Set(value string) error {
if nil == receiver {
return nil
}
*receiver = CreatePath(value)
return nil
}
// String returns the value of a finger.Path.
//
// Note that if finger.Path is empty, then it returns the default finger Path,
// which is ""..
//
// With String you cannot tell the difference between a finger.Path with a value of "",
// and an empty finger.Path.
func (receiver Path) String() string {
if !receiver.something {
return ""
}
return receiver.value
}
// Unwrap is used to unwrap a finger.Path.
//
// var path finger.Path
//
// // ...
//
// value, something := path.Unwrap()
//
// If finger.Path is holding something, then ‘something’ (in the code above) is ‘true’.
//
// If finger.Path is holding nothing, then ‘something’ (in the code above) is ‘false’.
func (receiver Path) Unwrap() (string, bool) {
return receiver.value, receiver.something
}