Skip to content

Commit d8fc45d

Browse files
committed
refactor: clean up code by adding missing line breaks and comments for clarity
1 parent 52f9c46 commit d8fc45d

16 files changed

Lines changed: 132 additions & 34 deletions

gateway/client.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func (c *Client) WatchServices(ctx context.Context, serviceName string, onChange
165165
// Route table unchanged — update cache but skip remounting
166166
c.manifestCache[event.Manifest.InstanceID] = event.Manifest
167167
c.mu.Unlock()
168+
168169
return
169170
}
170171
}
@@ -196,8 +197,10 @@ func (c *Client) WatchServices(ctx context.Context, serviceName string, onChange
196197
if newHash == c.currentRoutesHash {
197198
// Routes unchanged — skip remounting
198199
c.mu.Unlock()
200+
199201
return
200202
}
203+
201204
c.currentRoutesHash = newHash
202205
c.mu.Unlock()
203206

@@ -229,6 +232,7 @@ func (c *Client) WatchServicesAtomic(ctx context.Context, serviceName string, ha
229232

230233
if err := handler.CommitRoutes(); err != nil {
231234
_ = handler.RollbackRoutes()
235+
232236
return fmt.Errorf("failed to commit initial routes: %w", err)
233237
}
234238

@@ -248,6 +252,7 @@ func (c *Client) WatchServicesAtomic(ctx context.Context, serviceName string, ha
248252
old.RoutesChecksum == event.Manifest.RoutesChecksum {
249253
c.manifestCache[event.Manifest.InstanceID] = event.Manifest
250254
c.mu.Unlock()
255+
251256
return
252257
}
253258
}
@@ -275,8 +280,10 @@ func (c *Client) WatchServicesAtomic(ctx context.Context, serviceName string, ha
275280
c.mu.Lock()
276281
if newHash == c.currentRoutesHash {
277282
c.mu.Unlock()
283+
278284
return
279285
}
286+
280287
c.currentRoutesHash = newHash
281288
c.mu.Unlock()
282289

@@ -302,6 +309,7 @@ func (c *Client) convertToRouteDescriptors(manifests []*farp.SchemaManifest) []f
302309
// Prefer pre-computed route table if available
303310
if len(manifest.RouteTable) > 0 {
304311
routes = append(routes, manifest.RouteTable...)
312+
305313
continue
306314
}
307315

@@ -339,7 +347,7 @@ func computeRouteTableHash(routes []ServiceRoute) string {
339347
return entries[i].Path < entries[j].Path
340348
})
341349

342-
data, _ := json.Marshal(entries)
350+
data, _ := json.Marshal(entries) //nolint:errchkjson // simple struct cannot fail to marshal
343351
hash := sha256.Sum256(data)
344352

345353
return hex.EncodeToString(hash[:])
@@ -365,10 +373,11 @@ func computeRouteDescriptorHash(routes []farp.RouteDescriptor) string {
365373
if entries[i].Path != entries[j].Path {
366374
return entries[i].Path < entries[j].Path
367375
}
376+
368377
return entries[i].Protocol < entries[j].Protocol
369378
})
370379

371-
data, _ := json.Marshal(entries)
380+
data, _ := json.Marshal(entries) //nolint:errchkjson // simple struct cannot fail to marshal
372381
hash := sha256.Sum256(data)
373382

374383
return hex.EncodeToString(hash[:])
@@ -479,7 +488,7 @@ func (c *Client) fetchSchema(ctx context.Context, descriptor *farp.SchemaDescrip
479488
}
480489

481490
// Execute request
482-
resp, err := c.httpClient.Do(req)
491+
resp, err := c.httpClient.Do(req) //nolint:gosec // URL comes from service manifest, not user input
483492
if err != nil {
484493
return nil, fmt.Errorf("failed to fetch schema from URL %s: %w", descriptor.Location.URL, err)
485494
}
@@ -849,7 +858,7 @@ func computeManifestsHash(cache map[string]*farp.SchemaManifest) string {
849858
return entries[i].ID < entries[j].ID
850859
})
851860

852-
data, _ := json.Marshal(entries)
861+
data, _ := json.Marshal(entries) //nolint:errchkjson // simple struct cannot fail to marshal
853862
hash := sha256.Sum256(data)
854863

855864
return hex.EncodeToString(hash[:])

