Skip to content

Commit

Permalink
RSDK-8767 - populate local_name and remote_path to all resource names…
Browse files Browse the repository at this point in the history
… sent from RDK (#4519)
  • Loading branch information
Kschappacher authored Nov 13, 2024
1 parent b460122 commit 06fdd6e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
10 changes: 6 additions & 4 deletions protoutils/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
// ResourceNameToProto converts a resource.Name to its proto counterpart.
func ResourceNameToProto(name resource.Name) *commonpb.ResourceName {
return &commonpb.ResourceName{
Namespace: string(name.API.Type.Namespace),
Type: name.API.Type.Name,
Subtype: name.API.SubtypeName,
Name: name.ShortName(),
Namespace: string(name.API.Type.Namespace),
Type: name.API.Type.Name,
Subtype: name.API.SubtypeName,
Name: name.ShortName(),
RemotePath: name.RemoteNameToRemoteArray(),
LocalName: name.Name,
}
}

Expand Down
17 changes: 17 additions & 0 deletions protoutils/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"go.viam.com/test"
"google.golang.org/protobuf/types/known/wrapperspb"

"go.viam.com/rdk/resource"
)

func TestStringToAnyPB(t *testing.T) {
Expand Down Expand Up @@ -40,3 +42,18 @@ func TestStringToAnyPB(t *testing.T) {
wrappedVal4 := wrapperspb.String("abcd")
test.That(t, anyVal.MessageIs(wrappedVal4), test.ShouldBeTrue)
}

func TestResourceNameToProto(t *testing.T) {
resourceName := resource.Name{
Name: "totallyLegitResource",
Remote: "remote1:remote2:remote3",
API: resource.NewAPI("space", "fake", "fakeFake"),
}
resourceNameProto := ResourceNameToProto(resourceName)
finalResource := ResourceNameFromProto(resourceNameProto)

test.That(t, resourceNameProto.LocalName, test.ShouldEqual, "totallyLegitResource")
test.That(t, resourceNameProto.RemotePath, test.ShouldResemble, []string{"remote1", "remote2", "remote3"})
test.That(t, resourceNameProto.Name, test.ShouldEqual, "remote1:remote2:remote3:totallyLegitResource")
test.That(t, finalResource, test.ShouldResemble, resourceName)
}
8 changes: 8 additions & 0 deletions resource/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ func (n Name) String() string {
return name
}

// RemoteNameToRemoteArray returns an ordered array of all of remotes in a resource name.
func (n Name) RemoteNameToRemoteArray() []string {
if n.Remote == "" {
return []string{}
}
return strings.Split(n.Remote, ":")
}

// SDPTrackName returns a valid SDP video/audio track name as defined in RFC 4566 (https://www.rfc-editor.org/rfc/rfc4566)
// where track names should not include colons.
func (n Name) SDPTrackName() string {
Expand Down
18 changes: 18 additions & 0 deletions resource/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,21 @@ func TestNamesToStrings(t *testing.T) {
test.That(t, NamesToStrings(tc.input), test.ShouldResemble, tc.output)
}
}

func TestRemoteNameToRemoteArray(t *testing.T) {
t.Run("name with multiple remote returns array of remotes", func(t *testing.T) {
nameTest := Name{
Remote: "foo:bar:wow",
}
remoteArray := nameTest.RemoteNameToRemoteArray()
test.That(t, remoteArray, test.ShouldResemble, []string{"foo", "bar", "wow"})
})

t.Run("name with empty remotes should return empty string array", func(t *testing.T) {
nameTest := Name{
Remote: "",
}
remoteArray := nameTest.RemoteNameToRemoteArray()
test.That(t, remoteArray, test.ShouldResemble, []string{})
})
}
26 changes: 16 additions & 10 deletions robot/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ var serverNewResource = arm.Named("")

var serverOneResourceResponse = []*commonpb.ResourceName{
{
Namespace: string(serverNewResource.API.Type.Namespace),
Type: serverNewResource.API.Type.Name,
Subtype: serverNewResource.API.SubtypeName,
Name: serverNewResource.Name,
Namespace: string(serverNewResource.API.Type.Namespace),
Type: serverNewResource.API.Type.Name,
Subtype: serverNewResource.API.SubtypeName,
Name: serverNewResource.Name,
RemotePath: []string{},
LocalName: "",
},
}

Expand Down Expand Up @@ -325,17 +327,21 @@ func TestServer(t *testing.T) {
expectedResp := []*pb.ResourceRPCSubtype{
{
Subtype: &commonpb.ResourceName{
Namespace: string(serverNewResource.API.Type.Namespace),
Type: serverNewResource.API.Type.Name,
Subtype: serverNewResource.API.SubtypeName,
Namespace: string(serverNewResource.API.Type.Namespace),
Type: serverNewResource.API.Type.Name,
Subtype: serverNewResource.API.SubtypeName,
RemotePath: []string{},
Name: "",
},
ProtoService: desc1.GetFullyQualifiedName(),
},
{
Subtype: &commonpb.ResourceName{
Namespace: string(otherAPI.Type.Namespace),
Type: otherAPI.Type.Name,
Subtype: otherAPI.SubtypeName,
Namespace: string(otherAPI.Type.Namespace),
Type: otherAPI.Type.Name,
Subtype: otherAPI.SubtypeName,
RemotePath: []string{},
Name: "",
},
ProtoService: desc2.GetFullyQualifiedName(),
},
Expand Down

0 comments on commit 06fdd6e

Please sign in to comment.