Skip to content

Commit

Permalink
#3 added logic to fetch topic collection by name or ID
Browse files Browse the repository at this point in the history
  • 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.
82 changes: 82 additions & 0 deletions internal/client/common/topic_collection.go
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
}

0 comments on commit c912c87

Please sign in to comment.