Skip to content

Commit

Permalink
fix: Locator parsing fixes
Browse files Browse the repository at this point in the history
- Fixes #169 where the output of where values are stored is wrong.
- Fixes #170 where the ignored value is truncated when it should not be.
  • Loading branch information
schmidtw committed Mar 15, 2024
1 parent 04936e3 commit 2e1866f
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
}

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 @@ 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 2e1866f

Please sign in to comment.