forked from clipperhouse/gen
/
type.go
68 lines (53 loc) · 1.42 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 main
import (
"regexp"
"strings"
"github.com/clipperhouse/inflect"
)
type Type struct {
Package *Package
Pointer string
Name string
StandardMethods []string
Projections []*Projection
Containers []string
Imports []string
}
func (t *Type) LocalName() (result string) {
return t.Pointer + t.Name
}
func (t *Type) Plural() (result string) {
result = inflect.Pluralize(t.Name)
if result == t.Name {
result += "s"
}
return
}
func (t *Type) FileName() string {
return strings.ToLower(t.Name) + "_gen.go"
}
func (t *Type) AddProjection(methodName, typeName string) {
t.Projections = append(t.Projections, &Projection{methodName, typeName, t})
}
type Projection struct {
Method string
Type string
Parent *Type
}
func (p *Projection) MethodName() string {
name := p.Type
pointer := regexp.MustCompile(`^\**`)
pointers := len(pointer.FindAllString(name, -1)[0])
name = strings.Replace(name, "*", "", -1) + strings.Repeat("Pointer", pointers)
slice := regexp.MustCompile(`(\[\])`)
slices := len(slice.FindAllString(name, -1))
name = strings.Replace(name, "[]", "", -1) + strings.Repeat("Slice", slices)
illegal := regexp.MustCompile(`[^\p{L}\p{N}]+`)
name = illegal.ReplaceAllString(name, " ")
name = strings.Title(name)
name = strings.Replace(name, " ", "", -1)
return p.Method + strings.Title(name)
}
func (p *Projection) String() string {
return p.MethodName()
}