Update on the Review Comments

1. Resolved review comments.
2. Resolved Go linting issues.
3. Increase coverage from 93 to 96 percentage for module.
This commit is contained in:
MohitKatare-protean
2025-03-27 12:24:17 +05:30
parent 2fa34759bc
commit be30b522b5
14 changed files with 291 additions and 102 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"github.com/beckn/beckn-onix/pkg/log"
"github.com/beckn/beckn-onix/pkg/model"
"github.com/beckn/beckn-onix/pkg/plugin"
"github.com/beckn/beckn-onix/pkg/plugin/definition"
@@ -25,24 +24,12 @@ type PluginManager interface {
SchemaValidator(ctx context.Context, cfg *plugin.Config) (definition.SchemaValidator, error)
}
// Provider represents a function that initializes an HTTP handler using a PluginManager.
type Provider func(ctx context.Context, mgr PluginManager, cfg *Config) (http.Handler, error)
// Type defines different handler types for processing requests.
type Type string
const (
// HandlerTypeStd represents the standard handler type used for general request processing.
HandlerTypeStd Type = "std"
// HandlerTypeRegSub represents the registry subscriber handler type for handling registry subscription requests.
HandlerTypeRegSub Type = "regSub"
// HandlerTypeNPSub represents the network participant subscriber handler type for handling network participant subscription requests.
HandlerTypeNPSub Type = "npSub"
// HandlerTypeLookup represents the lookup handler type used for resolving service details.
HandlerTypeLookup Type = "lookUp"
)
// PluginCfg holds the configuration for various plugins.
@@ -108,13 +95,3 @@ func (s *Step) UnmarshalYAML(unmarshal func(interface{}) error) error {
*s = step
return nil
}
// DummyHandler is a basic HTTP handler that returns a fixed response.
func DummyHandler(ctx context.Context, mgr PluginManager, cfg *Config) (http.Handler, error) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("Dummy Handler Response")); err != nil {
log.Error(context.Background(), err, "failed to write nack response")
}
}), nil
}

View File

@@ -230,35 +230,6 @@ func (h *stdHandler) initSteps(ctx context.Context, mgr PluginManager, cfg *Conf
steps[c.ID] = step
}
// Register processing steps
for _, step := range cfg.Steps {
var s definition.Step
var err error
switch step {
case "sign":
s, err = newSignStep(h.signer, h.km)
case "validateSign":
s, err = newValidateSignStep(h.signValidator, h.km)
case "validateSchema":
s, err = newValidateSchemaStep(h.schemaValidator)
case "addRoute":
s, err = newRouteStep(h.router)
case "broadcast":
s = &broadcastStep{}
default:
if customStep, exists := steps[step]; exists {
s = customStep
} else {
return fmt.Errorf("unrecognized step: %s", step)
}
}
if err != nil {
return err
}
h.steps = append(h.steps, s)
}
log.Infof(ctx, "Processor steps initialized: %v", cfg.Steps)
return nil
}

View File

@@ -155,12 +155,3 @@ func (s *addRouteStep) Run(ctx *model.StepContext) error {
log.Debugf(ctx, "ctx.Route to %#v", ctx.Route)
return nil
}
// broadcastStep is a stub for broadcasting.
type broadcastStep struct{}
// Run is a placeholder for future implementation.
func (b *broadcastStep) Run(ctx *model.StepContext) error {
// TODO: Implement broadcast logic if needed
return nil
}

View File

@@ -16,33 +16,22 @@ type Config struct {
Handler handler.Config
}
// Provider represents a function that initializes an HTTP handler using a PluginManager.
type Provider func(ctx context.Context, mgr handler.PluginManager, cfg *handler.Config) (http.Handler, error)
// handlerProviders maintains a mapping of handler types to their respective providers.
var handlerProviders = map[handler.Type]handler.Provider{
var handlerProviders = map[handler.Type]Provider{
handler.HandlerTypeStd: handler.NewStdHandler,
}
// GetDummyHandlerProviders returns a dummy handler provider mapping for testing purposes.
func GetDummyHandlerProviders() map[handler.Type]handler.Provider {
return map[handler.Type]handler.Provider{
handler.HandlerTypeStd: handler.DummyHandler,
}
}
// getHandlerProviders is a function to retrieve handler providers, which can be overridden for testing.
var getHandlerProviders = func() map[handler.Type]handler.Provider {
return handlerProviders
}
// Register initializes and registers handlers based on the provided configuration.
// It iterates over the module configurations, retrieves appropriate handler providers,
// and registers the handlers with the HTTP multiplexer.
func Register(ctx context.Context, mCfgs []Config, mux *http.ServeMux, mgr handler.PluginManager) error {
log.Debugf(ctx, "Registering modules with config: %#v", mCfgs)
providers := getHandlerProviders()
// Iterate over the handlers in the configuration.
for _, c := range mCfgs {
rmp, ok := providers[c.Handler.Type]
rmp, ok := handlerProviders[c.Handler.Type]
if !ok {
return fmt.Errorf("invalid module : %s", c.Name)
}