-
Notifications
You must be signed in to change notification settings - Fork 20
/
type.go
68 lines (54 loc) · 1.26 KB
/
type.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
package typewriter
import (
"fmt"
"regexp"
"strings"
"go/types"
)
type Type struct {
Pointer Pointer
Name string
Tags TagSlice
comparable, numeric, ordered bool
test test
types.Type
}
type test bool
// a convenience for using bool in file name, see WriteAll
func (t test) String() string {
if t {
return "_test"
}
return ""
}
func (t Type) String() (result string) {
return fmt.Sprintf("%s%s", t.Pointer.String(), t.Name)
}
// LongName provides a name that may be useful for generated names.
// For example, map[string]Foo becomes MapStringFoo.
func (t Type) LongName() string {
s := strings.Replace(t.String(), "[]", "Slice[]", -1) // hacktastic
r := regexp.MustCompile(`[\[\]{}*]`)
els := r.Split(s, -1)
var parts []string
for _, s := range els {
parts = append(parts, strings.Title(s))
}
return strings.Join(parts, "")
}
func (t Type) FindTag(tw Interface) (Tag, bool) {
for _, tag := range t.Tags {
if tag.Name == tw.Name() {
return tag, true
}
}
return Tag{}, false
}
// Pointer exists as a type to allow simple use as bool or as String, which returns *
type Pointer bool
func (p Pointer) String() string {
if p {
return "*"
}
return ""
}