Skip to content

Commit

Permalink
s/naming: add ParseComponentRef function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewphelpsj committed Jun 20, 2024
1 parent 99f2aac commit a3a9130
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions snap/naming/componentref.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func NewComponentRef(snapName, componentName string) ComponentRef {
return ComponentRef{SnapName: snapName, ComponentName: componentName}
}

// ParseComponentRef parses a string in the form <snap>+<comp> into a
// ComponentRef.
func ParseComponentRef(s string) (ComponentRef, error) {
snapName, componentName, err := SplitFullComponentName(s)
if err != nil {
return ComponentRef{}, err
}

return ComponentRef{SnapName: snapName, ComponentName: componentName}, nil
}

// SplitFullComponentName splits <snap>+<comp> in <snap> and <comp> strings.
func SplitFullComponentName(fullComp string) (string, string, error) {
names := strings.Split(fullComp, "+")
Expand Down
10 changes: 10 additions & 0 deletions snap/naming/componentref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ func (s *componentRefSuite) TestSplitFullComponentNameErr(c *C) {
c.Check(comp, Equals, "")
}
}

func (s *componentRefSuite) TestParseComponentRef(c *C) {
cr, err := naming.ParseComponentRef("foo+bar")
c.Check(err, IsNil)
c.Check(cr.SnapName, Equals, "foo")
c.Check(cr.ComponentName, Equals, "bar")

cr, err = naming.ParseComponentRef("foo+bar+")
c.Assert(err, ErrorMatches, `.*incorrect component name "foo\+bar\+"`)
}

0 comments on commit a3a9130

Please sign in to comment.