diff --git a/atproto/syntax/atidentifier.go b/atproto/syntax/atidentifier.go index 653594f28..21a0f8879 100644 --- a/atproto/syntax/atidentifier.go +++ b/atproto/syntax/atidentifier.go @@ -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 { @@ -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 { diff --git a/atproto/syntax/atidentifier_test.go b/atproto/syntax/atidentifier_test.go index 7bf1ff4e7..b8bce0276 100644 --- a/atproto/syntax/atidentifier_test.go +++ b/atproto/syntax/atidentifier_test.go @@ -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()) +}