-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.go
107 lines (100 loc) · 2.28 KB
/
actor.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
package finger
import (
"fmt"
)
// Actor represents a finger-protocol actor.
//
// For example, in this finger-protocol request:
//
// "dariush@example.com@something.social\r\n"
//
// “dariush” is the actor.
//
// ⁂
//
// For debugging, one can see the value (or lack of a value) of a Actor with code like
// the following:
//
// var actor finger.Actor
//
// // ...
//
// fmt.Printf("finger actor: %#v", actor)
//
// Notice that the verb being used is “%#v” (and not just “%v”).
//
// ⁂
//
// To get a value out of a Actor, do something like the following:
//
// var actor finger.Actor
//
// // ...
//
// value, something := actor.Unwrap()
// if something {
// // a value was set for the finger.Actor
// } else {
// // no value was set for the finger.Actor
// }
//
// Note that this is unwrapping the finger.Actor optional-type.
type Actor struct {
value string
something bool
}
// EmptyActor is used to create a finger.Actor with nothing in it.
func EmptyActor() Actor {
return Actor{}
}
// CreateActor is used to create a finger.Actor with something in it.
func CreateActor(value string) Actor {
return Actor{
value:value,
something:true,
}
}
// Unwrap is used to unwrap a finger.Actor.
//
// var actor finger.Actor
//
// // ...
//
// value, something := actor.Unwrap()
//
// If finger.Actor is holding something, then ‘something’ (in the code above) is ‘true’.
//
// If finger.Actor is holding nothing, then ‘something’ (in the code above) is ‘false’.
func (receiver Actor) 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 actor finger.Actor = finger.CreateActor("dariush")
//
// // ...
//
// fmt.Printf("actor = %#v", actor)
//
// // Output:
// // actor = finger.CreateActor("dariush")
//
// Also, for example:
//
// var actor finger.Actor = finger.EmptyActor()
//
// // ...
//
// fmt.Printf("actor = %#v", actor)
//
// // Output:
// // actor = finger.EmptyActor()
func (receiver Actor) GoString() string {
if !receiver.something {
return "finger.EmptyActor()"
}
return fmt.Sprintf("finger.CreateActor(%#v)", receiver.value)
}