github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/ctx_kv.go (about)

     1  package kit
     2  
     3  import "context"
     4  
     5  func (ctx *Context) setRoute(route string) *Context {
     6  	ctx.route = append(ctx.route[:0], route...)
     7  
     8  	return ctx
     9  }
    10  
    11  func (ctx *Context) Route() string {
    12  	return string(ctx.route)
    13  }
    14  
    15  func (ctx *Context) setServiceName(s string) *Context {
    16  	ctx.serviceName = append(ctx.serviceName[:0], s...)
    17  
    18  	return ctx
    19  }
    20  
    21  func (ctx *Context) ServiceName() string {
    22  	return string(ctx.serviceName)
    23  }
    24  
    25  func (ctx *Context) setContractID(contractID string) *Context {
    26  	ctx.contractID = append(ctx.contractID[:0], contractID...)
    27  
    28  	return ctx
    29  }
    30  
    31  func (ctx *Context) ContractID() string {
    32  	return string(ctx.contractID)
    33  }
    34  
    35  func (ctx *Context) Set(key string, val any) *Context {
    36  	ctx.Lock()
    37  	ctx.kv[key] = val
    38  	ctx.ctx = context.WithValue(ctx.ctx, key, val)
    39  	ctx.Unlock()
    40  
    41  	return ctx
    42  }
    43  
    44  func (ctx *Context) Get(key string) any {
    45  	ctx.Lock()
    46  	v := ctx.kv[key]
    47  	ctx.Unlock()
    48  
    49  	return v
    50  }
    51  
    52  func (ctx *Context) Walk(f func(key string, val any) bool) {
    53  	for k, v := range ctx.kv {
    54  		if !f(k, v) {
    55  			return
    56  		}
    57  	}
    58  }
    59  
    60  func (ctx *Context) Exists(key string) bool {
    61  	return ctx.Get(key) != nil
    62  }
    63  
    64  func (ctx *Context) GetInt64(key string, defaultValue int64) int64 {
    65  	v, ok := ctx.Get(key).(int64)
    66  	if !ok {
    67  		return defaultValue
    68  	}
    69  
    70  	return v
    71  }
    72  
    73  func (ctx *Context) GetInt32(key string, defaultValue int32) int32 {
    74  	v, ok := ctx.Get(key).(int32)
    75  	if !ok {
    76  		return defaultValue
    77  	}
    78  
    79  	return v
    80  }
    81  
    82  func (ctx *Context) GetUint64(key string, defaultValue uint64) uint64 {
    83  	v, ok := ctx.Get(key).(uint64)
    84  	if !ok {
    85  		return defaultValue
    86  	}
    87  
    88  	return v
    89  }
    90  
    91  func (ctx *Context) GetUint32(key string, defaultValue uint32) uint32 {
    92  	v, ok := ctx.Get(key).(uint32)
    93  	if !ok {
    94  		return defaultValue
    95  	}
    96  
    97  	return v
    98  }
    99  
   100  func (ctx *Context) GetString(key string, defaultValue string) string {
   101  	v, ok := ctx.Get(key).(string)
   102  	if !ok {
   103  		return defaultValue
   104  	}
   105  
   106  	return v
   107  }
   108  
   109  func (ctx *Context) GetBytes(key string, defaultValue []byte) []byte {
   110  	v, ok := ctx.Get(key).([]byte)
   111  	if !ok {
   112  		return defaultValue
   113  	}
   114  
   115  	return v
   116  }
   117  
   118  // LocalStore returns a local store which could be used to share some key-values between different
   119  // requests. If you need to have a shared key-value store between different instances of your server
   120  // then you need to use ClusterStore method.
   121  func (ctx *Context) LocalStore() Store {
   122  	return ctx.ls
   123  }