From 905d1608be3cdb2774553d8b306f39f477ff7777 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 18 Nov 2021 15:45:31 +0300 Subject: [PATCH] config: add helpers to load/save options Signed-off-by: Vasiliy Tolstov --- config/context.go | 20 ++++++++++++++++++++ config/options.go | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/config/context.go b/config/context.go index 7bc54872..a93d22f6 100644 --- a/config/context.go +++ b/config/context.go @@ -32,3 +32,23 @@ func SetOption(k, v interface{}) Option { o.Context = context.WithValue(o.Context, k, v) } } + +// SetSaveOption returns a function to setup a context with given value +func SetSaveOption(k, v interface{}) SaveOption { + return func(o *SaveOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} + +// SetLoadOption returns a function to setup a context with given value +func SetLoadOption(k, v interface{}) LoadOption { + return func(o *LoadOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} diff --git a/config/options.go b/config/options.go index 3dd70687..c3c0645e 100644 --- a/config/options.go +++ b/config/options.go @@ -66,6 +66,7 @@ type LoadOptions struct { Struct interface{} Override bool Append bool + Context context.Context } func NewLoadOptions(opts ...LoadOption) LoadOptions { @@ -102,7 +103,8 @@ type SaveOption func(o *SaveOptions) // SaveOptions struct type SaveOptions struct { - Struct interface{} + Struct interface{} + Context context.Context } // SaveStruct override struct for save to config