diff --git a/atproto/syntax/aturi.go b/atproto/syntax/aturi.go index cdf340fc..cd499fec 100644 --- a/atproto/syntax/aturi.go +++ b/atproto/syntax/aturi.go @@ -62,14 +62,14 @@ func (n ATURI) Authority() AtIdentifier { // Returns path segment, without leading slash, as would be used in an atproto repository key. Or empty string if there is no path. func (n ATURI) Path() string { parts := strings.SplitN(string(n), "/", 5) - if len(parts) < 3 { + if len(parts) < 4 { // something has gone wrong (would not validate) return "" } - if len(parts) == 3 { - return parts[2] + if len(parts) == 4 { + return parts[3] } - return parts[2] + "/" + parts[3] + return parts[3] + "/" + parts[4] } // Returns a valid NSID if there is one in the appropriate part of the path, otherwise empty. diff --git a/atproto/syntax/aturi_test.go b/atproto/syntax/aturi_test.go index 2851e35a..4ba27830 100644 --- a/atproto/syntax/aturi_test.go +++ b/atproto/syntax/aturi_test.go @@ -20,11 +20,20 @@ func TestInteropATURIsValid(t *testing.T) { if len(line) == 0 || line[0] == '#' { continue } - _, err := ParseATURI(line) + aturi, err := ParseATURI(line) if err != nil { fmt.Println("FAILED, GOOD: " + line) } assert.NoError(err) + + // check that Path() is working + col := aturi.Collection() + rkey := aturi.RecordKey() + if rkey != "" { + assert.Equal(col.String()+"/"+rkey.String(), aturi.Path()) + } else if col != "" { + assert.Equal(col.String(), aturi.Path()) + } } assert.NoError(scanner.Err()) } @@ -67,7 +76,22 @@ func TestATURIParts(t *testing.T) { rkey := uri.RecordKey() assert.Equal(parts[3], rkey.String()) } +} + +func TestATURIPath(t *testing.T) { + assert := assert.New(t) + uri1, err := ParseATURI("at://did:abc:123/io.nsid.someFunc/record-key") + assert.NoError(err) + assert.Equal("io.nsid.someFunc/record-key", uri1.Path()) + + uri2, err := ParseATURI("at://did:abc:123/io.nsid.someFunc") + assert.NoError(err) + assert.Equal("io.nsid.someFunc", uri2.Path()) + + uri3, err := ParseATURI("at://did:abc:123") + assert.NoError(err) + assert.Equal("", uri3.Path()) } func TestATURINormalize(t *testing.T) { @@ -93,5 +117,6 @@ func TestATURINoPanic(t *testing.T) { _ = bad.RecordKey() _ = bad.Normalize() _ = bad.String() + _ = bad.Path() } }