-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
157 lines (142 loc) · 3.35 KB
/
host.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package finger
import (
"fmt"
)
const (
defaultHost = "127.0.0.1"
)
// Host represents a finger-protocol host.
//
// For example, in this finger-protocol request:
//
// "[email protected]@something.social\r\n"
//
// “example.com” and “something.social” are hosts.
//
// ⁂
//
// For debugging, one can see the value (or lack of a value) of a Host with code like
// the following:
//
// var host finger.Host
//
// // ...
//
// fmt.Printf("finger host: %#v", host)
//
// Notice that the verb being used is “%#v” (and not just “%v”).
//
// ⁂
//
// To get a value out of a Host, do something like the following:
//
// var host finger.Host
//
// // ...
//
// value, something := host.Unwrap()
// if something {
// // a value was set for the finger.Host
// } else {
// // no value was set for the finger.Host
// }
//
// Note that this is unwrapping the finger.Host optional-type.
type Host struct {
value string
something bool
}
// EmptyHost is used to create a finger.Host with nothing in it.
func EmptyHost() Host {
return Host{}
}
// CreateHost is used to create a finger.Host with something in it.
func CreateHost(value string) Host {
return Host{
value:value,
something:true,
}
}
func DefaultHost() Host {
return CreateHost(defaultHost)
}
// 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 host finger.Host = finger.CreateHost("dariush")
//
// // ...
//
// fmt.Printf("host = %#v", host)
//
// // Output:
// // host = finger.CreateHost("dariush")
//
// Also, for example:
//
// var host finger.Host = finger.EmptyHost()
//
// // ...
//
// fmt.Printf("host = %#v", host)
//
// // Output:
// // host = finger.EmptyHost()
func (receiver Host) GoString() string {
if !receiver.something {
return "finger.EmptyHost()"
}
return fmt.Sprintf("finger.CreateHost(%#v)", receiver.value)
}
// Set sets the value of a finger.Host.
//
// Set mainly exists so that finger.Host can be as a flag.Value, and thus be used with functions
// such as flag.Var().
func (receiver *Host) Set(value string) error {
if nil == receiver {
return nil
}
*receiver = CreateHost(value)
return nil
}
// String returns the value of a finger.Host.
//
// Note that if finger.Host is empty, then it returns the default finger Host,
// which is 127.0.0.1.
//
// With String you cannot tell the difference between a finger.Host with a value of 127.0.0.1
// and an empty finger.Host.
func (receiver Host) String() string {
if !receiver.something {
return defaultHost
}
return receiver.value
}
// Resolve resolves a finger.Host.
//
// If the finger.Host has a value, then Resolve returns that.
//
// Else if the finger.Host is empty, then Resolve returns the default finger host value,
// which is 127.0.0.1.
func (receiver Host) Resolve() string {
if !receiver.something {
return defaultHost
}
return receiver.value
}
// Unwrap is used to unwrap a finger.Host.
//
// var host finger.Host
//
// // ...
//
// value, something := host.Unwrap()
//
// If finger.Host is holding something, then ‘something’ (in the code above) is ‘true’.
//
// If finger.Host is holding nothing, then ‘something’ (in the code above) is ‘false’.
func (receiver Host) Unwrap() (string, bool) {
return receiver.value, receiver.something
}