github.com/vmware/transport-go@v1.3.4/model/message_factory.go (about)

     1  // Copyright 2019-2020 VMware, Inc.
     2  // SPDX-License-Identifier: BSD-2-Clause
     3  
     4  package model
     5  
     6  import "github.com/google/uuid"
     7  
     8  type MessageConfig struct {
     9  	Id            *uuid.UUID
    10  	DestinationId *uuid.UUID
    11  	Destination   string
    12  	Channel       string
    13  	Payload       interface{}
    14  	Headers       []MessageHeader
    15  	Direction     Direction
    16  	Err           error
    17  }
    18  
    19  func checkId(msgConfig *MessageConfig) {
    20  	if msgConfig.Id == nil {
    21  		id := uuid.New()
    22  		msgConfig.Id = &id
    23  	}
    24  }
    25  
    26  func GenerateRequest(msgConfig *MessageConfig) *Message {
    27  	checkId(msgConfig)
    28  	return &Message{
    29  		Headers:       msgConfig.Headers,
    30  		Id:            msgConfig.Id,
    31  		Channel:       msgConfig.Channel,
    32  		DestinationId: msgConfig.DestinationId,
    33  		Destination:   msgConfig.Destination,
    34  		Payload:       msgConfig.Payload,
    35  		Direction:     RequestDir}
    36  }
    37  
    38  func GenerateResponse(msgConfig *MessageConfig) *Message {
    39  	checkId(msgConfig)
    40  	return &Message{
    41  		Headers:       msgConfig.Headers,
    42  		Id:            msgConfig.Id,
    43  		Channel:       msgConfig.Channel,
    44  		DestinationId: msgConfig.DestinationId,
    45  		Destination:   msgConfig.Destination,
    46  		Payload:       msgConfig.Payload,
    47  		Direction:     ResponseDir}
    48  }
    49  
    50  func GenerateError(msgConfig *MessageConfig) *Message {
    51  	checkId(msgConfig)
    52  	return &Message{
    53  		Id:            msgConfig.Id,
    54  		Channel:       msgConfig.Channel,
    55  		DestinationId: msgConfig.DestinationId,
    56  		Destination:   msgConfig.Destination,
    57  		Error:         msgConfig.Err,
    58  		Direction:     ErrorDir}
    59  }