/
chan.go
38 lines (32 loc) · 1.05 KB
/
chan.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
package prefixer
import (
"go/ast"
)
// chanPrefixer implements Prefixer for ChanType.
type chanPrefixer struct {
*ast.ChanType
ValuePrefixer Prefixer
}
var _ Prefixer = &chanPrefixer{} // ensure interface is implemented
// Valid validates the prefixer and its subprefixers.
//
// An example would be a composition of a StarExpr with an ArrayType of an Ident.
// In that case, all three prefixers computed from those ast.Expr will be validated with Valid.
func (c *chanPrefixer) Valid() error {
c.ValuePrefixer = NewPrefixer(c.Value)
return c.ValuePrefixer.Valid()
}
// ToString transforms a Prefixer (ast.Expr) into its string representation.
// It also returns a boolean indicating whether the type is exported.
func (c *chanPrefixer) ToString(sourcePackage string, typeParams []string, prefixes ...string) (_ string, _ bool) {
prefix := func() string {
if c.Dir == ast.SEND {
return "chan<- "
}
if c.Dir == ast.RECV {
return "<-chan "
}
return "chan "
}()
return c.ValuePrefixer.ToString(sourcePackage, typeParams, append(prefixes, prefix)...)
}