FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stackrox/central/graphql/generator/codegen/codegen.go at master · stackrox/stackrox · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stackrox
/
stackrox
Public
Notifications
You must be signed in to change notification settings
Fork
191
Star
1.3k
Code
Issues
54
Pull requests
603
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
stackrox
/
central
/
graphql
/
generator
/
codegen
/
codegen.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
171 lines (161 loc) · 4.69 KB
Breadcrumbs
stackrox
/
central
/
graphql
/
generator
/
codegen
/
codegen.go
Copy path
File metadata and controls
171 lines (161 loc) · 4.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
codegen
import
(
"bytes"
_
"embed"
"fmt"
"io"
"reflect"
"strings"
"text/template"
"github.com/stackrox/rox/central/graphql/generator"
"github.com/stackrox/rox/pkg/protocompat"
"github.com/stackrox/rox/pkg/set"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
//go:embed codegen.go.tpl
var
codegen
string
var
templates
=
map
[
string
]
string
{
"codegen"
:
codegen
,
"enum"
:
`value.String()`
,
"enumslice"
:
`stringSlice(value)`
,
"float"
:
`float64(value)`
,
"id"
:
`graphql.ID(value)`
,
"int"
:
`int32(value)`
,
"label"
:
`labelsResolver(value)`
,
"pointer"
:
`resolver.root.wrap{{.Type.Elem.Name}}(value, true, nil)`
,
"pointerslice"
:
`resolver.root.wrap{{plural .Type.Elem.Elem.Name}}(value, nil)`
,
"raw"
:
`value`
,
"rawslice"
:
`value`
,
"time"
:
`protocompat.ConvertTimestampToGraphqlTimeOrError(value)`
,
"bytes"
:
`base64.StdEncoding.EncodeToString(value)`
,
}
func
listName
(
td
typeData
)
string
{
split
:=
strings
.
Split
(
td
.
Package
,
"/"
)
return
fmt
.
Sprintf
(
"%s.List%s"
,
split
[
len
(
split
)
-
1
],
td
.
Name
)
}
func
isEnum
(
p
reflect.
Type
)
bool
{
if
p
==
nil
{
return
false
}
fullName
:=
protoreflect
.
FullName
(
strings
.
ReplaceAll
(
importedName
(
p
),
"_"
,
"."
))
_
,
err
:=
protoregistry
.
GlobalTypes
.
FindEnumByName
(
fullName
)
return
err
==
nil
}
func
getFieldTransform
(
fd
fieldData
) (
templateName
string
,
returnType
string
) {
switch
fd
.
Type
.
Kind
() {
case
reflect
.
String
:
if
fd
.
Name
==
"Id"
{
return
"id"
,
"graphql.ID"
}
return
"raw"
,
"string"
case
reflect
.
Int32
:
if
isEnum
(
fd
.
Type
) {
return
"enum"
,
"string"
}
return
"raw"
,
"int32"
case
reflect
.
Uint32
:
return
"int"
,
"int32"
case
reflect
.
Int64
:
return
"int"
,
"int32"
case
reflect
.
Float32
:
return
"float"
,
"float64"
case
reflect
.
Float64
:
return
"raw"
,
"float64"
case
reflect
.
Bool
:
return
"raw"
,
"bool"
case
reflect
.
Uint8
:
return
"raw"
,
"byte"
case
reflect
.
Map
:
if
fd
.
Type
.
Key
().
Kind
()
==
reflect
.
String
&&
fd
.
Type
.
Elem
().
Kind
()
==
reflect
.
String
{
return
"label"
,
"labels"
}
case
reflect
.
Pointer
:
if
fd
.
Type
==
protocompat
.
TimestampPtrType
{
return
"time"
,
"(*graphql.Time, error)"
}
if
fd
.
Type
.
Implements
(
messageType
) {
if
isListType
(
fd
.
Type
) {
// if a field returns a list type, we don't automatically handle this for now.
return
""
,
""
}
return
"pointer"
,
fmt
.
Sprintf
(
"(*%sResolver, error)"
,
lower
(
fd
.
Type
.
Elem
().
Name
()))
}
case
reflect
.
Slice
:
// Handle []byte (protobuf bytes) as base64-encoded strings
if
fd
.
Type
.
Elem
().
Kind
()
==
reflect
.
Uint8
{
return
"bytes"
,
"string"
}
template
,
ret
:=
getFieldTransform
(
fieldData
{
Name
:
fd
.
Name
,
Type
:
fd
.
Type
.
Elem
()})
if
len
(
ret
)
>
0
&&
ret
[
0
]
==
'('
{
// this converts (*fooResolver, error) into ([]*fooResolver, error)
return
template
+
"slice"
,
ret
[
0
:
1
]
+
"[]"
+
ret
[
1
:]
}
return
template
+
"slice"
,
"[]"
+
ret
}
return
""
,
""
}
func
translator
(
t
*
template.
Template
)
func
(
fieldData
)
string
{
return
func
(
fd
fieldData
)
string
{
tmplName
,
_
:=
getFieldTransform
(
fd
)
b
:=
&
bytes.
Buffer
{}
err
:=
t
.
ExecuteTemplate
(
b
,
tmplName
,
fd
)
if
err
!=
nil
{
panic
(
err
)
}
return
b
.
String
()
}
}
func
valueType
(
fd
fieldData
)
string
{
_
,
returnType
:=
getFieldTransform
(
fd
)
return
returnType
}
func
listField
(
td
schemaEntry
,
field
fieldData
)
bool
{
t
,
ok
:=
td
.
ListData
[
field
.
Name
]
return
ok
&&
t
==
field
.
Type
}
// GenerateResolvers produces go code for resolvers for all the types found by the typewalk.
func
GenerateResolvers
(
parameters
generator.
TypeWalkParameters
,
writer
io.
Writer
) {
typeData
:=
typeWalk
(
parameters
)
rootTemplate
:=
template
.
New
(
"codegen"
)
rootTemplate
.
Funcs
(template.
FuncMap
{
"importedName"
:
importedName
,
"isEnum"
:
isEnum
,
"listField"
:
listField
,
"listName"
:
listName
,
"lower"
:
lower
,
"nonListField"
:
func
(
td
schemaEntry
,
field
fieldData
)
bool
{
return
!
listField
(
td
,
field
) },
"plural"
:
plural
,
"translator"
:
translator
(
rootTemplate
),
"valueType"
:
valueType
,
"schemaType"
:
schemaType
,
})
for
name
,
text
:=
range
templates
{
thisTemplate
:=
rootTemplate
if
rootTemplate
.
Name
()
!=
name
{
thisTemplate
=
rootTemplate
.
New
(
name
)
}
_
,
err
:=
thisTemplate
.
Parse
(
text
)
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"Template %q: %s"
,
name
,
err
))
}
}
imports
:=
set
.
NewStringSet
()
for
_
,
td
:=
range
typeData
{
// Input types are not directly referenced in the generated Go code.
if
td
.
IsInputType
{
continue
}
if
td
.
Package
!=
""
{
imports
.
Add
(
td
.
Package
)
}
}
entries
:=
makeSchemaEntries
(
typeData
)
err
:=
rootTemplate
.
Execute
(
writer
,
struct
{
Entries
[]
schemaEntry
Imports
map
[
string
]
struct
{}
}{
entries
,
imports
})
if
err
!=
nil
{
panic
(
err
)
}
}
Back
|
FazBrowse Home
|
New Git URL