dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/address.go (about)

     1  //
     2  // Copyright (C) 2021 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package dtos
     7  
     8  import (
     9  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    10  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    11  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    12  )
    13  
    14  type Address struct {
    15  	Type string `json:"type" validate:"oneof='REST' 'MQTT' 'EMAIL'"`
    16  
    17  	Host string `json:"host" validate:"required_unless=Type EMAIL"`
    18  	Port int    `json:"port" validate:"required_unless=Type EMAIL"`
    19  
    20  	RESTAddress    `json:",inline" validate:"-"`
    21  	MQTTPubAddress `json:",inline" validate:"-"`
    22  	EmailAddress   `json:",inline" validate:"-"`
    23  }
    24  
    25  // Validate satisfies the Validator interface
    26  func (a *Address) Validate() error {
    27  	err := common.Validate(a)
    28  	if err != nil {
    29  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "invalid Address.", err)
    30  	}
    31  	switch a.Type {
    32  	case common.REST:
    33  		err = common.Validate(a.RESTAddress)
    34  		if err != nil {
    35  			return errors.NewCommonEdgeX(errors.KindContractInvalid, "invalid RESTAddress.", err)
    36  		}
    37  	case common.MQTT:
    38  		err = common.Validate(a.MQTTPubAddress)
    39  		if err != nil {
    40  			return errors.NewCommonEdgeX(errors.KindContractInvalid, "invalid MQTTPubAddress.", err)
    41  		}
    42  	case common.EMAIL:
    43  		err = common.Validate(a.EmailAddress)
    44  		if err != nil {
    45  			return errors.NewCommonEdgeX(errors.KindContractInvalid, "invalid EmailAddress.", err)
    46  		}
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  type RESTAddress struct {
    53  	Path       string `json:"path,omitempty"`
    54  	HTTPMethod string `json:"httpMethod,omitempty" validate:"required,oneof='GET' 'HEAD' 'POST' 'PUT' 'PATCH' 'DELETE' 'TRACE' 'CONNECT'"`
    55  }
    56  
    57  func NewRESTAddress(host string, port int, httpMethod string) Address {
    58  	return Address{
    59  		Type: common.REST,
    60  		Host: host,
    61  		Port: port,
    62  		RESTAddress: RESTAddress{
    63  			HTTPMethod: httpMethod,
    64  		},
    65  	}
    66  }
    67  
    68  type MQTTPubAddress struct {
    69  	Publisher      string `json:"publisher,omitempty" validate:"required"`
    70  	Topic          string `json:"topic,omitempty" validate:"required"`
    71  	QoS            int    `json:"qos,omitempty"`
    72  	KeepAlive      int    `json:"keepAlive,omitempty"`
    73  	Retained       bool   `json:"retained,omitempty"`
    74  	AutoReconnect  bool   `json:"autoReconnect,omitempty"`
    75  	ConnectTimeout int    `json:"connectTimeout,omitempty"`
    76  }
    77  
    78  func NewMQTTAddress(host string, port int, publisher string, topic string) Address {
    79  	return Address{
    80  		Type: common.MQTT,
    81  		Host: host,
    82  		Port: port,
    83  		MQTTPubAddress: MQTTPubAddress{
    84  			Publisher: publisher,
    85  			Topic:     topic,
    86  		},
    87  	}
    88  }
    89  
    90  type EmailAddress struct {
    91  	Recipients []string `json:"recipients,omitempty" validate:"gt=0,dive,email"`
    92  }
    93  
    94  func NewEmailAddress(recipients []string) Address {
    95  	return Address{
    96  		Type: common.EMAIL,
    97  		EmailAddress: EmailAddress{
    98  			Recipients: recipients,
    99  		},
   100  	}
   101  }
   102  
   103  func ToAddressModel(a Address) models.Address {
   104  	var address models.Address
   105  
   106  	switch a.Type {
   107  	case common.REST:
   108  		address = models.RESTAddress{
   109  			BaseAddress: models.BaseAddress{
   110  				Type: a.Type, Host: a.Host, Port: a.Port,
   111  			},
   112  			Path:       a.RESTAddress.Path,
   113  			HTTPMethod: a.RESTAddress.HTTPMethod,
   114  		}
   115  	case common.MQTT:
   116  		address = models.MQTTPubAddress{
   117  			BaseAddress: models.BaseAddress{
   118  				Type: a.Type, Host: a.Host, Port: a.Port,
   119  			},
   120  			Publisher:      a.MQTTPubAddress.Publisher,
   121  			Topic:          a.MQTTPubAddress.Topic,
   122  			QoS:            a.QoS,
   123  			KeepAlive:      a.KeepAlive,
   124  			Retained:       a.Retained,
   125  			AutoReconnect:  a.AutoReconnect,
   126  			ConnectTimeout: a.ConnectTimeout,
   127  		}
   128  	case common.EMAIL:
   129  		address = models.EmailAddress{
   130  			BaseAddress: models.BaseAddress{
   131  				Type: a.Type,
   132  			},
   133  			Recipients: a.EmailAddress.Recipients,
   134  		}
   135  	}
   136  	return address
   137  }
   138  
   139  func FromAddressModelToDTO(address models.Address) Address {
   140  	dto := Address{
   141  		Type: address.GetBaseAddress().Type,
   142  		Host: address.GetBaseAddress().Host,
   143  		Port: address.GetBaseAddress().Port,
   144  	}
   145  
   146  	switch a := address.(type) {
   147  	case models.RESTAddress:
   148  		dto.RESTAddress = RESTAddress{
   149  			Path:       a.Path,
   150  			HTTPMethod: a.HTTPMethod,
   151  		}
   152  	case models.MQTTPubAddress:
   153  		dto.MQTTPubAddress = MQTTPubAddress{
   154  			Publisher:      a.Publisher,
   155  			Topic:          a.Topic,
   156  			QoS:            a.QoS,
   157  			KeepAlive:      a.KeepAlive,
   158  			Retained:       a.Retained,
   159  			AutoReconnect:  a.AutoReconnect,
   160  			ConnectTimeout: a.ConnectTimeout,
   161  		}
   162  	case models.EmailAddress:
   163  		dto.EmailAddress = EmailAddress{
   164  			Recipients: a.Recipients,
   165  		}
   166  	}
   167  	return dto
   168  }
   169  
   170  func ToAddressModels(dtos []Address) []models.Address {
   171  	models := make([]models.Address, len(dtos))
   172  	for i, c := range dtos {
   173  		models[i] = ToAddressModel(c)
   174  	}
   175  	return models
   176  }
   177  
   178  func FromAddressModelsToDTOs(models []models.Address) []Address {
   179  	dtos := make([]Address, len(models))
   180  	for i, c := range models {
   181  		dtos[i] = FromAddressModelToDTO(c)
   182  	}
   183  	return dtos
   184  }