-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 added logic to fetch topic collection by name or ID
- Loading branch information
Krishnakant C
authored and
Krishnakant C
committed
Sep 6, 2024
1 parent
ca2006a
commit c912c87
Showing
1 changed file
with
82 additions
and
0 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,82 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
// TopicCollection represents a collection of topics defined by name or ID. | ||
type TopicCollection interface { | ||
TopicIds() []Uuid | ||
TopicNames() []string | ||
} | ||
|
||
// TopicIdCollection represents a collection of topics defined by their topic ID. | ||
type TopicIdCollection struct { | ||
topicIds []Uuid | ||
mu sync.RWMutex | ||
} | ||
|
||
// NewTopicIdCollection creates a new TopicIdCollection with the given topic IDs. | ||
func NewTopicIdCollection(topicIds []Uuid) *TopicIdCollection { | ||
return &TopicIdCollection{ | ||
topicIds: append([]Uuid{}, topicIds...), // Create a copy to prevent external modifications | ||
} | ||
} | ||
|
||
// TopicIds returns a read-only slice of topic IDs. | ||
func (c *TopicIdCollection) TopicIds() []Uuid { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
return append([]Uuid{}, c.topicIds...) // Return a copy to ensure immutability | ||
} | ||
|
||
// TopicNameCollection represents a collection of topics defined by their topic name. | ||
type TopicNameCollection struct { | ||
topicNames []string | ||
mu sync.RWMutex | ||
} | ||
|
||
// NewTopicNameCollection creates a new TopicNameCollection with the given topic names. | ||
func NewTopicNameCollection(topicNames []string) *TopicNameCollection { | ||
return &TopicNameCollection{ | ||
topicNames: append([]string{}, topicNames...), // Create a copy to prevent external modifications | ||
} | ||
} | ||
|
||
// TopicNames returns a read-only slice of topic names. | ||
func (c *TopicNameCollection) TopicNames() []string { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
return append([]string{}, c.topicNames...) // Return a copy to ensure immutability | ||
} | ||
|
||
// NewTopic creates a new topic with the given ID and name. | ||
func NewTopic(id Uuid, name string) (*Topic, error) { | ||
if id.Compare(ZeroUUID) == 0 { | ||
return nil, errors.New("topic ID cannot be empty") | ||
} | ||
if name == "" { | ||
return nil, errors.New("topic name cannot be empty") | ||
} | ||
return &Topic{ | ||
id: id, | ||
name: name, | ||
}, nil | ||
} | ||
|
||
// Topic represents a single topic with an ID and name. | ||
type Topic struct { | ||
id Uuid | ||
name string | ||
} | ||
|
||
// ID returns the topic ID. | ||
func (t *Topic) ID() Uuid { | ||
return t.id | ||
} | ||
|
||
// Name returns the topic name. | ||
func (t *Topic) Name() string { | ||
return t.name | ||
} |