github.com/Finschia/finschia-sdk@v0.48.1/types/router.go (about)

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