Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fctx: allow to pass 'kv strings' to fctx.With #36

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions fctx/fctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,8 @@ func WithMeta(ctx context.Context, kv ...string) context.Context {
return nil
}

data := make(map[string]string)

// overwrite any existing context metadata
if parent, ok := ctx.Value(contextKey{}).(map[string]string); ok {
// make a copy to avoid mutating parent context data via map reference.
for k, v := range parent {
data[k] = v
}
}

l := len(kv)
if l%2 != 0 {
l -= 1 // don't error on odd number of args
}

for i := 0; i < l; i += 2 {
k := kv[i]
v := kv[i+1]

data[k] = v
}

return context.WithValue(ctx, contextKey{}, data)
return context.WithValue(ctx, contextKey{}, createMeta(ctx, kv...))
}

// Wrap wraps an error with the metadata stored in the context using `WithMeta`.
Expand All @@ -81,33 +60,34 @@ func WithMeta(ctx context.Context, kv ...string) context.Context {
//
// user, err := database.GetUser(ctx, userID)
// if err != nil {
// return nil, fctx.Wrap(ctx, err, "role", "admin")
// return nil, fctx.Wrap(err, ctx, "role", "admin")
// }
//
// This library aims to be simple so there is no stack trace collection or
// additional message parameter. If you need this functionality, use pkg/errors.
//
// user, err := database.GetUser(ctx, userID)
// if err != nil {
// return nil, fctx.Wrap(ctx,
// errors.Wrap(err, "failed to get user data"),
// return nil, fctx.Wrap(errors.Wrap(err, "failed to get user data"),
// ctx,
// "role", "admin")
// }
func Wrap(err error, ctx context.Context, kv ...string) error {
if err == nil || ctx == nil {
return err
}

meta := make(map[string]string)
return &withContext{err, createMeta(ctx, kv...)}
}

parent, ok := ctx.Value(contextKey{}).(map[string]string)
if !ok {
return err
}
func createMeta(ctx context.Context, kv ...string) map[string]string {
meta := make(map[string]string)

// make a copy to avoid mutating parent error data via map reference.
for k, v := range parent {
meta[k] = v
if parent, ok := ctx.Value(contextKey{}).(map[string]string); ok {
// make a copy to avoid mutating parent context meta via map reference.
for k, v := range parent {
meta[k] = v
}
}

l := len(kv)
Expand All @@ -122,13 +102,13 @@ func Wrap(err error, ctx context.Context, kv ...string) error {
meta[k] = v
}

return &withContext{err, meta}
return meta
}

// With implements the Fault Wrapper interface.
func With(ctx context.Context) func(error) error {
func With(ctx context.Context, kv ...string) func(error) error {
return func(err error) error {
return Wrap(err, ctx)
return Wrap(err, ctx, kv...)
}
}

Expand Down