-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer_builder.go
75 lines (66 loc) · 2.38 KB
/
consumer_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package danube
import (
"errors"
// Path to your generated proto package
)
// ConsumerBuilder is a builder for creating a new Consumer instance. It allows
// setting various properties for the consumer such as topic, name, subscription,
// subscription type, and options.
type ConsumerBuilder struct {
client *DanubeClient
topic string
consumerName string
subscription string
subscriptionType *SubType
consumerOptions ConsumerOptions
}
func newConsumerBuilder(client *DanubeClient) *ConsumerBuilder {
return &ConsumerBuilder{client: client}
}
// WithTopic sets the topic name for the consumer. This is a required field.
//
// Parameters:
// - topic: The name of the topic for the consumer.
func (b *ConsumerBuilder) WithTopic(topic string) *ConsumerBuilder {
b.topic = topic
return b
}
// WithConsumerName sets the name of the consumer. This is a required field.
//
// Parameters:
// - name: The name assigned to the consumer instance.
func (b *ConsumerBuilder) WithConsumerName(name string) *ConsumerBuilder {
b.consumerName = name
return b
}
// WithSubscription sets the name of the subscription for the consumer. This is a required field.
//
// Parameters:
// - subscription: The name of the subscription for the consumer.
func (b *ConsumerBuilder) WithSubscription(subscription string) *ConsumerBuilder {
b.subscription = subscription
return b
}
// WithSubscriptionType sets the type of subscription for the consumer. This field is optional.
//
// Parameters:
// - subType: The type of subscription (e.g., EXCLUSIVE, SHARED, FAILOVER).
func (b *ConsumerBuilder) WithSubscriptionType(subType SubType) *ConsumerBuilder {
b.subscriptionType = &subType
return b
}
// Build creates a new Consumer instance using the settings configured in the ConsumerBuilder.
// It performs validation to ensure that all required fields are set before creating the consumer.
//
// Returns:
// - *Consumer: A pointer to the newly created Consumer instance if successful.
// - error: An error if required fields are missing or if consumer creation fails.
func (b *ConsumerBuilder) Build() (*Consumer, error) {
if b.topic == "" || b.consumerName == "" || b.subscription == "" {
return nil, errors.New("missing required fields")
}
return newConsumer(b.client, b.topic, b.consumerName, b.subscription, b.subscriptionType, b.consumerOptions), nil
}
type ConsumerOptions struct {
Others string
}