You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 24, 2025. It is now read-only.
Currently, the RPC handler uses switch statements to route method names to appropriate handler functions. This approach works but could be improved for maintainability and scalability.
Proposed Solution
Refactor the RPC handler to use maps for mapping method names directly to handler functions. This would:
Eliminate the need for switch statements when routing methods
Provide O(1) lookup time for method resolution
Make adding new methods easier (just add a new entry to the map)
Improve code maintainability
Implementation Suggestion
// Define a type for handler functionstypeRPCHandlerFuncfunc(c*fiber.Ctx, ownerIDuint, reqRPCRequest) error// In RPCHandler struct, add maps for method routingtypeRPCHandlerstruct {
ProjectHandlers*ProjectHandlersTaskHandlers*TaskHandlers// Maps for method routingprojectMethodsmap[string]RPCHandlerFunctaskMethodsmap[string]RPCHandlerFunc
}
// Initialize maps in constructorfuncNewRPCHandler(projectHandlers*ProjectHandlers, taskHandlers*TaskHandlers) *RPCHandler {
h:=&RPCHandler{
ProjectHandlers: projectHandlers,
TaskHandlers: taskHandlers,
projectMethods: make(map[string]RPCHandlerFunc),
taskMethods: make(map[string]RPCHandlerFunc),
}
// Register project methodsh.projectMethods[ProjectCreate] =projectHandlers.Createh.projectMethods[ProjectGet] =projectHandlers.Get// ... register other methods// Register task methodsh.taskMethods[TaskGet] =taskHandlers.Get// ... register other methodsreturnh
}
// Simplify method handlingfunc (h*RPCHandler) handleProjectMethod(c*fiber.Ctx, reqRPCRequest) error {
handler, exists:=h.projectMethods[req.Method]
if!exists {
returnrespondWithRPCError(c, fiber.StatusBadRequest, ErrMsgUnknownProjMethod, nil, req.ID)
}
returnhandler(c, ownerID, req)
}
This approach would make the code more maintainable and extensible.
Related to
PR #182: Implemented RPC model for projects and tasks
Description
Currently, the RPC handler uses switch statements to route method names to appropriate handler functions. This approach works but could be improved for maintainability and scalability.
Proposed Solution
Refactor the RPC handler to use maps for mapping method names directly to handler functions. This would:
Implementation Suggestion
This approach would make the code more maintainable and extensible.
Related to
PR #182: Implemented RPC model for projects and tasks
Requested by
@mojtaba-esk