Skip to content

Commit

Permalink
syntax: more AtIdentifier helpers and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bnewbold committed Sep 15, 2023
1 parent b1efee8 commit 898befd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions atproto/syntax/atidentifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func ParseAtIdentifier(raw string) (*AtIdentifier, error) {
return &AtIdentifier{Inner: handle}, nil
}

func (n AtIdentifier) IsHandle() bool {
_, ok := n.Inner.(Handle)
return ok
}

func (n AtIdentifier) AsHandle() (Handle, error) {
handle, ok := n.Inner.(Handle)
if ok {
Expand All @@ -32,6 +37,11 @@ func (n AtIdentifier) AsHandle() (Handle, error) {
return "", fmt.Errorf("AT Identifier is not a Handle")
}

func (n AtIdentifier) IsDID() bool {
_, ok := n.Inner.(DID)
return ok
}

func (n AtIdentifier) AsDID() (DID, error) {
did, ok := n.Inner.(DID)
if ok {
Expand Down
35 changes: 35 additions & 0 deletions atproto/syntax/atidentifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,38 @@ func TestInteropAtIdentifiersInvalid(t *testing.T) {
}
assert.NoError(scanner.Err())
}

func TestDowncase(t *testing.T) {
assert := assert.New(t)

aidh, err := ParseAtIdentifier("example.com")
assert.NoError(err)
assert.True(aidh.IsHandle())
assert.False(aidh.IsDID())
_, err = aidh.AsHandle()
assert.NoError(err)
_, err = aidh.AsDID()
assert.Error(err)

aidd, err := ParseAtIdentifier("did:web:example.com")
assert.NoError(err)
assert.False(aidd.IsHandle())
assert.True(aidd.IsDID())
_, err = aidd.AsHandle()
assert.Error(err)
_, err = aidd.AsDID()
assert.NoError(err)
}

func TestEmtpy(t *testing.T) {
assert := assert.New(t)

atid := AtIdentifier{}

assert.False(atid.IsHandle())
assert.False(atid.IsDID())
assert.Equal(atid, atid.Normalize())
atid.AsHandle()
atid.AsDID()
assert.Empty(atid.String())
}

0 comments on commit 898befd

Please sign in to comment.