123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package request
- import (
- "encoding/json"
- "errors"
- "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client"
- "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client/router"
- "git.sxidc.com/go-tools/api_binding/mqtt_binding/response"
- "git.sxidc.com/service-supports/fslog"
- "github.com/go-playground/validator/v10"
- )
- type CallerIdentifierRequest struct {
- CallerIdentifier string `json:"callerIdentifier" validate:"required"`
- }
- func (req *CallerIdentifierRequest) Identifier() string {
- return req.CallerIdentifier
- }
- func (req *CallerIdentifierRequest) Copy() response.Identifier {
- return &CallerIdentifierRequest{}
- }
- var validate = validator.New(validator.WithRequiredStructEnabled())
- func BindingJson[O any](c *mqtt_client.MqttClient, item *router.Item, request any,
- responseIdentifier response.Identifier, sendFunc response.SendFunc[O]) bool {
- data := item.GetData()
- if data != nil && len(data) != 0 {
- if responseIdentifier != nil {
- err := json.Unmarshal(data, responseIdentifier)
- if err != nil {
- err := errors.New("Topic: " + item.Topic + " Response Identifier Unmarshal Error: " + err.Error())
- fslog.Error(err)
- return false
- }
- err = validate.Struct(responseIdentifier)
- if err != nil {
- err := errors.New("Topic: " + item.Topic + " Response Identifier Validate Error: " + err.Error())
- fslog.Error(err)
- return false
- }
- }
- if request != nil {
- err := json.Unmarshal(data, request)
- if err != nil {
- fslog.Error(err)
- var zero O
- sendFunc(c, item, responseIdentifier, zero, err)
- return false
- }
- }
- }
- if request != nil {
- err := validate.Struct(request)
- if err != nil {
- fslog.Error(err)
- var zero O
- sendFunc(c, item, responseIdentifier, zero, err)
- return false
- }
- }
- return true
- }
|