add context helpers

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-12-07 16:10:20 +03:00
parent b5d3b699cf
commit c6fd9c1c23
9 changed files with 115 additions and 53 deletions

26
config/context.go Normal file
View File

@ -0,0 +1,26 @@
package config
import (
"context"
)
type configKey struct{}
func FromContext(ctx context.Context) (Config, bool) {
c, ok := ctx.Value(configKey{}).(Config)
return c, ok
}
func NewContext(ctx context.Context, c Config) context.Context {
return context.WithValue(ctx, configKey{}, c)
}
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}

View File

@ -14,3 +14,13 @@ func FromContext(ctx context.Context) (Logger, bool) {
func NewContext(ctx context.Context, l Logger) context.Context {
return context.WithValue(ctx, loggerKey{}, l)
}
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}

View File

@ -69,12 +69,3 @@ func WithContext(ctx context.Context) Option {
args.Context = ctx
}
}
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}

View File

@ -1,2 +0,0 @@
client/grpc/* micro-client-grpc
#server/grpc/* micro-server-grpc

View File

@ -0,0 +1,26 @@
package transport
import (
"context"
)
type transportKey struct{}
func FromContext(ctx context.Context) (Transport, bool) {
c, ok := ctx.Value(transportKey{}).(Transport)
return c, ok
}
func NewContext(ctx context.Context, c Transport) context.Context {
return context.WithValue(ctx, transportKey{}, c)
}
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}

41
pull.sh
View File

@ -1,41 +0,0 @@
#!/bin/bash -ex
if [ "$1" == "--force" ]; then
force="yes"
fi
srcsha="--root"
dstsha="HEAD"
commitrange="${srcsha} ${dstsha}"
while read srcpath dstpath; do
if [ "${srcpath::1}" == "#" ] ; then
continue
fi
relpath="${srcpath//\*}"
rm -rf patches/
dstsha=$(git rev-parse HEAD)
if [ -f "../${dstpath}/.synced" ]; then
srcsha=$(cat "../${dstpath}/.synced" | tr -d '\n')
commitrange="${srcsha}..${dstsha}"
fi
git format-patch --find-copies --break-rewrites --find-renames=100% --relative="${relpath}" --no-stat --minimal --minimal --no-cover-letter --no-signature "${commitrange}" -o patches/ -- "${srcpath}"
for p in $(ls patches/); do
grep -q 'From: Vasiliy Tolstov <v.tolstov' "patches/${p}" || sed -i '/Signed-off-by: Vasiliy Tolstov/d' "patches/${p}"
done
if [ "x$(find patches/ -type f -name '*.patch' | wc -l)" != "x0" ]; then
pushd ../${dstpath} >/dev/null
git am --rerere-autoupdate --3way ../micro/patches/*.patch
popd >/dev/null
fi
echo -n "${dstsha}" > ../${dstpath}/.synced
done < mapping.txt

26
registry/context.go Normal file
View File

@ -0,0 +1,26 @@
package registry
import (
"context"
)
type registryKey struct{}
func FromContext(ctx context.Context) (Registry, bool) {
c, ok := ctx.Value(registryKey{}).(Registry)
return c, ok
}
func NewContext(ctx context.Context, c Registry) context.Context {
return context.WithValue(ctx, registryKey{}, c)
}
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}

View File

@ -17,7 +17,7 @@ func NewContext(ctx context.Context, s Server) context.Context {
return context.WithValue(ctx, serverKey{}, s)
}
// Setoption returns a function to setup a context with given value
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {

26
store/context.go Normal file
View File

@ -0,0 +1,26 @@
package store
import (
"context"
)
type storeKey struct{}
func storeContext(ctx context.Context) (Store, bool) {
c, ok := ctx.Value(storeKey{}).(Store)
return c, ok
}
func NewContext(ctx context.Context, c Store) context.Context {
return context.WithValue(ctx, storeKey{}, c)
}
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}