/
graph.go
256 lines (226 loc) · 6.69 KB
/
graph.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package hsc
import (
"fmt"
"strings"
"sync"
"github.com/marcozac/hubspot-go"
"golang.org/x/tools/go/packages"
)
type Graph struct {
// OutDir is the output directory for the generated files.
//
// Default: "."
OutDir string
// PackageName is the name of the package for the generated files.
//
// Default: "hubspot"
PackageName string
// PackagePath is the path of the package for the generated files.
//
// Example: "github.com/marcozac/hubspot-go"
PackagePath string
// Imports is a map formatted as "path:alias" for the imports of the graph.
Imports map[string]string
// Objects is a list of objects of the node.
Objects []*Object
// Header is the header for the generated files.
//
// Default: "Code generated by hsc. DO NOT EDIT."
Header string
// importHS is true if the types should imported from the hubspot-go package
importHS bool
mu sync.RWMutex
}
// ImportHS returns true if the types should imported from the hubspot-go package
func (g *Graph) ImportHS() bool {
return g.importHS
}
func (g *Graph) Init() error {
if g.OutDir == "" {
g.OutDir = "."
}
if g.PackageName == "" {
g.PackageName = "hubspot"
}
if g.Header == "" {
g.Header = "Code generated by hsc. DO NOT EDIT."
}
return g.resolveImports()
}
func (g *Graph) resolveImports() error {
if g.Imports == nil {
g.Imports = make(map[string]string)
}
g.importHS = g.PackagePath != "github.com/marcozac/hubspot-go"
if g.importHS {
g.Imports["github.com/marcozac/hubspot-go"] = "hubspot"
}
for _, o := range g.Objects {
o.importHS = g.importHS
if g.importHS {
for _, p := range o.Properties {
p.importHS = true
}
}
if o.EndpointTarget != nil {
g.mu.RLock()
alias, ok := g.Imports[o.EndpointTarget.Package]
g.mu.RUnlock()
if !ok {
g.mu.Lock()
pkgs, err := packages.Load(&packages.Config{}, o.EndpointTarget.Package)
if err != nil {
return fmt.Errorf("load packages: %w", err)
}
if l := len(pkgs); l == 0 || l > 1 {
return fmt.Errorf("unexpected number of packages: %d", l)
}
pkg := pkgs[0]
var i int
alias = pkg.Name
importsLoop:
for _, a := range g.Imports {
if alias == a {
i++
alias = fmt.Sprintf("%s_%d", pkg.Name, i)
goto importsLoop // restart the loop and check the new alias
}
}
g.Imports[o.EndpointTarget.Package] = alias
g.mu.Unlock()
}
o.EndpointTarget.alias = alias // update the alias of the endpoint target
}
}
return nil
}
type Object struct {
// Name is the name of the object.
//
// Example: "Contact"
Name string
// Properties is a list of properties of the object.
Properties []*Property
// Endpoint is the endpoint of the object.
//
// Example: "https://api.hubapi.com/crm/v3/objects/contacts"
Endpoint string
// EndpointTarget is the target endpoint for the object.
//
// It is used if [Object.Endpoint] is empty.
EndpointTarget *EndpointTarget
// importHS is true if the types should imported from the hubspot-go package
importHS bool
}
// ImportHS returns true if the types should imported from the hubspot-go package
func (o *Object) ImportHS() bool {
return o.importHS
}
type Property struct {
*hubspot.Property
fieldName string
structFieldType string
// importHS is true if the types should imported from the hubspot-go package
importHS bool
}
// ImportHS returns true if the types should imported from the hubspot-go package
func (p *Property) ImportHS() bool {
return p.importHS
}
// IsDefault returns true if the property is a HubSpot default property.
func (p Property) IsDefault() bool {
return p.CreatedUserId == ""
}
func (p *Property) StructFieldName() string {
if p.fieldName == "" {
var sb strings.Builder
ss := strings.Split(p.Name, "_")
for _, s := range ss {
if len(s) == 0 {
// Keep the underscore if the string is empty.
// It happens when the property name is, for example,
// "hs__something". The underscore is kept to avoid
// collisions with other properties.
sb.WriteString("_")
continue
}
sb.WriteString(strings.ToUpper(s[:1]))
if len(s) > 1 {
sb.WriteString(s[1:])
}
}
p.fieldName = sb.String()
}
return p.fieldName
}
func (p *Property) StructFieldType() string {
if p.structFieldType == "" {
if p.importHS {
p.structFieldType = "hubspot."
}
switch p.Type {
case hubspot.PropertyTypeString, hubspot.PropertyTypePhoneNumber:
p.structFieldType = "string" // override the import (if any)
case hubspot.PropertyTypeNumber:
// Set the type to Int64 for the hs_object_id property.
//
// Currently HubSpot uses int64 for Contact ID, this covers also the future case
// where they might use int64 for other properties.
//
// See: https://developers.hubspot.com/changelog/increasing-the-size-of-contact-record-ids
if p.Name == "hs_object_id" {
p.structFieldType += "Int64"
} else {
p.structFieldType += "Int"
}
case hubspot.PropertyTypeBool:
p.structFieldType += "Bool"
case hubspot.PropertyTypeDate:
// must to be a pointer to respect the omitempty tag
p.structFieldType = "*" + p.structFieldType + "Date"
case hubspot.PropertyTypeDateTime:
// must to be a pointer to respect the omitempty tag
p.structFieldType = "*" + p.structFieldType + "DateTime"
case hubspot.PropertyTypeEnumeration:
switch p.FieldType {
case hubspot.PropertyFieldTypeRadio, hubspot.PropertyFieldTypeSelect, hubspot.PropertyFieldTypeCalculationEquation:
p.structFieldType = "string"
case hubspot.PropertyFieldTypeBooleanCheckbox:
p.structFieldType += "Bool"
case hubspot.PropertyFieldTypeNumber:
p.structFieldType += "Int"
case hubspot.PropertyFieldTypeCheckbox, hubspot.PropertyFieldTypeCalculationRollup:
p.structFieldType += "Enumeration"
default:
panic(fmt.Errorf("unknown property field type: %s", p.FieldType))
}
default:
panic(fmt.Errorf("unknown property type: %s", p.Type))
}
}
return p.structFieldType
}
func (p Property) StructFieldJSONTag() string {
return fmt.Sprintf("`json:\"%s,omitempty\"`", p.Name)
}
type EndpointTarget struct {
// Name is the name of the endpoint target.
//
// It must be equal to an exported name in the endpoint package.
//
// Example: "Contacts" => github.com/marcozac/hubspot-go/endpoint.Contacts
Name string
// Package is the package of the endpoint target.
//
// Example: "github.com/marcozac/hubspot-go/endpoint"
Package string
// alias is the alias of the endpoint target used for imports.
alias string
}
// Alias returns the alias of the endpoint target used for imports.
//
// Since the alias may be altered by [Graph], this method should be called only
// after [Graph.resolveImports] has been called or (better) in a template.
func (et *EndpointTarget) Alias() string {
return et.alias
}