Use typed context keys instead of string literals#4814
Use typed context keys instead of string literals#4814tmchow wants to merge 2 commits intoarmadaproject:masterfrom
Conversation
Greptile SummaryThis PR refactors bare string context keys ( Confidence Score: 5/5Safe to merge — purely a refactor with no logic changes, all write/read sites updated consistently. All five files make symmetric, mechanical substitutions of string literals with typed constants. The unexported No files require special attention.
|
| Filename | Overview |
|---|---|
| internal/common/ctxkeys/ctxkeys.go | New leaf package with unexported contextKey type and three exported constants — correct idiomatic Go pattern for collision-safe context keys. |
| internal/common/auth/common.go | Local principalKey const now aliases ctxkeys.PrincipalKey; "user" write-site replaced with ctxkeys.UserKey. Write/read symmetry preserved — GetPrincipal/WithPrincipal both use principalKey. |
| internal/common/requestid/interceptors.go | "requestId" write-site in AddToIncomingContext replaced with ctxkeys.RequestIDKey; consistent with read sites in armadacontext and grpc. |
| internal/common/armadacontext/armada_context.go | Read sites in FromGrpcCtx updated to use ctxkeys.UserKey and ctxkeys.RequestIDKey — no logic change. |
| internal/common/grpc/grpc.go | Read sites in InterceptorLogger updated to use typed keys — no logic change. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
subgraph ctxkeys ["internal/common/ctxkeys"]
PK["PrincipalKey (contextKey)"]
UK["UserKey (contextKey)"]
RK["RequestIDKey (contextKey)"]
end
subgraph writers ["Context Writers"]
AUTH["auth/common.go\nCreateGrpcMiddlewareAuthFunction"]
REQID["requestid/interceptors.go\nAddToIncomingContext"]
end
subgraph readers ["Context Readers"]
ARMADA["armadacontext/armada_context.go\nFromGrpcCtx"]
GRPC["grpc/grpc.go\nInterceptorLogger"]
AUTHREAD["auth/common.go\nGetPrincipal / WithPrincipal"]
end
PK -->|principalKey alias| AUTHREAD
UK --> AUTH
UK --> ARMADA
UK --> GRPC
RK --> REQID
RK --> ARMADA
RK --> GRPC
Reviews (5): Last reviewed commit: "fix(ctxkeys): unexport ContextKey type f..." | Re-trigger Greptile
5ee6144 to
7e41304
Compare
Introduce a ctxkeys package with a custom ContextKey type to prevent
context value collisions from bare string keys. Replace all plain
string context keys ("principal", "user", "requestId") with typed
constants from the new package.
The ctxkeys package is a leaf dependency with no imports, avoiding
circular dependencies between auth, requestid, and armadacontext.
Closes armadaproject#4780
Signed-off-by: Trevin Chow <trevin@trevinchow.com>
c67bc17 to
ba87952
Compare
Make the ContextKey type unexported (contextKey) so no external package can construct a colliding key. The exported constants remain accessible across internal packages while the type itself is hidden. This contribution was developed with AI assistance (Claude Code). Signed-off-by: Trevin Chow <trevin@trevinchow.com>
ba87952 to
d644b5f
Compare
Replaces bare string context keys (
"principal","user","requestId") with typed constants from a newctxkeyspackage. This prevents silent value collisions if another package uses the same string as a context key.The
ctxkeyspackage is a leaf dependency with zero imports, so it can be used byauth,requestid,armadacontext, andgrpcwithout introducing circular dependencies.Changes:
internal/common/ctxkeys/ctxkeys.gowithContextKeytype and three constantsauth/common.go:principalKeynow usesctxkeys.PrincipalKey,"user"literal replaced withctxkeys.UserKeyrequestid/interceptors.go:"requestId"literal replaced withctxkeys.RequestIDKeyarmadacontext/armada_context.go: readers updated to use typed keysgrpc/grpc.go: readers updated to use typed keysCloses #4780
This PR was authored by a human with AI coding assistance.