-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
81 changed files
with
1,270 additions
and
780 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package actoruid | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
"github.com/google/uuid" | ||
) | ||
|
||
const prefix = "urn:opg:poas:makeregister:users:" | ||
|
||
type UID struct{ value string } | ||
|
||
func New() UID { | ||
return UID{value: uuid.NewString()} | ||
} | ||
|
||
func FromRequest(r interface{ FormValue(string) string }) UID { | ||
return UID{value: r.FormValue("id")} | ||
} | ||
|
||
func (u UID) IsZero() bool { | ||
return len(u.value) == 0 | ||
} | ||
|
||
func (u UID) String() string { | ||
return u.value | ||
} | ||
|
||
func (u UID) PrefixedString() string { | ||
return prefix + u.value | ||
} | ||
|
||
func (u UID) MarshalJSON() ([]byte, error) { | ||
if u.value == "" { | ||
return []byte("null"), nil | ||
} | ||
|
||
return []byte(`"` + u.PrefixedString() + `"`), nil | ||
} | ||
|
||
func (u *UID) UnmarshalText(text []byte) error { | ||
if len(text) == 0 { | ||
return nil | ||
} | ||
|
||
uid, found := strings.CutPrefix(string(text), prefix) | ||
if !found { | ||
return errors.New("invalid uid prefix") | ||
} | ||
|
||
u.value = uid | ||
return nil | ||
} | ||
|
||
func (u UID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) { | ||
return attributevalue.Marshal(u.value) | ||
} | ||
|
||
func (u *UID) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error { | ||
return attributevalue.Unmarshal(av, &u.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package actoruid | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUID(t *testing.T) { | ||
uid := UID{value: "abc"} | ||
|
||
assert.Equal(t, "abc", uid.String()) | ||
assert.Equal(t, prefix+"abc", uid.PrefixedString()) | ||
} | ||
|
||
func TestUIDFromRequest(t *testing.T) { | ||
r, _ := http.NewRequest(http.MethodGet, "/?id=abc", nil) | ||
|
||
assert.Equal(t, UID{value: "abc"}, FromRequest(r)) | ||
} | ||
|
||
func TestUIDZero(t *testing.T) { | ||
assert.True(t, UID{}.IsZero()) | ||
assert.False(t, New().IsZero()) | ||
} | ||
|
||
func TestUIDJSON(t *testing.T) { | ||
uid := UID{value: "abc"} | ||
|
||
jsonData, _ := json.Marshal(uid) | ||
assert.Equal(t, `"urn:opg:poas:makeregister:users:abc"`, string(jsonData)) | ||
|
||
var a UID | ||
err := json.Unmarshal([]byte(`"urn:opg:poas:makeregister:users:abc"`), &a) | ||
assert.Nil(t, err) | ||
assert.Equal(t, a, uid) | ||
|
||
emptyData, _ := json.Marshal(UID{}) | ||
assert.Equal(t, `null`, string(emptyData)) | ||
|
||
var b UID | ||
err = json.Unmarshal([]byte(`null`), &b) | ||
assert.Nil(t, err) | ||
assert.True(t, b.IsZero()) | ||
|
||
var c UID | ||
err = json.Unmarshal([]byte(`""`), &c) | ||
assert.Nil(t, err) | ||
assert.True(t, c.IsZero()) | ||
} | ||
|
||
func TestUIDJSONInvalidPrefix(t *testing.T) { | ||
var v UID | ||
err := json.Unmarshal([]byte(`"urn:opg:poas:makeregister:users2:abc"`), &v) | ||
assert.ErrorContains(t, err, "invalid uid prefix") | ||
} | ||
|
||
func TestUIDAttributeValue(t *testing.T) { | ||
uid := UID{value: "abc"} | ||
|
||
avData, _ := attributevalue.Marshal(uid) | ||
assert.Equal(t, &types.AttributeValueMemberS{Value: "abc"}, avData) | ||
|
||
var b UID | ||
_ = attributevalue.Unmarshal(&types.AttributeValueMemberS{Value: "abc"}, &b) | ||
assert.Equal(t, b, uid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.