-
Notifications
You must be signed in to change notification settings - Fork 124
RSDK-11340: Combine Resource.FromRobot and Resource.FromDependencies #5315
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
base: main
Are you sure you want to change the base?
Conversation
…ies and FromRobot with GetResource
Warning your change may break code samples. If your change modifies any of the following functions please contact @viamrobotics/fleet-management. Thanks!
|
… implemented by al "Providers"
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.
Nice!!! A few requests:
- You're recreating a lot of functionality in
GetResource
, when the implementers already have ways to do it (e.g.Lookup
). Just call the existing methods from withinGetResource
. - You're marking methods as deprecated (good thing!). You should provide the alternative so that users know what to use instead
resource/resource.go
Outdated
|
||
// GetResource implements Provider for Dependencies by looking up a resource by name. | ||
func (d Dependencies) GetResource(name Name) (Resource, error) { | ||
res, err := d.Lookup(name) |
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.
You can probably just return d.Lookup(name)
instead of doing all the error checking
robot/impl/local_robot.go
Outdated
|
||
// GetResource implements resource.Provider for a localRobot by looking up a resource by name. | ||
func (r *localRobot) GetResource(name resource.Name) (resource.Resource, error) { | ||
res, err := r.ResourceByName(name) |
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.
Similarly, just return r.ResourceByName(name)
instead of doing all the checks
robot/impl/resource_manager_test.go
Outdated
|
||
// GetResource implements resource.Provider for a dummyRobot by looking up a resource by name. | ||
func (rr *dummyRobot) GetResource(name resource.Name) (resource.Resource, error) { | ||
res, err := rr.ResourceByName(name) |
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.
Same, just return this
robot/client/client.go
Outdated
|
||
// GetResource implements resource.Provider for a RobotClient by looking up a resource by name. | ||
func (rc *RobotClient) GetResource(name resource.Name) (resource.Resource, error) { | ||
res, err := rc.ResourceByName(name) |
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.
return rc.ResourceByName(name)
testutils/inject/robot.go
Outdated
|
||
// GetResource calls the injected ResourceByName or the real version of GetResource. | ||
func (r *Robot) GetResource(name resource.Name) (resource.Resource, error) { | ||
r.Mu.RLock() |
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.
just call r.ResourceByName(...)
here, and that'll take care of checking if there's an override
type Dependencies map[Name]Resource | ||
|
||
// FromDependencies returns a named component from a collection of dependencies. | ||
// Deprecated: FromDependencies returns a named component from a collection of dependencies. |
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.
Provide the new function to use if marking as deprecated
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.
nice, this looks great! I have a couple small recommendations/requests, but otherwise this lgtm!
components/switch/switch.go
Outdated
} | ||
|
||
// FromDependencies is a helper for getting the named button component from a collection of dependencies. | ||
// Deprecated: FromDependencies is a helper for getting the named button component from a collection of dependencies. |
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.
(minor, flyby request) totally unrelated to your PR, but this looks like a bad historical copy/paste. Admittedly this is now deprecated but probably good to still change this from button
to switch
.
// FromRobot is a helper for getting the named Gizmo from the given Robot. | ||
// Deprecated: FromRobot is a helper for getting the named Gizmo from the given Robot. | ||
// Use FromProvider instead. | ||
// | ||
//nolint:revive // ignore exported comment check |
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.
(minor) Since this is example code and we aren't really concerned about breaking changes, it's probably better to just remove FromRobot
entirely and make sure the example shows current best practices.
Same for the Summation
api.
testutils/inject/robot.go
Outdated
|
||
// GetResource calls the injected ResourceByName or the real version of GetResource. | ||
func (r *Robot) GetResource(name resource.Name) (resource.Resource, error) { | ||
return r.ResourceByNameFunc(name) |
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.
(minor) probably better to return r.ResourceByName(name)
, which checks if ResourceByNameFunc
is nil and acts accordingly.
New Interface called Provider with a GetResource method.
GetResource is implemented in all the Resources respectively to either fetch the named resource from a collection of dependencies or from the given robot based on what was passed in.
All instances of Resource.FromRobot and Resource.FromDependencies were replaced with Resource.GetResource.