Skip to content
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

fix: Locator parsing fixes #171

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

// 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 @@

// 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
}

Check warning on line 162 in id.go

View check run for this annotation

Codecov / codecov/patch

id.go#L161-L162

Added lines #L161 - L162 were not covered by tests
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 @@
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
Loading