-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileParts.go
149 lines (123 loc) · 2.93 KB
/
fileParts.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package lib
import (
"strings"
)
// GoFileImports is a collection of imports for a model file
type GoFileImports []string
// Len returns the length of the collection of imports
func (i *GoFileImports) Len() int {
return len(*i)
}
// Get returns an import at an index
func (i *GoFileImports) Get(idx int) string {
return (*i)[idx]
}
// Append adds to the collection of imports
func (i *GoFileImports) Append(m string) {
*i = append(*i, m)
}
// ToString returns a string representation of imports on a go file
func (i *GoFileImports) ToString() string {
ret := ""
if len(*i) > 0 {
ret += "import ("
for _, m := range *i {
ret += "\n\t" + m
}
ret += "\n)\n"
}
return ret
}
// GoStruct represents a model struct
type GoStruct struct {
Package string
Name string
Fields *GoStructFields
Methods *GoStructMethods
Comments string
Imports *GoFileImports
}
// NewGoStruct returns a new GoStruct
func NewGoStruct() *GoStruct {
return &GoStruct{
Fields: &GoStructFields{},
Imports: &GoFileImports{},
}
}
// GoStructFields is a collection of a go struct fields
type GoStructFields []*GoStructField
// Len returns the length of the collection of imports
func (i *GoStructFields) Len() int {
return len(*i)
}
// Get returns an import at an index
func (i *GoStructFields) Get(idx int) *GoStructField {
return (*i)[idx]
}
// Append adds to the collection of imports
func (i *GoStructFields) Append(m *GoStructField) {
*i = append(*i, m)
}
// GoStructField is a field on a Model struct
type GoStructField struct {
Name string
DataType string
Tags []*GoStructFieldTag
Comments string
}
// ToString returns a tring representation of a go struct field
func (m *GoStructField) ToString() string {
str := m.Name +
" " +
m.DataType +
" "
if len(m.Tags) > 0 {
str += "`"
tags := []string{}
for _, t := range m.Tags {
tags = append(tags, t.ToString())
}
str += strings.Join(tags, " ")
str += "`"
}
if len(m.Comments) > 0 {
str += " // " + m.Comments
} else {
str += "\n"
}
return str
}
// GoStructMethods is a collection of go methods on a struct
type GoStructMethods []*GoStructMethod
// Len returns the number of methods
func (g *GoStructMethods) Len() int {
return len(*g)
}
// Append adds a new struct to the struct collection
func (g *GoStructMethods) Append(m *GoStructMethod) {
*g = append(*g, m)
}
// Get gets a struct at index
func (g *GoStructMethods) Get(index int) (m *GoStructMethod) {
return (*g)[index]
}
// GoStructMethod is a method on a struct
type GoStructMethod struct {
Code string
Docs []string
}
// GoStructFieldTag is a tag on a field on a model struct
type GoStructFieldTag struct {
Name string
Value string
Options []string
}
// ToString returns a string representation of a go struct field tag
func (t *GoStructFieldTag) ToString() string {
str := t.Name + ":\"" + t.Value
if len(t.Options) > 0 {
str += "," + strings.Join(t.Options, ",")
}
str += "\""
return str
}