dispatcher_modifier.go raw

   1  // Copyright (c) 2016, 2018, 2025, Oracle and/or its affiliates.  All rights reserved.
   2  // This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
   3  
   4  package auth
   5  
   6  import "github.com/nrdcg/oci-go-sdk/common/v1065"
   7  
   8  // dispatcherModifier gives ability to modify a HTTPRequestDispatcher before use.
   9  type dispatcherModifier struct {
  10  	modifiers []func(common.HTTPRequestDispatcher) (common.HTTPRequestDispatcher, error)
  11  }
  12  
  13  // newDispatcherModifier creates a new dispatcherModifier with optional initial modifier (may be nil).
  14  func newDispatcherModifier(modifier func(common.HTTPRequestDispatcher) (common.HTTPRequestDispatcher, error)) *dispatcherModifier {
  15  	dispatcherModifier := &dispatcherModifier{
  16  		modifiers: make([]func(common.HTTPRequestDispatcher) (common.HTTPRequestDispatcher, error), 0),
  17  	}
  18  	if modifier != nil {
  19  		dispatcherModifier.QueueModifier(modifier)
  20  	}
  21  	return dispatcherModifier
  22  }
  23  
  24  // QueueModifier queues up a new modifier
  25  func (c *dispatcherModifier) QueueModifier(modifier func(common.HTTPRequestDispatcher) (common.HTTPRequestDispatcher, error)) {
  26  	c.modifiers = append(c.modifiers, modifier)
  27  }
  28  
  29  // Modify the provided HTTPRequestDispatcher with this modifier, and return the result, or error if something goes wrong
  30  func (c *dispatcherModifier) Modify(dispatcher common.HTTPRequestDispatcher) (common.HTTPRequestDispatcher, error) {
  31  	if len(c.modifiers) > 0 {
  32  		for _, modifier := range c.modifiers {
  33  			var err error
  34  			if dispatcher, err = modifier(dispatcher); err != nil {
  35  				common.Debugf("An error occurred when attempting to modify the dispatcher. Error was: %s", err.Error())
  36  				return nil, err
  37  			}
  38  		}
  39  	}
  40  	return dispatcher, nil
  41  }
  42