-
Notifications
You must be signed in to change notification settings - Fork 7
/
scan.go
166 lines (140 loc) · 4.17 KB
/
scan.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package dingo
import (
"encoding/json"
"reflect"
"sort"
"strconv"
"strings"
)
// Scan contains the parsed information about the service definitions.
type Scan struct {
TypeManager *TypeManager
ImportsWithoutParams map[string]string
Defs []*ScannedDef
ProviderPackage string
ProviderName string
}
// ScannedDef contains the parsed information about a service definition.
type ScannedDef struct {
Def *Def
Name string
FormattedName string
Scope string
ObjectType reflect.Type
ObjectTypeString string
BuildIsFunc bool
BuildTypeString string
Params map[string]*ParamInfo
CloseTypeString string
Unshared bool
}
// ParamsString returns the parameters as they should appear
// in a structure inside a go file.
func (def *ScannedDef) ParamsString() string {
if def.BuildIsFunc {
params := make([]string, len(def.Params))
for i := 0; i < len(def.Params); i++ {
params[i] = "p" + strconv.Itoa(i)
}
return strings.Join(params, ", ")
}
params := ""
for _, param := range def.Params {
params += param.Name + `: p` + param.Index + ",\n"
}
return params
}
// BuildDependsOnRawDef returns true if the service constructor
// needs the definition contained in the Provider.
func (def *ScannedDef) BuildDependsOnRawDef() bool {
if def.BuildIsFunc {
return true
}
for _, param := range def.Params {
if param.ServiceName == "" && !param.UndefinedStructParam {
return true
}
}
return false
}
// GenerateCommentScope returns the scope as it should be printed in the generated comments.
func (def *ScannedDef) GenerateCommentScope() string {
if def.Scope == "" {
return "main"
}
return strings.ReplaceAll(def.Scope, "\n", "")
}
// GenerateCommentDescription returns the description as it should be printed in the generated comments.
func (def *ScannedDef) GenerateCommentDescription() string {
if def.Def.Description == "" {
return ""
}
comment := ""
for _, part := range strings.Split(def.Def.Description, "\n") {
comment += "\t\t// " + part + "\n"
}
return comment + "\t\t//\n"
}
// GenerateCommentParams returns the params as they should be printed in the generated comments.
func (def *ScannedDef) GenerateCommentParams() string {
if len(def.Params) == 0 {
return "\t\t// \tparams: nil\n"
}
keys := []string{}
for key := range def.Params {
keys = append(keys, key)
}
sort.Strings(keys)
comment := "\t\t// \tparams:\n"
for _, key := range keys {
p, _ := def.Params[key]
k, _ := json.Marshal(key)
comment += "\t\t// \t\t- " + string(k) + ": "
if p.ServiceName != "" {
name, _ := json.Marshal(p.ServiceName)
comment += "Service(" + strings.ReplaceAll(p.TypeString, "\n", "") + ")"
comment += " [" + string(name) + "]\n"
} else {
comment += "Value(" + strings.ReplaceAll(p.TypeString, "\n", "") + ")\n"
}
}
return comment
}
// GenerateComment returns the text used in the comments of the generated code.
func (def *ScannedDef) GenerateComment() string {
comment := def.GenerateCommentDescription()
name, _ := json.Marshal(def.Name)
scope, _ := json.Marshal(def.GenerateCommentScope())
comment += "\t\t// ---------------------------------------------\n"
comment += "\t\t// \tname: " + string(name) + "\n"
comment += "\t\t// \ttype: " + strings.ReplaceAll(def.ObjectTypeString, "\n", "") + "\n"
comment += "\t\t// \tscope: " + string(scope) + "\n"
if def.BuildIsFunc {
comment += "\t\t// \tbuild: func" + "\n"
} else {
comment += "\t\t// \tbuild: struct" + "\n"
}
comment += def.GenerateCommentParams()
if def.Unshared {
comment += "\t\t// \tunshared: true" + "\n"
} else {
comment += "\t\t// \tunshared: false" + "\n"
}
if def.Def.Close != nil {
comment += "\t\t// \tclose: true" + "\n"
} else {
comment += "\t\t// \tclose: false" + "\n"
}
comment += "\t\t// ---------------------------------------------"
return comment
}
// ParamInfo contains the parsed information about a parameter.
type ParamInfo struct {
Name string
Index string
ServiceName string
Type reflect.Type
TypeString string
UndefinedStructParam bool
Def *ScannedDef
}