Skip to content

Commit

Permalink
chore: separate encoder type
Browse files Browse the repository at this point in the history
  • Loading branch information
lehainam-dev committed Jan 24, 2024
1 parent 175ef90 commit 0bd891e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 3 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ func getPublisher(c *cli.Context, redisClient *redis.Client, topic string) (publ
}

func getMessageEncoder(c *cli.Context) encoder.Encoder {
publisherType := c.String(publisherTypeFlag.Name)
switch publisherType {
case publisherpkg.PublisherTypeKafka:
encoderType := c.String(encoderTypeFlag.Name)
switch encoderType {
case encoder.EncoderTypeProtobuf:
return encoder.NewProtobufEncoder()
default:
return encoder.NewJSONEncoder()
Expand Down
16 changes: 15 additions & 1 deletion internal/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,20 @@ var (
Usage: "A list of address for connecting to kafka. Default: localhost:9092",
}

encoderTypeFlag = &cli.StringFlag{
Name: "encoder-type",
EnvVars: []string{"ENCODER_TYPE"},
Value: "",
Required: true,
Usage: "Type of encoder. Supports: `protobuf`, `json` (default)",
}

publisherTypeFlag = &cli.StringFlag{
Name: "publisher-type",
EnvVars: []string{"PUBLISHER_TYPE"},
Value: "",
Required: true,
Usage: "Type of publisher. Supports: `kafka`, `redis-stream`",
Usage: "Type of publisher. Supports: `kafka`, `redis-stream` (default)",
}
publisherTopicFlag = &cli.StringFlag{
Name: "publisher-topic",
Expand Down Expand Up @@ -160,6 +168,11 @@ func NewKafkaFlags() []cli.Flag {
return []cli.Flag{kafkaAddrsFlag}
}

// NewEncoderFlags returns flags for encoder.
func NewEncoderFlags() []cli.Flag {
return []cli.Flag{encoderTypeFlag}
}

// NewPublisherFlags returns flags for publishers.
func NewPublisherFlags() []cli.Flag {
return []cli.Flag{publisherTypeFlag, publisherMaxLenFlag, publisherTopicFlag}
Expand All @@ -181,6 +194,7 @@ func NewFlags() []cli.Flag {
}
flags = append(flags, NewSentryFlags()...)
flags = append(flags, NewRedisFlags()...)
flags = append(flags, NewEncoderFlags()...)
flags = append(flags, NewKafkaFlags()...)
flags = append(flags, NewPublisherFlags()...)
flags = append(flags, NewBlockKeeperFlags()...)
Expand Down
6 changes: 6 additions & 0 deletions pkg/encoder/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package encoder

const (
EncoderTypeProtobuf = "protobuf"
EncoderTypeJSON = "json"
)

0 comments on commit 0bd891e

Please sign in to comment.