-
Notifications
You must be signed in to change notification settings - Fork 9
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
SYSENG-1357: generic client default options #361
Conversation
9ead8f6
to
3f68833
Compare
Code Climate has analyzed commit f5b16ea and detected 0 issues on this pull request. View more on Code Climate. |
3a1abc3
to
167a9f9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
found a few things that would be worth changing, but please also first note what I wrote you in Mattermost
pkg/api/types/options.go
Outdated
type AnyOption interface { | ||
ApplyToAny(Options) error | ||
} | ||
|
||
type Option interface{} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing doc comments
"go.anx.io/go-anxcloud/pkg/api/types" | ||
) | ||
|
||
func EnvironmentProduction(override bool) types.Option { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing doc comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the comment (not pushed yet).
But do we need this file and its content at all in this project? We are already defaulting to the prod environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm probably not, good point - up to you
url, err := np.EndpointURL( | ||
types.ContextWithOperation(types.ContextWithOptions(context.TODO(), &types.ListOptions{}), types.OperationList)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this change necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below
pkg/api/options.go
Outdated
if options, err := types.OptionsFromContext(ctx); err != nil { | ||
return "", err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't it just return the default value in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. not sure about that. I think Options are always expected to be available on the context when using this helper (GetEnvironmentPathSegment
) defaulting to prod (in most cases) on error sounds a bit dangerous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really not sure .. I think the risk to default to prod due to this is pretty low and can be reasonably enough tested with unit tests, but makes the code using this function a lot nicer in exchange
And dealing with the risk is also possible by using tokens that cannot use the wrong service
pkg/api/options.go
Outdated
} else if err != nil { | ||
return "", err | ||
} else if envString, ok := env.(string); !ok { | ||
return "", ErrEnvironmentSegmentNotTypeString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the comment above is accepted, this is the last branch that could return an error - could it be sensible to have a environments
variable on types.commonOptions
that can only hold strings, removing the need for this function to be able to return an error, which then also helps with the now 5 returns in apis/kubernetes/v1.endpointURL
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With extra methods SetEnvironment
& GetEnvironment
on types.Options i suppose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like that, yea
b1074c9
to
e6d05f2
Compare
pkg/api/api_implementation.go
Outdated
for _, requestOpt := range a.requestOptions { | ||
if getOpt, ok := requestOpt.(types.GetOption); ok { | ||
err = errors.Join(err, getOpt.ApplyToGet(&options)) | ||
} | ||
} | ||
for _, opt := range opts { | ||
opt.ApplyToGet(&options) | ||
err = errors.Join(err, opt.ApplyToGet(&options)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be prettied up a bit for all the request methods with a small generic helper:
func resolveRequestOptions[T any](commonOptions []types.Option, requestOptions []T) []T {
return append(filterOptions[T](commonOptions), requestOptions...)
}
func filterOptions[T any](opts []types.Option) []T {
ret := make([]T, 0, len(opts))
for _, v := range opts {
if v, ok := v.(T); ok {
ret = append(ret, v)
}
}
return ret
}
With that present, the marked code would be replaced with this:
for _, opt := range resolveRequestOptions(a.requestOptions, opts) {
err = errors.Join(err, opt.ApplyToGet(&options))
}
@@ -73,3 +107,26 @@ func (o *commonOptions) Set(key string, val interface{}, overwrite bool) error { | |||
o.additional[key] = val | |||
return nil | |||
} | |||
|
|||
func (o commonOptions) GetEnvironment(key string) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no doc comment..
pkg/api/errors.go
Outdated
|
||
// ErrEnvironmentSegmentNotTypeString is returned when the environment segment is not of type string | ||
ErrEnvironmentSegmentNotTypeString = errors.New("environment segment is not of type string") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed anymore, right?
d56adca
to
9fc6738
Compare
9fc6738
to
512b567
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, the failing integration tests well meh.
Otherwise doc strings have all been added.
All relevant integration tests went through, I'm ok with merging without all of them being green |
Description
This PR adds functionality to define default requests options via
WithRequestOptions
when creating a new generic API client.Checklist
Unreleased
section in CHANGELOG.md, if user facing changeReferences
Community Note