github.com/blend/go-sdk@v1.20220411.3/web/ctx_option.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"net/http"
    12  	"time"
    13  
    14  	"github.com/blend/go-sdk/logger"
    15  	"github.com/blend/go-sdk/webutil"
    16  )
    17  
    18  // CtxOption is an option for a context.
    19  type CtxOption func(*Ctx)
    20  
    21  // OptCtxApp sets the context app.
    22  func OptCtxApp(a *App) CtxOption {
    23  	return func(c *Ctx) { c.App = a }
    24  }
    25  
    26  // OptCtxAuth sets the context auth manager.
    27  func OptCtxAuth(auth AuthManager) CtxOption {
    28  	return func(c *Ctx) { c.Auth = auth }
    29  }
    30  
    31  // OptCtxDefaultProvider sets the context default result provider.
    32  func OptCtxDefaultProvider(rp ResultProvider) CtxOption {
    33  	return func(c *Ctx) { c.DefaultProvider = rp }
    34  }
    35  
    36  // OptCtxViews sets the context views cache.
    37  func OptCtxViews(vc *ViewCache) CtxOption {
    38  	return func(c *Ctx) { c.Views = vc }
    39  }
    40  
    41  // OptCtxState sets the context state.
    42  func OptCtxState(s State) CtxOption {
    43  	return func(c *Ctx) { c.State = s }
    44  }
    45  
    46  // OptCtxSession sets the context session.
    47  func OptCtxSession(s *Session) CtxOption {
    48  	return func(c *Ctx) { c.Session = s }
    49  }
    50  
    51  // OptCtxRoute sets the context route.
    52  func OptCtxRoute(r *Route) CtxOption {
    53  	return func(c *Ctx) { c.Route = r }
    54  }
    55  
    56  // OptCtxRouteParams sets the context route params.
    57  func OptCtxRouteParams(r RouteParameters) CtxOption {
    58  	return func(c *Ctx) { c.RouteParams = r }
    59  }
    60  
    61  // OptCtxRequestStarted sets the context request started timestamp.
    62  func OptCtxRequestStarted(ts time.Time) CtxOption {
    63  	return func(c *Ctx) { c.RequestStarted = ts }
    64  }
    65  
    66  // OptCtxLog sets the context logger.
    67  func OptCtxLog(log logger.Log) CtxOption {
    68  	return func(c *Ctx) { c.Log = log }
    69  }
    70  
    71  // OptCtxTracer sets the context tracer.
    72  func OptCtxTracer(tracer Tracer) CtxOption {
    73  	return func(c *Ctx) { c.Tracer = tracer }
    74  }
    75  
    76  // OptCtxRouteParamValue sets the context default result provider.
    77  func OptCtxRouteParamValue(key, value string) CtxOption {
    78  	return func(c *Ctx) {
    79  		if c.RouteParams == nil {
    80  			c.RouteParams = make(RouteParameters)
    81  		}
    82  		c.RouteParams[key] = value
    83  	}
    84  }
    85  
    86  // CtxRequestOption is a ctx option that wraps a request option.
    87  func CtxRequestOption(opt func(*http.Request) error) CtxOption {
    88  	return func(c *Ctx) {
    89  		_ = opt(c.Request)
    90  	}
    91  }
    92  
    93  // OptCtxQueryValue sets a query value on a context.
    94  func OptCtxQueryValue(key, value string) CtxOption {
    95  	return CtxRequestOption(webutil.OptQueryValue(key, value))
    96  }
    97  
    98  // OptCtxHeaderValue sets a header value on a context.
    99  func OptCtxHeaderValue(key, value string) CtxOption {
   100  	return CtxRequestOption(webutil.OptHeaderValue(key, value))
   101  }
   102  
   103  // OptCtxPostFormValue sets a form value on a context.
   104  func OptCtxPostFormValue(key, value string) CtxOption {
   105  	return CtxRequestOption(webutil.OptPostFormValue(key, value))
   106  }
   107  
   108  // OptCtxCookieValue sets a cookie value on a context.
   109  func OptCtxCookieValue(key, value string) CtxOption {
   110  	return CtxRequestOption(webutil.OptCookieValue(key, value))
   111  }
   112  
   113  // OptCtxBodyBytes sets a post body on a context.
   114  func OptCtxBodyBytes(body []byte) CtxOption {
   115  	return CtxRequestOption(webutil.OptBodyBytes(body))
   116  }
   117  
   118  // OptCtxPostedFiles sets posted files on a context.
   119  func OptCtxPostedFiles(files ...webutil.PostedFile) CtxOption {
   120  	return CtxRequestOption(webutil.OptPostedFiles(files...))
   121  }