-
Notifications
You must be signed in to change notification settings - Fork 3
/
indexlist.go
60 lines (46 loc) · 1.73 KB
/
indexlist.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
package prefixer
import (
"errors"
"fmt"
"go/ast"
"strings"
)
// indexPrefixer implements Prefixer for IndexListExpr.
type indexListPrefixer struct {
*ast.IndexListExpr
XPrefixer Prefixer
IndicesPrefixers []Prefixer
}
var _ Prefixer = &indexListPrefixer{}
// 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 (i *indexListPrefixer) Valid() error {
errs := make([]error, 0, len(i.Indices)+1)
i.XPrefixer = NewPrefixer(i.X)
errs = append(errs, i.XPrefixer.Valid())
i.IndicesPrefixers = make([]Prefixer, 0, len(i.Indices))
for _, indice := range i.Indices {
prefixer := NewPrefixer(indice)
errs = append(errs, prefixer.Valid())
i.IndicesPrefixers = append(i.IndicesPrefixers, prefixer)
}
return errors.Join(errs...)
}
// ToString transforms a Prefixer (ast.Expr) into its string representation.
// It also returns a boolean indicating whether the type is exported.
func (i *indexListPrefixer) ToString(sourcePackage string, typeParams []string, prefixes ...string) (_ string, _ bool) {
x, xExported := i.XPrefixer.ToString(sourcePackage, typeParams, prefixes...)
exported := true
indices := make([]string, 0, len(i.IndicesPrefixers))
for _, indice := range i.IndicesPrefixers {
stringType, indiceExported := indice.ToString(sourcePackage, typeParams)
// don't affect directly because once exported is false, it should stays even if other fields are exported
if !indiceExported {
exported = false
}
indices = append(indices, stringType)
}
return fmt.Sprintf("%s[%s]", x, strings.Join(indices, ", ")), xExported && exported
}