From f6ad289b8d27219c47aefec24113ccb02a62dd99 Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 24 Jul 2025 15:55:09 -0600 Subject: refactor: extract defaultEntity type --- app/domain/entity.go | 34 +++++++++++++++++++++++++++++ app/domain/entity_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ app/domain/sparkle.go | 17 ++++----------- app/domain/sparkle_test.go | 9 ++++++++ 4 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 app/domain/entity_test.go (limited to 'app/domain') diff --git a/app/domain/entity.go b/app/domain/entity.go index 0377c51..0dc3995 100644 --- a/app/domain/entity.go +++ b/app/domain/entity.go @@ -1,6 +1,40 @@ package domain +import ( + "errors" + + v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" +) + type Entity interface { Identifiable Validate() error } + +type defaultEntity struct { + ID ID `json:"id" jsonapi:"primary,entities"` +} + +func (s *defaultEntity) GetID() ID { + return s.ID +} + +func (s *defaultEntity) SetID(id ID) error { + s.ID = id + return nil +} + +func (s *defaultEntity) ToGID() string { + return "gid://sparkle/Entity/" + s.ID.String() +} + +func (self *defaultEntity) ToObjectReference() *v1.ObjectReference { + return &v1.ObjectReference{ + ObjectType: "entity", + ObjectId: self.ID.String(), + } +} + +func (s *defaultEntity) Validate() error { + return errors.New("method Validate not implemented") +} diff --git a/app/domain/entity_test.go b/app/domain/entity_test.go new file mode 100644 index 0000000..ee3fbae --- /dev/null +++ b/app/domain/entity_test.go @@ -0,0 +1,54 @@ +package domain + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/xlgmokha/x/pkg/x" +) + +func TestDefaultEntity(t *testing.T) { + type example struct { + defaultEntity + } + + t.Run("GetID", func(t *testing.T) { + t.Run("returns the assigned ID", func(t *testing.T) { + example := x.New[*example](WithULID[*example]()) + + assert.Equal(t, example.ID, example.GetID()) + }) + }) + + t.Run("SetID", func(t *testing.T) { + t.Run("assigns a new ID", func(t *testing.T) { + id := ID("1") + example := x.New[*example]() + example.SetID(id) + + assert.Equal(t, id, example.GetID()) + }) + + }) + + t.Run("ToGID", func(t *testing.T) { + example := x.New[*example](WithULID[*example]()) + gid := example.ToGID() + + assert.Equal(t, fmt.Sprintf("gid://sparkle/Entity/%s", example.ID), gid) + }) + + t.Run("ToObjectReference", func(t *testing.T) { + t.Run("returns a valid object reference", func(t *testing.T) { + example := x.New[*example](WithULID[*example]()) + reference := example.ToObjectReference() + + require.NotNil(t, reference) + require.NoError(t, reference.Validate()) + require.NoError(t, reference.ValidateAll()) + assert.Equal(t, fmt.Sprintf("object_type:\"entity\" object_id:\"%s\"", example.ID), reference.String()) + }) + }) +} diff --git a/app/domain/sparkle.go b/app/domain/sparkle.go index 2ea5a53..10e24de 100644 --- a/app/domain/sparkle.go +++ b/app/domain/sparkle.go @@ -9,10 +9,10 @@ import ( ) type Sparkle struct { - ID ID `json:"id" jsonapi:"primary,sparkles"` Sparklee string `json:"sparklee" jsonapi:"attr,sparklee"` Author *User `json:"author" jsonapi:"attr,author"` Reason string `json:"reason" jsonapi:"attr,reason"` + defaultEntity } var SparkleRegex = regexp.MustCompile(`\A\s*(?P@\w+)\s+(?P.+)\z`) @@ -35,21 +35,12 @@ func NewSparkle(text string) (*Sparkle, error) { } return &Sparkle{ - ID: ID(pls.GenerateULID()), - Sparklee: matches[SparkleeIndex], - Reason: matches[ReasonIndex], + defaultEntity: defaultEntity{ID: ID(pls.GenerateULID())}, + Sparklee: matches[SparkleeIndex], + Reason: matches[ReasonIndex], }, nil } -func (s *Sparkle) GetID() ID { - return s.ID -} - -func (s *Sparkle) SetID(id ID) error { - s.ID = id - return nil -} - func (s *Sparkle) ToGID() string { return "gid://sparkle/Sparkle/" + s.ID.String() } diff --git a/app/domain/sparkle_test.go b/app/domain/sparkle_test.go index c040d89..101d271 100644 --- a/app/domain/sparkle_test.go +++ b/app/domain/sparkle_test.go @@ -52,6 +52,15 @@ func TestSparkle(t *testing.T) { }) }) + t.Run("ToGID", func(t *testing.T) { + t.Run("returns a valid global id", func(t *testing.T) { + sparkle := x.New[*Sparkle](WithULID[*Sparkle]()) + gid := sparkle.ToGID() + + assert.Equal(t, fmt.Sprintf("gid://sparkle/Sparkle/%s", sparkle.ID), gid) + }) + }) + t.Run("ToObjectReference", func(t *testing.T) { t.Run("returns a valid object reference", func(t *testing.T) { sparkle := x.New[*Sparkle](WithULID[*Sparkle]()) -- cgit v1.2.3