Skip to content

Conversation

dhritinaidu
Copy link
Member

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.

@dhritinaidu dhritinaidu requested review from a team, njooma and stuqdog and removed request for a team September 24, 2025 15:36
@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Sep 24, 2025
Copy link
Contributor

Warning your change may break code samples. If your change modifies any of the following functions please contact @viamrobotics/fleet-management. Thanks!

component function
base IsMoving
button Push
genericcomponent DoCommand
board GPIOPinByName
camera Properties
encoder Properties
motor IsMoving
sensor Readings
servo Position
arm EndPosition
audio MediaProperties
gantry Lengths
gripper IsMoving
input_controller Controls
movement_sensor LinearAcceleration
power_sensor Power
pose_tracker Poses
genericservice DoCommand
motion GetPose
vision GetProperties

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Sep 24, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Sep 25, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Sep 25, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Sep 25, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Sep 25, 2025
Copy link
Member

@njooma njooma left a 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:

  1. 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 within GetResource.
  2. You're marking methods as deprecated (good thing!). You should provide the alternative so that users know what to use instead


// 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)
Copy link
Member

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


// 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)
Copy link
Member

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


// 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, just return this


// 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return rc.ResourceByName(name)


// GetResource calls the injected ResourceByName or the real version of GetResource.
func (r *Robot) GetResource(name resource.Name) (resource.Resource, error) {
r.Mu.RLock()
Copy link
Member

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.
Copy link
Member

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

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Sep 26, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Sep 26, 2025
@dhritinaidu dhritinaidu requested a review from njooma September 26, 2025 19:36
Copy link
Member

@stuqdog stuqdog left a 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!

}

// 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.
Copy link
Member

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.

Comment on lines 26 to 29
// 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
Copy link
Member

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.


// 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)
Copy link
Member

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.

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe to test This pull request is marked safe to test from a trusted zone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants