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

improvement: New*ParamStorer and ContextWith*Params accept variadic map arguments #133

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-133.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: New*ParamStorer and ContextWith*Params accept variadic map arguments
links:
- https://github.com/palantir/witchcraft-go-params/pull/133
20 changes: 16 additions & 4 deletions paramstorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ func NewParamStorer(paramStorers ...ParamStorer) ParamStorer {
}

// NewSafeParamStorer returns a new ParamStorer that stores the provided parameters as SafeParams.
func NewSafeParamStorer(safeParams map[string]interface{}) ParamStorer {
return NewSafeAndUnsafeParamStorer(safeParams, nil)
func NewSafeParamStorer(safeParams ...map[string]interface{}) ParamStorer {
storer := &mapParamStorer{}
for _, p := range safeParams {
for k, v := range p {
storer.putSafeParam(k, v)
}
}
return storer
}

// NewSafeParam returns a new ParamStorer that stores a single safe parameter.
Expand All @@ -35,8 +41,14 @@ func NewSafeParam(key string, value interface{}) ParamStorer {
}

// NewUnsafeParamStorer returns a new ParamStorer that stores the provided parameters as UnsafeParams.
func NewUnsafeParamStorer(unsafeParams map[string]interface{}) ParamStorer {
return NewSafeAndUnsafeParamStorer(nil, unsafeParams)
func NewUnsafeParamStorer(unsafeParams ...map[string]interface{}) ParamStorer {
storer := &mapParamStorer{}
for _, p := range unsafeParams {
for k, v := range p {
storer.putUnsafeParam(k, v)
}
}
return storer
}

// NewUnsafeParam returns a new ParamStorer that stores a single unsafe parameter.
Expand Down
8 changes: 4 additions & 4 deletions paramstorer_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func ContextWithSafeParam(ctx context.Context, key string, value interface{}) co
// ContextWithSafeParams returns a copy of the provided context that contains the provided safe parameters. If the
// provided context already has safe/unsafe params, the newly returned context will contain the result of merging the
// previous parameters with the provided parameters.
func ContextWithSafeParams(ctx context.Context, safeParams map[string]interface{}) context.Context {
return ContextWithParamStorers(ctx, NewSafeParamStorer(safeParams))
func ContextWithSafeParams(ctx context.Context, safeParams ...map[string]interface{}) context.Context {
return ContextWithParamStorers(ctx, NewSafeParamStorer(safeParams...))
}

// ContextWithUnsafeParam returns a copy of the provided context that contains the provided unsafe parameter. If the
Expand All @@ -39,8 +39,8 @@ func ContextWithUnsafeParam(ctx context.Context, key string, value interface{})
// ContextWithUnsafeParams returns a copy of the provided context that contains the provided unsafe parameters. If the
// provided context already has safe/unsafe params, the newly returned context will contain the result of merging the
// previous parameters with the provided parameters.
func ContextWithUnsafeParams(ctx context.Context, unsafeParams map[string]interface{}) context.Context {
return ContextWithParamStorers(ctx, NewUnsafeParamStorer(unsafeParams))
func ContextWithUnsafeParams(ctx context.Context, unsafeParams ...map[string]interface{}) context.Context {
return ContextWithParamStorers(ctx, NewUnsafeParamStorer(unsafeParams...))
}

// ContextWithSafeAndUnsafeParams returns a copy of the provided context that contains the provided safe and unsafe
Expand Down