gateway/client_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ func TestClient_MultiSchemaManifest(t *testing.T) {
12331233
switch schemaType {
12341234
case "openapi":
12351235
hasREST = true
1236+
12361237
if route.Path != "/api/users" {
12371238
t.Errorf("REST route path = %v, want /api/users", route.Path)
12381239
}
@@ -1242,6 +1243,7 @@ func TestClient_MultiSchemaManifest(t *testing.T) {
12421243
}
12431244
case "asyncapi":
12441245
hasWebSocket = true
1246+
12451247
if route.Path != "/ws/events" {
12461248
t.Errorf("WebSocket route path = %v, want /ws/events", route.Path)
12471249
}

gateway/federated.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ func (h *FederatedSchemaHandler) ensureFresh(r *http.Request) error {
9090
currentHash := h.client.GetManifestsHash()
9191

9292
h.cache.mu.RLock()
93+
9394
if h.cache.manifestsHash == currentHash && h.cache.result != nil {
9495
h.cache.mu.RUnlock()
96+
9597
return nil
9698
}
99+
97100
h.cache.mu.RUnlock()
98101

99102
// Re-merge needed
@@ -151,6 +154,7 @@ func (h *FederatedSchemaHandler) ensureFresh(r *http.Request) error {
151154
func (h *FederatedSchemaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
152155
if r.Method != http.MethodGet {
153156
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
157+
154158
return
155159
}
156160

@@ -171,8 +175,8 @@ func (h *FederatedSchemaHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
171175
w.Header().Set("Content-Type", "application/json")
172176
w.WriteHeader(http.StatusInternalServerError)
173177

174-
errResp, _ := json.Marshal(map[string]string{"error": err.Error()})
175-
w.Write(errResp)
178+
errResp, _ := json.Marshal(map[string]string{"error": err.Error()}) //nolint:errchkjson // error response marshaling cannot fail
179+
_, _ = w.Write(errResp)
176180

177181
return
178182
}
@@ -207,13 +211,13 @@ func (h *FederatedSchemaHandler) serveJSON(w http.ResponseWriter, data []byte) {
207211

208212
if len(data) == 0 {
209213
w.WriteHeader(http.StatusNotFound)
210-
w.Write([]byte(`{"error":"no schemas available for this protocol"}`))
214+
_, _ = w.Write([]byte(`{"error":"no schemas available for this protocol"}`))
211215

212216
return
213217
}
214218

215219
w.WriteHeader(http.StatusOK)
216-
w.Write(data)
220+
_, _ = w.Write(data)
217221
}
218222

219223
// federatedSummary is the JSON structure for the /summary endpoint.
@@ -257,7 +261,7 @@ func (h *FederatedSchemaHandler) serveSummary(w http.ResponseWriter) {
257261
w.Header().Set("X-Farp-Federated", "true")
258262
w.WriteHeader(http.StatusOK)
259263

260-
json.NewEncoder(w).Encode(summary)
264+
_ = json.NewEncoder(w).Encode(summary) //nolint:errchkjson // best-effort HTTP response write
261265
}
262266

263267
// Invalidate forces the next request to rebuild the federated schemas.

gateway/federated_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func TestFederatedHandler_OpenAPIEndpoint(t *testing.T) {
121121

122122
// Verify it's valid OpenAPI JSON
123123
body, _ := io.ReadAll(resp.Body)
124+
124125
var spec merger.OpenAPISpec
125126
if err := json.Unmarshal(body, &spec); err != nil {
126127
t.Fatalf("Failed to unmarshal OpenAPI JSON: %v", err)

manifest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func CalculateRoutesChecksum(manifest *SchemaManifest) (string, error) {
378378
if sortedRoutes[i].Path != sortedRoutes[j].Path {
379379
return sortedRoutes[i].Path < sortedRoutes[j].Path
380380
}
381+
381382
return sortedRoutes[i].Protocol < sortedRoutes[j].Protocol
382383
})
383384

merger/merger.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,12 @@ func rewritePathItemRefs(item PathItem, prefix string) PathItem {
486486
if schema == nil {
487487
return nil
488488
}
489+
489490
rewritten := RewriteRefs(schema, prefix)
490491
if m, ok := rewritten.(map[string]any); ok {
491492
return m
492493
}
494+
493495
return schema
494496
}
495497

@@ -504,14 +506,17 @@ func rewritePathItemRefs(item PathItem, prefix string) PathItem {
504506
if op == nil {
505507
return
506508
}
509+
507510
for code, resp := range op.Responses {
508511
rewriteMediaTypes(resp.Content)
509512
op.Responses[code] = resp
510513
}
514+
511515
for i, param := range op.Parameters {
512516
param.Schema = rewriteSchema(param.Schema)
513517
op.Parameters[i] = param
514518
}
519+
515520
if op.RequestBody != nil {
516521
rewriteMediaTypes(op.RequestBody.Content)
517522
}

merger/merger_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ func TestMerger_Merge_CollapseServiceTags(t *testing.T) {
511511
// Should have exactly 1 tag: "TwinOS"
512512
if len(result.Spec.Tags) != 1 {
513513
t.Errorf("expected 1 collapsed tag, got %d", len(result.Spec.Tags))
514+
514515
for _, tag := range result.Spec.Tags {
515516
t.Logf(" tag: %s", tag.Name)
516517
}
@@ -527,6 +528,7 @@ func TestMerger_Merge_CollapseServiceTags(t *testing.T) {
527528
if op == nil {
528529
continue
529530
}
531+
530532
if len(op.Tags) != 1 || op.Tags[0] != "TwinOS" {
531533
t.Errorf("operation at %s should have tags [TwinOS], got %v", path, op.Tags)
532534
}
@@ -575,6 +577,7 @@ func TestMerger_Merge_CollapseServiceTags_MultipleServices(t *testing.T) {
575577
// Should have exactly 2 tags: "TwinOS" and "Portal"
576578
if len(result.Spec.Tags) != 2 {
577579
t.Errorf("expected 2 collapsed tags, got %d", len(result.Spec.Tags))
580+
578581
for _, tag := range result.Spec.Tags {
579582
t.Logf(" tag: %s", tag.Name)
580583
}
@@ -588,6 +591,7 @@ func TestMerger_Merge_CollapseServiceTags_MultipleServices(t *testing.T) {
588591
if !tagNames["TwinOS"] {
589592
t.Error("expected 'TwinOS' tag")
590593
}
594+
591595
if !tagNames["Portal"] {
592596
t.Error("expected 'Portal' tag")
593597
}

0 commit comments

Comments
 (0)