github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/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  }
    16  
    17  func NewConf(j string) (*Conf, error) {
    18  	conf := Conf{}
    19  	err := json.Unmarshal([]byte(j), &conf)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	if b, err := govalidator.ValidateStruct(&conf); !b {
    24  		err = fmt.Errorf("mqtt 配置文件有误:%v", err)
    25  		return nil, err
    26  	}
    27  	return &conf, nil
    28  }