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

Conversation

the-goodies
Copy link
Contributor

When calling fctx.Wrap with context which doesn't have contextKey{} (not made with fctx.WithMeta, like context.Background()), don't prematurely return. This allows to use context.Background() when you need to return an error with additional data, and you don't have a passed context or that context is passed, but is not “special” (not enhanced with fctx.WithMeta). So if you pass this empty context, and finally return using wrap with additional arguments, those arguments will dissapear:

return fctx.Wrap(err, ctx, // empty context without contextKey{}
	"data_from", dateFrom.Format(time.DateOnly),
	"data_to", dateTo.Format(time.DateOnly),
)

Also this change allows this pattern:

return fault.Wrap(err,
	fmsg.With("Service: ..."),
	fctx.With(context.Background(),
		"data_from", dateFrom.Format(time.DateOnly),
		"data_to", dateTo.Format(time.DateOnly),
	),
)

Instead of a previous more verbose:

return fault.Wrap(err,
	fmsg.With("Service: ..."),
	fctx.With(
		fctx.WithMeta(context.Background(),
			"data_from", dateFrom.Format(time.DateOnly),
			"data_to", dateTo.Format(time.DateOnly),
		),
	),
)

Also when calling fctx.Wrap with context which doesn't have contextKey{} (not made with fctx.WithMeta, like context.Background()), don't prematurely return. This allows to use context.Background() when you need to return an error with additional data and you don't have a passed context or that context is passed, but is not "special" (not enhanced with fctx.WithMeta).

This change allows this pattern:
	return fault.Wrap(err,
		fmsg.With("Service: ..."),
		fctx.With(context.Background(),
			"data_from", dateFrom.Format(time.DateOnly),
			"data_to", dateTo.Format(time.DateOnly),
		),
	)

Instead of a previous more verbose:
	return fault.Wrap(err,
		fmsg.With("Service: ..."),
		fctx.With(
			fctx.WithMeta(context.Background(),
				"data_from", dateFrom.Format(time.DateOnly),
				"data_to", dateTo.Format(time.DateOnly),
			),
		),
	)
@Southclaws
Copy link
Owner

No notes, love it!

Unfortunately I seem to have broken the tests on the master branch and they won't run on PRs because I didn't configure actions properly so bear with me while I resolve that and rebase this branch!

@Southclaws Southclaws merged commit f02308e into Southclaws:master Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants