fork of indigo with slightly nicer lexgen

syntax: path parsing helper

Changed files
+67
atproto
+26
atproto/syntax/path.go
··· 1 + package syntax 2 + 3 + import ( 4 + "errors" 5 + "fmt" 6 + "strings" 7 + ) 8 + 9 + // Parses an atproto repo path string in to "collection" (NSID) and record key parts. 10 + // 11 + // Does not return partial success: either both collection and record key are complete (and error is nil), or both are empty string (and error is not nil) 12 + func ParseRepoPath(raw string) (NSID, RecordKey, error) { 13 + parts := strings.SplitN(raw, "/", 3) 14 + if len(parts) != 2 { 15 + return "", "", errors.New("expected path to have two parts, separated by single slash") 16 + } 17 + nsid, err := ParseNSID(parts[0]) 18 + if err != nil { 19 + return "", "", fmt.Errorf("collection part of path not a valid NSID: %w", err) 20 + } 21 + rkey, err := ParseRecordKey(parts[1]) 22 + if err != nil { 23 + return "", "", fmt.Errorf("record key part of path not valid: %w", err) 24 + } 25 + return nsid, rkey, nil 26 + }
+41
atproto/syntax/path_test.go
··· 1 + package syntax 2 + 3 + import ( 4 + "testing" 5 + 6 + "github.com/stretchr/testify/assert" 7 + ) 8 + 9 + func TestRepoPath(t *testing.T) { 10 + assert := assert.New(t) 11 + 12 + testValid := [][]string{ 13 + {"app.bsky.feed.post/asdf", "app.bsky.feed.post", "asdf"}, 14 + } 15 + 16 + testErr := []string{ 17 + "", 18 + "/", 19 + "/app.bsky.feed.post/asdf", 20 + "/asdf", 21 + "./app.bsky.feed.post", 22 + "blob/asdf", 23 + "app.bsky.feed.post/", 24 + "app.bsky.feed.post/.", 25 + "app.bsky.feed.post/!", 26 + } 27 + 28 + for _, parts := range testValid { 29 + nsid, rkey, err := ParseRepoPath(parts[0]) 30 + assert.NoError(err) 31 + assert.Equal(parts[1], nsid.String()) 32 + assert.Equal(parts[2], rkey.String()) 33 + } 34 + 35 + for _, raw := range testErr { 36 + nsid, rkey, err := ParseRepoPath(raw) 37 + assert.Error(err) 38 + assert.Equal("", nsid.String()) 39 + assert.Equal("", rkey.String()) 40 + } 41 + }