github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/router.go (about)

     1  package types
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  // IsAlphaNumeric defines a regular expression for matching against alpha-numeric
     9  // values.
    10  
    11  var (
    12  	// IsAlphaNumeric defines a regular expression for matching against alpha-numeric
    13  	// values.
    14  	IsAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString
    15  
    16  	// IsAlphaLower defines regular expression to check if the string has lowercase
    17  	// alphabetic characters only.
    18  	IsAlphaLower = regexp.MustCompile(`^[a-z]+$`).MatchString
    19  
    20  	// IsAlphaUpper defines regular expression to check if the string has uppercase
    21  	// alphabetic characters only.
    22  	IsAlphaUpper = regexp.MustCompile(`^[A-Z]+$`).MatchString
    23  
    24  	// IsAlpha defines regular expression to check if the string has alphabetic
    25  	// characters only.
    26  	IsAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
    27  
    28  	// IsNumeric defines regular expression to check if the string has numeric
    29  	// characters only.
    30  	IsNumeric = regexp.MustCompile(`^[0-9]+$`).MatchString
    31  )
    32  
    33  
    34  // Router provides handlers for each transaction type.
    35  type Router interface {
    36  	AddRoute(r string, h Handler) Router
    37  	Route(ctx Context, path string) Handler
    38  }
    39  
    40  // QueryRouter provides queryables for each query path.
    41  type QueryRouter interface {
    42  	AddRoute(r string, h Querier) QueryRouter
    43  	Route(path string) Querier
    44  }
    45  
    46  
    47  
    48  type Route struct {
    49  	path    string
    50  	handler Handler
    51  }
    52  
    53  // NewRoute returns an instance of Route.
    54  func NewRoute(p string, h Handler) Route {
    55  	return Route{path: strings.TrimSpace(p), handler: h}
    56  }
    57  
    58  // Path returns the path the route has assigned.
    59  func (r Route) Path() string {
    60  	return r.path
    61  }
    62  
    63  // Handler returns the handler that handles the route.
    64  func (r Route) Handler() Handler {
    65  	return r.handler
    66  }
    67  
    68  // Empty returns true only if both handler and path are not empty.
    69  func (r Route) Empty() bool {
    70  	return r.handler == nil || r.path == ""
    71  }