Skip to content

Commit f519294

Browse files
committed
✨ Inject empty string check into link followers
1 parent 2d9e778 commit f519294

3 files changed

Lines changed: 130 additions & 1 deletion

File tree

changes/20250409181749.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Inject empty string check into link followers

generator/codegen/linkfollower_extensions.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,55 @@ func getReceiverType(fn *ast.FuncDecl) (receiver string, err error) {
259259
return
260260
}
261261

262+
func createEmptyLinkCheck() *ast.IfStmt {
263+
// link == ""
264+
cond := &ast.BinaryExpr{
265+
X: &ast.Ident{Name: "link"},
266+
Op: token.EQL,
267+
Y: &ast.BasicLit{Kind: token.STRING, Value: "\"\""},
268+
}
269+
270+
// errors.New("empty link")
271+
callExpr := &ast.CallExpr{
272+
Fun: &ast.SelectorExpr{
273+
X: &ast.Ident{Name: "errors"},
274+
Sel: &ast.Ident{Name: "New"},
275+
},
276+
Args: []ast.Expr{
277+
&ast.BasicLit{Kind: token.STRING, Value: "\"empty link\""},
278+
},
279+
}
280+
281+
// return nil, nil, errors.New("empty link")
282+
retStmt := &ast.ReturnStmt{
283+
Results: []ast.Expr{
284+
&ast.Ident{Name: "nil"},
285+
&ast.Ident{Name: "nil"},
286+
callExpr,
287+
},
288+
}
289+
290+
// { return nil, nil, errors.New("empty link") }
291+
block := &ast.BlockStmt{
292+
List: []ast.Stmt{retStmt},
293+
}
294+
295+
// if link == "" {
296+
// return nil, nil, errors.New("empty link")
297+
// }
298+
ifStmt := &ast.IfStmt{
299+
Cond: cond,
300+
Body: block,
301+
}
302+
303+
return ifStmt
304+
}
305+
262306
func modifyLocalVarPath(fn *ast.FuncDecl) (err error) {
263307
// This function looks for a definition of localVarPath, `localVarPath := localBasePath + "path"`,
264308
// and replaces the `"path"` part with `link`.
265309
// ex: localVarPath := localBasePath + "/build-jobs/" -> `localVarPath := localBasePath + link`
266-
for _, stmt := range fn.Body.List {
310+
for i, stmt := range fn.Body.List {
267311
assign, ok := stmt.(*ast.AssignStmt)
268312
if !ok {
269313
continue
@@ -272,6 +316,14 @@ func modifyLocalVarPath(fn *ast.FuncDecl) (err error) {
272316
if assign.Tok == token.DEFINE && len(assign.Lhs) > 0 {
273317
if ident, ok := assign.Lhs[0].(*ast.Ident); ok && ident.Name == "localVarPath" {
274318
if expr, ok := assign.Rhs[0].(*ast.BinaryExpr); ok && len(assign.Rhs) > 0 {
319+
// Add empty string check before modification
320+
newBody := make([]ast.Stmt, 0, len(fn.Body.List)+1)
321+
newBody = append(newBody, fn.Body.List[:i]...)
322+
newBody = append(newBody, createEmptyLinkCheck())
323+
newBody = append(newBody, fn.Body.List[i:]...)
324+
fn.Body.List = newBody
325+
326+
// Modify the line
275327
expr.Y = ast.NewIdent(resourcePath)
276328
}
277329

generator/codegen/testdata/linkfollowers/test.gen.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package client
77

88
import (
99
"bytes"
10+
"errors"
1011
"io"
1112
"net/http"
1213
"net/url"
@@ -34,6 +35,9 @@ func (a *BuildJobsAPIService) FollowGetBuildMessagesLink(r *ApiGetBuildMessagesR
3435
if err != nil {
3536
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
3637
}
38+
if link == "" {
39+
return nil, nil, errors.New("empty link")
40+
}
3741

3842
localVarPath := localBasePath + link
3943
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -186,6 +190,9 @@ func (a *BuildJobsAPIService) FollowBuildJobLink(r *ApiListBuildJobRequest, link
186190
if err != nil {
187191
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
188192
}
193+
if link == "" {
194+
return nil, nil, errors.New("empty link")
195+
}
189196

190197
localVarPath := localBasePath + link
191198

@@ -332,6 +339,9 @@ func (a *BuildJobsAPIService) FollowBuildOutputManagersLink(r *ApiListBuildOutpu
332339
if err != nil {
333340
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
334341
}
342+
if link == "" {
343+
return nil, nil, errors.New("empty link")
344+
}
335345

336346
localVarPath := localBasePath + link
337347
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -490,6 +500,9 @@ func (a *CMSISBuildersAPIService) FollowCmsisBuildersLink(r *ApiListCmsisBuilder
490500
if err != nil {
491501
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
492502
}
503+
if link == "" {
504+
return nil, nil, errors.New("empty link")
505+
}
493506

494507
localVarPath := localBasePath + link
495508

@@ -636,6 +649,9 @@ func (a *CMSISIntellisenseBuildersAPIService) FollowCmsisIntellisenseLink(r *Api
636649
if err != nil {
637650
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
638651
}
652+
if link == "" {
653+
return nil, nil, errors.New("empty link")
654+
}
639655

640656
localVarPath := localBasePath + link
641657

@@ -782,6 +798,9 @@ func (a *FPGAConnectionAPIService) FollowFpgaConnectionsLink(r *ApiListFpgaConne
782798
if err != nil {
783799
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
784800
}
801+
if link == "" {
802+
return nil, nil, errors.New("empty link")
803+
}
785804

786805
localVarPath := localBasePath + link
787806

@@ -928,6 +947,9 @@ func (a *FPGAJobsAPIService) FollowGetFpgaJobMessagesLink(r *ApiGetFpgaJobMessag
928947
if err != nil {
929948
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
930949
}
950+
if link == "" {
951+
return nil, nil, errors.New("empty link")
952+
}
931953

932954
localVarPath := localBasePath + link
933955
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -1080,6 +1102,9 @@ func (a *FPGAJobsAPIService) FollowFpgaJobArtefactManagersLink(r *ApiListFpgaJob
10801102
if err != nil {
10811103
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
10821104
}
1105+
if link == "" {
1106+
return nil, nil, errors.New("empty link")
1107+
}
10831108

10841109
localVarPath := localBasePath + link
10851110
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -1238,6 +1263,9 @@ func (a *FPGAPayloadsAPIService) FollowPayloadsLink(r *ApiListPayloadsRequest, l
12381263
if err != nil {
12391264
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
12401265
}
1266+
if link == "" {
1267+
return nil, nil, errors.New("empty link")
1268+
}
12411269

12421270
localVarPath := localBasePath + link
12431271

@@ -1384,6 +1412,9 @@ func (a *GenericWorkJobsAPIService) FollowGetGenericWorkJobMessagesLink(r *ApiGe
13841412
if err != nil {
13851413
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
13861414
}
1415+
if link == "" {
1416+
return nil, nil, errors.New("empty link")
1417+
}
13871418

13881419
localVarPath := localBasePath + link
13891420
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -1536,6 +1567,9 @@ func (a *GenericWorkJobsAPIService) FollowGenericWorkJobArtefactManagersLink(r *
15361567
if err != nil {
15371568
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
15381569
}
1570+
if link == "" {
1571+
return nil, nil, errors.New("empty link")
1572+
}
15391573

15401574
localVarPath := localBasePath + link
15411575
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -1694,6 +1728,9 @@ func (a *GenericWorkJobsAPIService) FollowGenericWorkJobsLink(r *ApiListGenericW
16941728
if err != nil {
16951729
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
16961730
}
1731+
if link == "" {
1732+
return nil, nil, errors.New("empty link")
1733+
}
16971734

16981735
localVarPath := localBasePath + link
16991736

@@ -1840,6 +1877,9 @@ func (a *GenericWorkersAPIService) FollowGenericWorkersLink(r *ApiListGenericWor
18401877
if err != nil {
18411878
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
18421879
}
1880+
if link == "" {
1881+
return nil, nil, errors.New("empty link")
1882+
}
18431883

18441884
localVarPath := localBasePath + link
18451885

@@ -1986,6 +2026,9 @@ func (a *IntellisenseJobsAPIService) FollowGetIntellisenseMessagesLink(r *ApiGet
19862026
if err != nil {
19872027
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
19882028
}
2029+
if link == "" {
2030+
return nil, nil, errors.New("empty link")
2031+
}
19892032

19902033
localVarPath := localBasePath + link
19912034
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -2138,6 +2181,9 @@ func (a *IntellisenseJobsAPIService) FollowIntellisenseJobLink(r *ApiListIntelli
21382181
if err != nil {
21392182
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
21402183
}
2184+
if link == "" {
2185+
return nil, nil, errors.New("empty link")
2186+
}
21412187

21422188
localVarPath := localBasePath + link
21432189

@@ -2284,6 +2330,9 @@ func (a *IntellisenseJobsAPIService) FollowIntellisenseOutputManagersLink(r *Api
22842330
if err != nil {
22852331
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
22862332
}
2333+
if link == "" {
2334+
return nil, nil, errors.New("empty link")
2335+
}
22872336

22882337
localVarPath := localBasePath + link
22892338
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -2442,6 +2491,9 @@ func (a *VHTRunJobsAPIService) FollowGetVhtRunJobMessagesLink(r *ApiGetVhtRunJob
24422491
if err != nil {
24432492
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
24442493
}
2494+
if link == "" {
2495+
return nil, nil, errors.New("empty link")
2496+
}
24452497

24462498
localVarPath := localBasePath + link
24472499
localVarPath = strings.Replace(localVarPath, "{"+"jobName"+"}", parameterValueToString(r.jobName, "jobName"), -1)
@@ -2594,6 +2646,9 @@ func (a *VHTRunJobsAPIService) FollowVhtRunJobsLink(r *ApiListVhtRunJobsRequest,
25942646
if err != nil {
25952647
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
25962648
}
2649+
if link == "" {
2650+
return nil, nil, errors.New("empty link")
2651+
}
25972652

25982653
localVarPath := localBasePath + link
25992654

@@ -2740,6 +2795,9 @@ func (a *VirtualHardwareTargetAPIService) FollowFilteredVhtInstancesLink(r *ApiL
27402795
if err != nil {
27412796
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
27422797
}
2798+
if link == "" {
2799+
return nil, nil, errors.New("empty link")
2800+
}
27432801

27442802
localVarPath := localBasePath + link
27452803
localVarPath = strings.Replace(localVarPath, "{"+"vhtName"+"}", parameterValueToString(r.vhtName, "vhtName"), -1)
@@ -2887,6 +2945,9 @@ func (a *VirtualHardwareTargetAPIService) FollowVhtsLink(r *ApiListVhtsRequest,
28872945
if err != nil {
28882946
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
28892947
}
2948+
if link == "" {
2949+
return nil, nil, errors.New("empty link")
2950+
}
28902951

28912952
localVarPath := localBasePath + link
28922953

@@ -3033,6 +3094,9 @@ func (a *VirtualHardwareTargetInstanceAPIService) FollowGetVhtInstanceMessagesLi
30333094
if err != nil {
30343095
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
30353096
}
3097+
if link == "" {
3098+
return nil, nil, errors.New("empty link")
3099+
}
30363100

30373101
localVarPath := localBasePath + link
30383102
localVarPath = strings.Replace(localVarPath, "{"+"instanceName"+"}", parameterValueToString(r.instanceName, "instanceName"), -1)
@@ -3185,6 +3249,9 @@ func (a *VirtualHardwareTargetInstanceAPIService) FollowVhtInstanceArtefactManag
31853249
if err != nil {
31863250
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
31873251
}
3252+
if link == "" {
3253+
return nil, nil, errors.New("empty link")
3254+
}
31883255

31893256
localVarPath := localBasePath + link
31903257
localVarPath = strings.Replace(localVarPath, "{"+"instanceName"+"}", parameterValueToString(r.instanceName, "instanceName"), -1)
@@ -3343,6 +3410,9 @@ func (a *VirtualHardwareTargetInstanceAPIService) FollowVhtInstancesLink(r *ApiL
33433410
if err != nil {
33443411
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
33453412
}
3413+
if link == "" {
3414+
return nil, nil, errors.New("empty link")
3415+
}
33463416

33473417
localVarPath := localBasePath + link
33483418

@@ -3489,6 +3559,9 @@ func (a *WorkspaceAPIService) FollowWorkspacesLink(r *ApiListWorkspacesRequest,
34893559
if err != nil {
34903560
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
34913561
}
3562+
if link == "" {
3563+
return nil, nil, errors.New("empty link")
3564+
}
34923565

34933566
localVarPath := localBasePath + link
34943567

@@ -3635,6 +3708,9 @@ func (a *WorkspaceSourceAPIService) FollowWorkspaceSourcesLink(r *ApiListWorkspa
36353708
if err != nil {
36363709
return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
36373710
}
3711+
if link == "" {
3712+
return nil, nil, errors.New("empty link")
3713+
}
36383714

36393715
localVarPath := localBasePath + link
36403716

0 commit comments

Comments
 (0)