-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget.go
133 lines (126 loc) · 3.06 KB
/
target.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
package finger
import (
"fmt"
)
// Target is used to hold a finger-protocol request ‘target’.
//
// Note that Target is implemented as an optional-type.
// (Which in other programming languages is called a option-type or a maybe-type.)
//
// ⁂
//
// Most people are probably only going to use Target as part
// of finger.Request.
//
// I.e,.
//
// func (receiver *MyFingerHandler) HandleFinger(rw finger.ResponseWriter, request finger.Request) {
//
// // ...
//
// fmt.Printf("finger request target: %#v", request.Target)
//
// // ...
//
// target, something := request.Target.Unwrap()
//
// // ...
//
// }
//
// ⁂
//
// For debugging, one can see the value (or lack of a value) of a Target with code like
// the following:
//
// fmt.Printf("finger request target: %#v", target)
//
// Notice that the verb being used is “%#v” (and not just “%v”).
//
// ⁂
//
// To get a value out of a Target, do something like the following:
//
// var target finger.Target
//
// // ...
//
// value, something := target.Unwrap()
// if something {
// // a value was set for the finger.Target
// } else {
// // no value was set for the finger.Target
// }
//
// Note that this is unwrapping the Target optional-type.
//
// ⁂
//
// One type of finger-protocol request looks like:
//
// "joeblow\r\n"
//
// Another type of finger-protocol request looks like:
//
// "/W joeblow\r\n"
//
// (There are other types of finger-protocol requests, but they aren't relevant here.)
//
// For each of these finger-protocol requests, what would be stored in the code (for finger.Target) is:
//
// var target finger.Target = finger.CreateTarget("joeblow")
type Target struct {
value string
something bool
}
// EmptyTarget is used to create a finger.Target with nothing in it.
func EmptyTarget() Target {
return Target{}
}
// CreateTarget is used to create a finger.Target with something in it.
func CreateTarget(value string) Target {
return Target{
value:value,
something:true,
}
}
// Unwrap is used to unwrap a finger.Target.
//
// value, something := target.Unwrap()
//
// If finger.Target is holding something, then ‘something’ (in the code above) is ‘true’.
//
// If finger.Target is holding nothing, then ‘something’ (in the code above) is ‘false’.
func (receiver Target) Unwrap() (string, bool) {
return receiver.value, receiver.something
}
// 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 target finger.Target = finger.CreateTarget("dariush")
//
// // ...
//
// fmt.Printf("target = %#v", target)
//
// // Output:
// // target = finger.CreateTarget("dariush")
//
// Also, for example:
//
// var target finger.Target = finger.EmptyTarget()
//
// // ...
//
// fmt.Printf("target = %#v", target)
//
// // Output:
// // target = finger.EmptyTarget()
func (receiver Target) GoString() string {
if !receiver.something {
return "finger.EmptyTarget()"
}
return fmt.Sprintf("finger.CreateTarget(%#v)", receiver.value)
}