github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/types/wasm/context.go (about) 1 package wasm 2 3 import ( 4 "context" 5 6 "github.com/machinefi/w3bstream/pkg/depends/conf/log" 7 "github.com/machinefi/w3bstream/pkg/depends/conf/mqtt" 8 "github.com/machinefi/w3bstream/pkg/depends/x/contextx" 9 "github.com/machinefi/w3bstream/pkg/depends/x/mapx" 10 "github.com/machinefi/w3bstream/pkg/depends/x/misc/must" 11 "github.com/machinefi/w3bstream/pkg/modules/metrics" 12 ) 13 14 type ( 15 CtxSQLStore struct{} 16 CtxKVStore struct{} 17 CtxLogger struct{} 18 CtxEnv struct{} 19 CtxRedisPrefix struct{} 20 CtxChainClient struct{} 21 CtxRuntimeResource struct{} 22 CtxRuntimeEventTypes struct{} 23 CtxMqttClient struct{} 24 CtxCustomMetrics struct{} 25 CtxFlow struct{} 26 ) 27 28 func WithSQLStore(ctx context.Context, v *Database) context.Context { 29 return contextx.WithValue(ctx, CtxSQLStore{}, v) 30 } 31 32 func WithSQLStoreContext(v *Database) contextx.WithContext { 33 return func(ctx context.Context) context.Context { 34 return contextx.WithValue(ctx, CtxSQLStore{}, v) 35 } 36 } 37 38 func SQLStoreFromContext(ctx context.Context) (*Database, bool) { 39 v, ok := ctx.Value(CtxSQLStore{}).(*Database) 40 return v, ok 41 } 42 43 func MustSQLStoreFromContext(ctx context.Context) *Database { 44 v, ok := SQLStoreFromContext(ctx) 45 must.BeTrue(ok) 46 return v 47 } 48 49 func WithKVStore(ctx context.Context, v KVStore) context.Context { 50 return contextx.WithValue(ctx, CtxKVStore{}, v) 51 } 52 53 func WithKVStoreContext(v KVStore) contextx.WithContext { 54 return func(ctx context.Context) context.Context { 55 return contextx.WithValue(ctx, CtxKVStore{}, v) 56 } 57 } 58 59 func KVStoreFromContext(ctx context.Context) (KVStore, bool) { 60 v, ok := ctx.Value(CtxKVStore{}).(KVStore) 61 return v, ok 62 } 63 64 func MustKVStoreFromContext(ctx context.Context) KVStore { 65 v, ok := KVStoreFromContext(ctx) 66 must.BeTrue(ok) 67 return v 68 } 69 70 func WithLogger(ctx context.Context, v log.Logger) context.Context { 71 return contextx.WithValue(ctx, CtxLogger{}, v) 72 } 73 74 func WithLoggerContext(v log.Logger) contextx.WithContext { 75 return func(ctx context.Context) context.Context { 76 return contextx.WithValue(ctx, CtxLogger{}, v) 77 } 78 } 79 80 func LoggerFromContext(ctx context.Context) (log.Logger, bool) { 81 v, ok := ctx.Value(CtxLogger{}).(log.Logger) 82 return v, ok 83 } 84 85 func MustLoggerFromContext(ctx context.Context) log.Logger { 86 v, ok := LoggerFromContext(ctx) 87 must.BeTrue(ok) 88 return v 89 } 90 91 func WithEnv(ctx context.Context, v *Env) context.Context { 92 return contextx.WithValue(ctx, CtxEnv{}, v) 93 } 94 95 func WithEnvContext(v *Env) contextx.WithContext { 96 return func(ctx context.Context) context.Context { 97 return contextx.WithValue(ctx, CtxEnv{}, v) 98 } 99 } 100 101 func EnvFromContext(ctx context.Context) (*Env, bool) { 102 v, ok := ctx.Value(CtxEnv{}).(*Env) 103 return v, ok 104 } 105 106 func MustEnvFromContext(ctx context.Context) *Env { 107 v, ok := EnvFromContext(ctx) 108 must.BeTrue(ok) 109 return v 110 } 111 112 func WithRedisPrefix(ctx context.Context, v string) context.Context { 113 return contextx.WithValue(ctx, CtxRedisPrefix{}, v) 114 } 115 116 func WithRedisPrefixContext(v string) contextx.WithContext { 117 return func(ctx context.Context) context.Context { 118 return contextx.WithValue(ctx, CtxRedisPrefix{}, v) 119 } 120 } 121 122 func RedisPrefixFromContext(ctx context.Context) (string, bool) { 123 v, ok := ctx.Value(CtxRedisPrefix{}).(string) 124 return v, ok 125 } 126 127 func MustRedisPrefixFromContext(ctx context.Context) string { 128 v, ok := RedisPrefixFromContext(ctx) 129 must.BeTrue(ok) 130 return v 131 } 132 133 func WithChainClient(ctx context.Context, v *ChainClient) context.Context { 134 return contextx.WithValue(ctx, CtxChainClient{}, v) 135 } 136 137 func WithChainClientContext(v *ChainClient) contextx.WithContext { 138 return func(ctx context.Context) context.Context { 139 return contextx.WithValue(ctx, CtxChainClient{}, v) 140 } 141 } 142 143 func ChainClientFromContext(ctx context.Context) (*ChainClient, bool) { 144 v, ok := ctx.Value(CtxChainClient{}).(*ChainClient) 145 return v, ok 146 } 147 148 func MustChainClientFromContext(ctx context.Context) *ChainClient { 149 v, ok := ChainClientFromContext(ctx) 150 must.BeTrue(ok) 151 return v 152 } 153 154 func WithRuntimeResource(ctx context.Context, v *mapx.Map[uint32, []byte]) context.Context { 155 return contextx.WithValue(ctx, CtxRuntimeResource{}, v) 156 } 157 158 func WithRuntimeResourceContext(v *mapx.Map[uint32, []byte]) contextx.WithContext { 159 return func(ctx context.Context) context.Context { 160 return contextx.WithValue(ctx, CtxRuntimeResource{}, v) 161 } 162 } 163 164 func RuntimeResourceFromContext(ctx context.Context) (*mapx.Map[uint32, []byte], bool) { 165 v, ok := ctx.Value(CtxRuntimeResource{}).(*mapx.Map[uint32, []byte]) 166 return v, ok 167 } 168 169 func MustRuntimeResourceFromContext(ctx context.Context) *mapx.Map[uint32, []byte] { 170 v, ok := RuntimeResourceFromContext(ctx) 171 must.BeTrue(ok) 172 return v 173 } 174 175 func WithRuntimeEventTypes(ctx context.Context, v *mapx.Map[uint32, []byte]) context.Context { 176 return contextx.WithValue(ctx, CtxRuntimeEventTypes{}, v) 177 } 178 179 func WithRuntimeEventTypesContext(v *mapx.Map[uint32, []byte]) contextx.WithContext { 180 return func(ctx context.Context) context.Context { 181 return contextx.WithValue(ctx, CtxRuntimeEventTypes{}, v) 182 } 183 } 184 185 func RuntimeEventTypesFromContext(ctx context.Context) (*mapx.Map[uint32, []byte], bool) { 186 v, ok := ctx.Value(CtxRuntimeEventTypes{}).(*mapx.Map[uint32, []byte]) 187 return v, ok 188 } 189 190 func MustRuntimeEventTypesFromContext(ctx context.Context) *mapx.Map[uint32, []byte] { 191 v, ok := RuntimeResourceFromContext(ctx) 192 must.BeTrue(ok) 193 return v 194 } 195 196 func WithMQTTClient(ctx context.Context, mq *mqtt.Client) context.Context { 197 return contextx.WithValue(ctx, CtxMqttClient{}, mq) 198 } 199 200 func WithMQTTClientContext(mq *mqtt.Client) contextx.WithContext { 201 return func(ctx context.Context) context.Context { 202 return contextx.WithValue(ctx, CtxMqttClient{}, mq) 203 } 204 } 205 206 func MQTTClientFromContext(ctx context.Context) (*mqtt.Client, bool) { 207 v, ok := ctx.Value(CtxMqttClient{}).(*mqtt.Client) 208 return v, ok 209 } 210 211 func MustMQTTClientFromContext(ctx context.Context) *mqtt.Client { 212 v, ok := MQTTClientFromContext(ctx) 213 must.BeTrue(ok) 214 return v 215 } 216 217 func WithCustomMetrics(ctx context.Context, mt metrics.CustomMetrics) context.Context { 218 return contextx.WithValue(ctx, CtxCustomMetrics{}, mt) 219 } 220 221 func WithCustomMetricsContext(mt metrics.CustomMetrics) contextx.WithContext { 222 return func(ctx context.Context) context.Context { 223 return contextx.WithValue(ctx, CtxCustomMetrics{}, mt) 224 } 225 } 226 227 func CustomMetricsFromContext(ctx context.Context) (metrics.CustomMetrics, bool) { 228 v, ok := ctx.Value(CtxCustomMetrics{}).(metrics.CustomMetrics) 229 return v, ok 230 } 231 232 func MustCustomMetricsFromContext(ctx context.Context) metrics.CustomMetrics { 233 v, ok := CustomMetricsFromContext(ctx) 234 must.BeTrue(ok) 235 return v 236 } 237 238 func WithFlow(ctx context.Context, flow *Flow) context.Context { 239 return contextx.WithValue(ctx, CtxFlow{}, flow) 240 } 241 242 func WithFlowContext(flow *Flow) contextx.WithContext { 243 return func(ctx context.Context) context.Context { 244 return contextx.WithValue(ctx, CtxFlow{}, flow) 245 } 246 } 247 248 func FlowFromContext(ctx context.Context) (*Flow, bool) { 249 v, ok := ctx.Value(CtxFlow{}).(*Flow) 250 return v, ok 251 } 252 253 func MustFlowFromContext(ctx context.Context) *Flow { 254 v, ok := FlowFromContext(ctx) 255 must.BeTrue(ok) 256 return v 257 }