github.com/argoproj/argo-events@v1.9.1/common/saramaconfig.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/IBM/sarama"
     8  	"github.com/spf13/viper"
     9  )
    10  
    11  // GetSaramaConfigFromYAMLString parse yaml string to sarama.config.
    12  // Note: All the time.Duration config can not be correctly decoded because it does not implement the decode function.
    13  func GetSaramaConfigFromYAMLString(yaml string) (*sarama.Config, error) {
    14  	v := viper.New()
    15  	v.SetConfigType("yaml")
    16  	if err := v.ReadConfig(bytes.NewBufferString(yaml)); err != nil {
    17  		return nil, err
    18  	}
    19  	cfg := sarama.NewConfig()
    20  	cfg.Producer.Return.Successes = true
    21  	if err := v.Unmarshal(cfg); err != nil {
    22  		return nil, fmt.Errorf("unable to decode into struct, %w", err)
    23  	}
    24  	if err := cfg.Validate(); err != nil {
    25  		return nil, fmt.Errorf("failed validating sarama config, %w", err)
    26  	}
    27  	return cfg, nil
    28  }