github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/mq/mqtt/mqtt.conf.go (about)

     1  package mqtt
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/asaskevich/govalidator"
     8  )
     9  
    10  type Conf struct {
    11  	Address  string `json:"address" valid:"dialstring,required"`
    12  	DumpData bool   `json:"dump"`
    13  	UserName string `json:"userName"`
    14  	Password string `json:"password"`
    15  	CertPath string `json:"cert"`
    16  }
    17  
    18  func NewConf(j string) (*Conf, error) {
    19  	conf := Conf{}
    20  	err := json.Unmarshal([]byte(j), &conf)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	if b, err := govalidator.ValidateStruct(&conf); !b {
    25  		err = fmt.Errorf("mqtt 配置文件有误:%v", err)
    26  		return nil, err
    27  	}
    28  	return &conf, nil
    29  }