did: add a MustParse function

This commit is contained in:
Michael Muré
2024-09-25 15:46:01 +02:00
parent 4a655506f9
commit 952a6bb922
2 changed files with 22 additions and 0 deletions

View File

@@ -116,3 +116,11 @@ func Parse(str string) (DID, error) {
buf = append(buf, suffix...) buf = append(buf, suffix...)
return DID{str: string(buf), code: DIDCore}, nil return DID{str: string(buf), code: DIDCore}, nil
} }
func MustParse(str string) DID {
did, err := Parse(str)
if err != nil {
panic(err)
}
return did
}

View File

@@ -2,6 +2,8 @@ package did
import ( import (
"testing" "testing"
"github.com/stretchr/testify/require"
) )
func TestParseDIDKey(t *testing.T) { func TestParseDIDKey(t *testing.T) {
@@ -15,6 +17,18 @@ func TestParseDIDKey(t *testing.T) {
} }
} }
func TestMustParseDIDKey(t *testing.T) {
str := "did:key:z6Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z"
require.NotPanics(t, func() {
d := MustParse(str)
require.Equal(t, str, d.String())
})
str = "did:key:z7Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z"
require.Panics(t, func() {
MustParse(str)
})
}
func TestDecodeDIDKey(t *testing.T) { func TestDecodeDIDKey(t *testing.T) {
str := "did:key:z6Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z" str := "did:key:z6Mkod5Jr3yd5SC7UDueqK4dAAw5xYJYjksy722tA9Boxc4z"
d0, err := Parse(str) d0, err := Parse(str)