Skip to content

Commit

Permalink
Merge pull request #166 from xmidt-org/locator
Browse files Browse the repository at this point in the history
Change *Locator to be Locator.
  • Loading branch information
schmidtw committed Mar 12, 2024
2 parents 5e79fc5 + dedf960 commit 727d830
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
10 changes: 5 additions & 5 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ type Locator struct {
}

// ParseLocator parses a raw locator string into a canonicalized locator.
func ParseLocator(locator string) (*Locator, error) {
func ParseLocator(locator string) (Locator, error) {
match := LocatorPattern.FindStringSubmatch(locator)
if match == nil {
return nil, ErrorInvalidLocator
return Locator{}, ErrorInvalidLocator
}

var l Locator
Expand All @@ -154,18 +154,18 @@ func ParseLocator(locator string) (*Locator, error) {
switch l.Scheme {
case SchemeDNS, SchemeEvent:
if l.Authority == "" {
return nil, ErrorInvalidLocator
return Locator{}, ErrorInvalidLocator
}
case SchemeMAC, SchemeUUID, SchemeSerial, SchemeSelf:
id, err := makeDeviceID(l.Scheme, l.Authority)
if err != nil {
return nil, err
return Locator{}, err
}
l.ID = id
default:
}

return &l, nil
return l, nil
}

// HasDeviceID returns true if the locator is a device identifier.
Expand Down
29 changes: 15 additions & 14 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ func TestParseLocator(t *testing.T) {
tests := []struct {
description string
locator string
want *Locator
want Locator
str string
expectedErr error
}{
{
description: "cpe locator",
locator: "mac:112233445566",
str: "mac:112233445566",
want: &Locator{
want: Locator{
Scheme: SchemeMAC,
Authority: "112233445566",
ID: "mac:112233445566",
Expand All @@ -135,7 +135,7 @@ func TestParseLocator(t *testing.T) {
description: "cpe locator ensure lowercase",
locator: "Mac:112233445566",
str: "mac:112233445566",
want: &Locator{
want: Locator{
Scheme: SchemeMAC,
Authority: "112233445566",
ID: "mac:112233445566",
Expand All @@ -144,7 +144,7 @@ func TestParseLocator(t *testing.T) {
description: "locator with service",
locator: "DNS:foo.bar.com/service",
str: "dns:foo.bar.com/service",
want: &Locator{
want: Locator{
Scheme: SchemeDNS,
Authority: "foo.bar.com",
Service: "service",
Expand All @@ -153,7 +153,7 @@ func TestParseLocator(t *testing.T) {
description: "locator with service everything",
locator: "event:something/service/ignored",
str: "event:something/service/ignored",
want: &Locator{
want: Locator{
Scheme: SchemeEvent,
Authority: "something",
Service: "service",
Expand All @@ -163,7 +163,7 @@ func TestParseLocator(t *testing.T) {
description: "self locator with service",
locator: "SELF:/service",
str: "self:/service",
want: &Locator{
want: Locator{
Scheme: SchemeSelf,
Service: "service",
ID: "self:",
Expand All @@ -172,7 +172,7 @@ func TestParseLocator(t *testing.T) {
description: "self locator with service everything",
locator: "self:/service/ignored",
str: "self:/service/ignored",
want: &Locator{
want: Locator{
Scheme: SchemeSelf,
Service: "service",
Ignored: "/ignored",
Expand All @@ -185,23 +185,23 @@ func TestParseLocator(t *testing.T) {
description: "dns scheme",
locator: "dns:foo.bar.com",
str: "dns:foo.bar.com",
want: &Locator{
want: Locator{
Scheme: SchemeDNS,
Authority: "foo.bar.com",
},
}, {
description: "event scheme",
locator: "event:targetedEvent",
str: "event:targetedEvent",
want: &Locator{
want: Locator{
Scheme: SchemeEvent,
Authority: "targetedEvent",
},
}, {
description: "mac scheme",
locator: "mac:112233445566",
str: "mac:112233445566",
want: &Locator{
want: Locator{
Scheme: SchemeMAC,
Authority: "112233445566",
ID: "mac:112233445566",
Expand All @@ -210,7 +210,7 @@ func TestParseLocator(t *testing.T) {
description: "serial scheme",
locator: "serial:AsdfSerial",
str: "serial:AsdfSerial",
want: &Locator{
want: Locator{
Scheme: SchemeSerial,
Authority: "AsdfSerial",
ID: "serial:AsdfSerial",
Expand All @@ -219,7 +219,7 @@ func TestParseLocator(t *testing.T) {
description: "uuid scheme",
locator: "uuid:bbee1f69-2f64-4aa9-a422-27d68b40b152",
str: "uuid:bbee1f69-2f64-4aa9-a422-27d68b40b152",
want: &Locator{
want: Locator{
Scheme: SchemeUUID,
Authority: "bbee1f69-2f64-4aa9-a422-27d68b40b152",
ID: "uuid:bbee1f69-2f64-4aa9-a422-27d68b40b152",
Expand All @@ -228,7 +228,7 @@ func TestParseLocator(t *testing.T) {
description: "self scheme",
locator: "self:",
str: "self:",
want: &Locator{
want: Locator{
Scheme: SchemeSelf,
ID: "self:",
},
Expand Down Expand Up @@ -261,7 +261,8 @@ func TestParseLocator(t *testing.T) {
assert.ErrorIs(err, tc.expectedErr)
assert.Equal(tc.want, got)

if tc.want != nil {
l := Locator{}
if tc.want != l {
assert.Equal(tc.str, got.String())
}
})
Expand Down

0 comments on commit 727d830

Please sign in to comment.