Skip to content

Commit d27b0d7

Browse files
ChrisJBurnsclaude
andcommitted
Drop marshalJSONString in favor of strconv.Quote
json.Marshal of a Go string cannot fail at runtime — encoding/json's stringEncoder has no error path for valid Go strings. The helper existed to placate errcheck rather than to catch a real failure mode, adding two unreachable error branches per call site. strconv.Quote produces the same JSON-encoded bytes with no error-checking overhead, and the call sites become readable single expressions. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 8d7ff32 commit d27b0d7

1 file changed

Lines changed: 7 additions & 20 deletions

File tree

cmd/thv-operator/controllers/mcpauthzconfig_controller.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"encoding/json"
99
"fmt"
10+
"strconv"
1011
"time"
1112

1213
"k8s.io/apimachinery/pkg/api/errors"
@@ -210,18 +211,13 @@ func buildFullAuthzConfigJSON(spec mcpv1beta1.MCPAuthzConfigSpec) ([]byte, autho
210211
return nil, nil, fmt.Errorf("config field is empty")
211212
}
212213

213-
versionJSON, err := marshalJSONString(authzConfigVersion)
214-
if err != nil {
215-
return nil, nil, fmt.Errorf("failed to marshal version: %w", err)
216-
}
217-
typeJSON, err := marshalJSONString(spec.Type)
218-
if err != nil {
219-
return nil, nil, fmt.Errorf("failed to marshal type: %w", err)
220-
}
221-
214+
// strconv.Quote produces the same JSON-encoded form as json.Marshal on a
215+
// Go string, without an error return: encoding/json's stringEncoder has
216+
// no failure path for a valid Go string, which authzConfigVersion (a
217+
// constant) and spec.Type (validated upstream) both are.
222218
fullConfig := map[string]json.RawMessage{
223-
"version": versionJSON,
224-
"type": typeJSON,
219+
"version": json.RawMessage(strconv.Quote(authzConfigVersion)),
220+
"type": json.RawMessage(strconv.Quote(spec.Type)),
225221
factory.ConfigKey(): spec.Config.Raw,
226222
}
227223

@@ -232,15 +228,6 @@ func buildFullAuthzConfigJSON(spec mcpv1beta1.MCPAuthzConfigSpec) ([]byte, autho
232228
return result, factory, nil
233229
}
234230

235-
// marshalJSONString marshals a string value to JSON, returning an error instead of panicking.
236-
func marshalJSONString(v string) (json.RawMessage, error) {
237-
b, err := json.Marshal(v)
238-
if err != nil {
239-
return nil, fmt.Errorf("failed to marshal %q: %w", v, err)
240-
}
241-
return b, nil
242-
}
243-
244231
// canonicalizeSpecForHash returns a copy of spec whose Config.Raw has been
245232
// re-marshalled into canonical JSON form (sorted keys, no extra whitespace).
246233
// The returned value is suitable for ctrlutil.CalculateConfigHash and produces

0 commit comments

Comments
 (0)