forked from percolate/charlatan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
105 lines (80 loc) · 2.34 KB
/
channel.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
package main
import "fmt"
// Channel is the built-in bidirectional channel
type Channel struct {
subType Type
parameterFormat string
fieldFormat string
}
// ParameterFormat returns syntax for a parameter declaration
func (t *Channel) ParameterFormat() string {
if t.parameterFormat != "" {
return t.parameterFormat
}
t.parameterFormat = fmt.Sprintf("chan %s", t.subType.ParameterFormat())
return t.parameterFormat
}
// ReferenceFormat returns the syntax for a reference
func (t *Channel) ReferenceFormat() string {
return ""
}
// FieldFormat returns the syntax for a field declaration
func (t *Channel) FieldFormat() string {
if t.fieldFormat != "" {
return t.fieldFormat
}
t.fieldFormat = fmt.Sprintf("chan %s", t.subType.FieldFormat())
return t.fieldFormat
}
// ReceiveChannel is the built-in receive-only channel
type ReceiveChannel struct {
subType Type
parameterFormat string
fieldFormat string
}
// ParameterFormat returns syntax for a parameter declaration
func (t *ReceiveChannel) ParameterFormat() string {
if t.parameterFormat != "" {
return t.parameterFormat
}
t.parameterFormat = fmt.Sprintf("<-chan %s", t.subType.ParameterFormat())
return t.parameterFormat
}
// ReferenceFormat returns the syntax for a reference
func (t *ReceiveChannel) ReferenceFormat() string {
return ""
}
// FieldFormat returns the syntax for a field declaration
func (t *ReceiveChannel) FieldFormat() string {
if t.fieldFormat != "" {
return t.fieldFormat
}
t.fieldFormat = fmt.Sprintf("<-chan %s", t.subType.FieldFormat())
return t.fieldFormat
}
// SendChannel is the built-in send-only channel
type SendChannel struct {
subType Type
parameterFormat string
fieldFormat string
}
// ParameterFormat returns syntax for a parameter declaration
func (t *SendChannel) ParameterFormat() string {
if t.parameterFormat != "" {
return t.parameterFormat
}
t.parameterFormat = fmt.Sprintf("chan<- %s", t.subType.ParameterFormat())
return t.parameterFormat
}
// ReferenceFormat returns the syntax for a reference
func (t *SendChannel) ReferenceFormat() string {
return ""
}
// FieldFormat returns the syntax for a field declaration
func (t *SendChannel) FieldFormat() string {
if t.fieldFormat != "" {
return t.fieldFormat
}
t.fieldFormat = fmt.Sprintf("chan<- %s", t.subType.FieldFormat())
return t.fieldFormat
}