github.com/Axway/agent-sdk@v1.1.101/pkg/transaction/jmsprotocol.go (about)

     1  package transaction
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // JMSProtocol - Represents the details in a transaction event for the JMS protocol
    10  type JMSProtocol struct {
    11  	Type             string `json:"type,omitempty"`
    12  	AuthSubjectID    string `json:"authSubjectId,omitempty"`
    13  	JMSMessageID     string `json:"jmsMessageID,omitempty"`
    14  	JMSCorrelationID string `json:"jmsCorrelationID,omitempty"`
    15  	JMSDestination   string `json:"jmsDestination,omitempty"`
    16  	JMSProviderURL   string `json:"jmsProviderURL,omitempty"`
    17  	JMSDeliveryMode  int    `json:"jmsDeliveryMode,omitempty"`
    18  	JMSPriority      int    `json:"jmsPriority,omitempty"`
    19  	JMSReplyTo       string `json:"jmsReplyTo,omitempty"`
    20  	JMSRedelivered   int    `json:"jmsRedelivered,omitempty"`
    21  	JMSTimestamp     int    `json:"jmsTimestamp,omitempty"`
    22  	JMSExpiration    int    `json:"jmsExpiration,omitempty"`
    23  	JMSType          string `json:"jmsType,omitempty"`
    24  	JMSStatus        string `json:"jmsStatus,omitempty"`
    25  	JMSStatusText    string `json:"jmsStatusText,omitempty"`
    26  }
    27  
    28  // ToMapStringString - convert the JMSProtocol to a map[string]string
    29  func (j *JMSProtocol) ToMapStringString() (map[string]string, error) {
    30  	jmsPropertyStringMap := make(map[string]string)
    31  
    32  	value := reflect.ValueOf(j).Elem()
    33  	for i := 0; i < value.NumField(); i++ {
    34  		field := value.Field(i)
    35  		jsonField := value.Type().Field(i).Tag.Get("json")
    36  		jsonKey := strings.Split(jsonField, ",")[0]
    37  
    38  		switch field.Kind() {
    39  		case reflect.String:
    40  			jmsPropertyStringMap[jsonKey] = field.String()
    41  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    42  			jmsPropertyStringMap[jsonKey] = strconv.FormatInt(field.Int(), 10)
    43  		}
    44  	}
    45  	return jmsPropertyStringMap, nil
    46  }
    47  
    48  // FromMapStringString - convert the map[string]string to a JMSProtocol
    49  func (j *JMSProtocol) FromMapStringString(propertyMap map[string]string) error {
    50  	ignoreFields := map[string]bool{
    51  		"Type":         true,
    52  		"JMSStatus":    true,
    53  		"JMSTimestamp": true,
    54  	}
    55  
    56  	value := reflect.ValueOf(j).Elem()
    57  	for i := 0; i < value.NumField(); i++ {
    58  		field := value.Field(i)
    59  		fieldName := value.Type().Field(i).Name
    60  		jsonField := value.Type().Field(i).Tag.Get("json")
    61  
    62  		jsonKey := strings.Split(jsonField, ",")[0]
    63  		propertyMapValue, found := propertyMap[jsonKey]
    64  
    65  		if _, ignore := ignoreFields[fieldName]; ignore {
    66  			// Skip and ignore fields
    67  			continue
    68  		}
    69  
    70  		switch field.Kind() {
    71  		case reflect.String:
    72  			// update the fields value
    73  			value.FieldByName(fieldName).SetString("")
    74  			if found {
    75  				value.FieldByName(fieldName).SetString(propertyMapValue)
    76  			}
    77  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    78  			// Default the value to 0, set to new value if int
    79  			value.FieldByName(fieldName).SetInt(0)
    80  			if intValue, err := strconv.ParseInt(propertyMapValue, 10, 64); found && err == nil {
    81  				value.FieldByName(fieldName).SetInt(intValue)
    82  			}
    83  		}
    84  	}
    85  	return nil
    86  }