Skip to content

Commit

Permalink
Merge pull request #171 from xmidt-org/fix-169-170
Browse files Browse the repository at this point in the history
fix: Locator parsing fixes
  • Loading branch information
schmidtw committed Mar 15, 2024
2 parents 04936e3 + 2e1866f commit 6606e14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
18 changes: 13 additions & 5 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var (

// LocatorPattern is the precompiled regular expression that all locators must match.
LocatorPattern = regexp.MustCompile(
`^(?P<scheme>(?i)mac|uuid|dns|serial|event|self):(?P<authority>[^/]+)?(?P<service>/[^/]+)?(?P<ignored>/[^/]+)?`,
`^(?P<scheme>(?i)mac|uuid|dns|serial|event|self):(?P<authority>[^/]+)?(?P<service>/[^/]+)?(?P<ignored>.+)?`,
)
)

Expand Down Expand Up @@ -152,10 +152,18 @@ func ParseLocator(locator string) (Locator, error) {

// If the locator is a device identifier, then we need to parse it.
switch l.Scheme {
case SchemeDNS, SchemeEvent:
case SchemeDNS:
if l.Authority == "" {
return Locator{}, ErrorInvalidLocator
}
case SchemeEvent:
if l.Authority == "" {
return Locator{}, ErrorInvalidLocator
}
if l.Service != "" {
l.Ignored = "/" + l.Service + l.Ignored
l.Service = ""
}
case SchemeMAC, SchemeUUID, SchemeSerial, SchemeSelf:
id, err := makeDeviceID(l.Scheme, l.Authority)
if err != nil {
Expand Down Expand Up @@ -187,10 +195,10 @@ func (l Locator) String() string {
if l.Service != "" {
buf.WriteString("/")
buf.WriteString(l.Service)
}

if l.Ignored != "" {
buf.WriteString(l.Ignored)
}
if l.Ignored != "" {
buf.WriteString(l.Ignored)
}

return buf.String()
Expand Down
21 changes: 11 additions & 10 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,23 @@ func TestParseLocator(t *testing.T) {
},
}, {
description: "locator with service",
locator: "DNS:foo.bar.com/service",
str: "dns:foo.bar.com/service",
locator: "DNS:foo.bar.com/service/ignored/really/really/ignored",
str: "dns:foo.bar.com/service/ignored/really/really/ignored",
want: Locator{
Scheme: SchemeDNS,
Authority: "foo.bar.com",
Service: "service",
Ignored: "/ignored/really/really/ignored",
},
}, {
description: "locator with service everything",
locator: "event:something/service/ignored",
str: "event:something/service/ignored",
locator: "event:event_name/ignored/really/really/ignored",
str: "event:event_name/ignored/really/really/ignored",
want: Locator{
Scheme: SchemeEvent,
Authority: "something",
Service: "service",
Ignored: "/ignored",
Authority: "event_name",
Service: "",
Ignored: "/ignored/really/really/ignored",
},
}, {
description: "self locator with service",
Expand All @@ -170,12 +171,12 @@ func TestParseLocator(t *testing.T) {
},
}, {
description: "self locator with service everything",
locator: "self:/service/ignored",
str: "self:/service/ignored",
locator: "self:/service/ignored/really/really/ignored",
str: "self:/service/ignored/really/really/ignored",
want: Locator{
Scheme: SchemeSelf,
Service: "service",
Ignored: "/ignored",
Ignored: "/ignored/really/really/ignored",
ID: "self:",
},
},
Expand Down

0 comments on commit 6606e14

Please sign in to comment.