Skip to content

Commit fc468e9

Browse files
committed
Convert string properties to other types - #39
1 parent 0e09bbe commit fc468e9

File tree

2 files changed

+239
-0
lines changed

2 files changed

+239
-0
lines changed

messageproperties_test.go

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,212 @@ func TestPropertyBytesMsg(t *testing.T) {
10881088
assert.Equal(t, 5, len(allPropNames))
10891089

10901090
}
1091+
1092+
/*
1093+
* Test the conversion between different message property data types.
1094+
*/
1095+
func TestPropertyTypesStringConversion(t *testing.T) {
1096+
1097+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
1098+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
1099+
assert.Nil(t, cfErr)
1100+
1101+
// Creates a connection to the queue manager, using defer to close it automatically
1102+
// at the end of the function (if it was created successfully)
1103+
context, ctxErr := cf.CreateContext()
1104+
assert.Nil(t, ctxErr)
1105+
if context != nil {
1106+
defer context.Close()
1107+
}
1108+
1109+
msg := context.CreateTextMessage()
1110+
1111+
unsetPropName := "thisPropertyIsNotSet"
1112+
1113+
// Set up some different string properties
1114+
stringOfStringPropName := "stringOfString"
1115+
stringOfStringValue := "myValue"
1116+
stringOfEmptyStrPropName := "stringOfEmptyStr"
1117+
stringOfEmptyValue := ""
1118+
1119+
stringOfIntPropName := "stringOfInt"
1120+
stringOfIntValue := "245"
1121+
stringOfIntPropName2 := "stringOfInt2"
1122+
stringOfIntValue2 := "-34678"
1123+
1124+
stringOfBoolPropName := "stringOfBool"
1125+
stringOfBoolValue := "true"
1126+
stringOfBoolPropName2 := "stringOfBool2"
1127+
stringOfBoolValue2 := "false"
1128+
1129+
stringOfDoublePropName := "stringOfDouble"
1130+
stringOfDoubleValue := "2.718527453"
1131+
stringOfDoublePropName2 := "stringOfDouble2"
1132+
stringOfDoubleValue2 := "-25675752.212345678"
1133+
1134+
msg.SetStringProperty(stringOfStringPropName, &stringOfStringValue)
1135+
msg.SetStringProperty(stringOfEmptyStrPropName, &stringOfEmptyValue)
1136+
msg.SetStringProperty(stringOfIntPropName, &stringOfIntValue)
1137+
msg.SetStringProperty(stringOfIntPropName2, &stringOfIntValue2)
1138+
msg.SetStringProperty(stringOfBoolPropName, &stringOfBoolValue)
1139+
msg.SetStringProperty(stringOfBoolPropName2, &stringOfBoolValue2)
1140+
msg.SetStringProperty(stringOfDoublePropName, &stringOfDoubleValue)
1141+
msg.SetStringProperty(stringOfDoublePropName2, &stringOfDoubleValue2)
1142+
1143+
// Now an int property
1144+
intPropName := "myIntProperty"
1145+
intPropValue := 553786
1146+
retErr := msg.SetIntProperty(intPropName, intPropValue)
1147+
assert.Nil(t, retErr)
1148+
1149+
// Now a double property
1150+
doublePropName := "myDoubleProperty"
1151+
doublePropValue := float64(3.1415926535)
1152+
retErr = msg.SetDoubleProperty(doublePropName, doublePropValue)
1153+
assert.Nil(t, retErr)
1154+
1155+
// Now a bool property
1156+
boolPropName := "myBoolProperty"
1157+
boolPropValue := true
1158+
retErr = msg.SetBooleanProperty(boolPropName, boolPropValue)
1159+
assert.Nil(t, retErr)
1160+
1161+
// Set up objects for send/receive
1162+
queue := context.CreateQueue("DEV.QUEUE.1")
1163+
consumer, errCons := context.CreateConsumer(queue)
1164+
if consumer != nil {
1165+
defer consumer.Close()
1166+
}
1167+
assert.Nil(t, errCons)
1168+
1169+
// Now send the message and get it back again, to check that it roundtripped.
1170+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, msg)
1171+
assert.Nil(t, errSend)
1172+
1173+
rcvMsg, errRvc := consumer.ReceiveNoWait()
1174+
assert.Nil(t, errRvc)
1175+
assert.NotNil(t, rcvMsg)
1176+
1177+
// Check string properties were set correctly
1178+
gotStringPropValue, gotStringErr := rcvMsg.GetStringProperty(stringOfStringPropName)
1179+
gotEmptyStrPropValue, gotEmptyStrErr := rcvMsg.GetStringProperty(stringOfEmptyStrPropName)
1180+
gotIntPropValue, gotIntErr := rcvMsg.GetStringProperty(stringOfIntPropName)
1181+
gotIntPropValue2, gotIntErr2 := rcvMsg.GetStringProperty(stringOfIntPropName2)
1182+
gotBoolPropValue, gotBoolErr := rcvMsg.GetStringProperty(stringOfBoolPropName)
1183+
gotBoolPropValue2, gotBoolErr2 := rcvMsg.GetStringProperty(stringOfBoolPropName2)
1184+
gotDoublePropValue, gotDoubleErr := rcvMsg.GetStringProperty(stringOfDoublePropName)
1185+
gotDoublePropValue2, gotDoubleErr2 := rcvMsg.GetStringProperty(stringOfDoublePropName2)
1186+
gotUnsetPropValue, gotUnsetErr := rcvMsg.GetStringProperty(unsetPropName)
1187+
assert.Nil(t, gotStringErr)
1188+
assert.Nil(t, gotEmptyStrErr)
1189+
assert.Nil(t, gotIntErr)
1190+
assert.Nil(t, gotIntErr2)
1191+
assert.Nil(t, gotBoolErr)
1192+
assert.Nil(t, gotBoolErr2)
1193+
assert.Nil(t, gotDoubleErr)
1194+
assert.Nil(t, gotDoubleErr2)
1195+
assert.Nil(t, gotUnsetErr)
1196+
assert.Equal(t, stringOfStringValue, *gotStringPropValue)
1197+
assert.Equal(t, stringOfEmptyValue, *gotEmptyStrPropValue)
1198+
assert.Equal(t, stringOfIntValue, *gotIntPropValue)
1199+
assert.Equal(t, stringOfIntValue2, *gotIntPropValue2)
1200+
assert.Equal(t, stringOfBoolValue, *gotBoolPropValue)
1201+
assert.Equal(t, stringOfBoolValue2, *gotBoolPropValue2)
1202+
assert.Equal(t, stringOfDoubleValue, *gotDoublePropValue)
1203+
assert.Equal(t, stringOfDoubleValue2, *gotDoublePropValue2)
1204+
assert.Nil(t, gotUnsetPropValue)
1205+
1206+
// Get the string properties back as int.
1207+
gotStrAsIntValue, gotStringErr := rcvMsg.GetIntProperty(stringOfStringPropName)
1208+
gotEmptyStrAsIntValue, gotEmptyStrErr := rcvMsg.GetIntProperty(stringOfEmptyStrPropName)
1209+
gotStrIntAsIntValue, gotIntErr := rcvMsg.GetIntProperty(stringOfIntPropName)
1210+
gotStrIntAsIntValue2, gotIntErr2 := rcvMsg.GetIntProperty(stringOfIntPropName2)
1211+
gotStrBoolAsIntValue, gotBoolErr := rcvMsg.GetIntProperty(stringOfBoolPropName)
1212+
gotStrBoolAsIntValue2, gotBoolErr2 := rcvMsg.GetIntProperty(stringOfBoolPropName2)
1213+
gotStrDoubleAsIntValue, gotDoubleErr := rcvMsg.GetIntProperty(stringOfDoublePropName)
1214+
gotStrDoubleAsIntValue2, gotDoubleErr2 := rcvMsg.GetIntProperty(stringOfDoublePropName2)
1215+
gotUnsetAsIntValue, gotUnsetErr := rcvMsg.GetIntProperty(unsetPropName)
1216+
assert.NotNil(t, gotStringErr)
1217+
assert.Equal(t, "1055", gotStringErr.GetErrorCode())
1218+
assert.Equal(t, "MQJMS_E_BAD_TYPE", gotStringErr.GetReason())
1219+
assert.NotNil(t, gotEmptyStrErr)
1220+
assert.Nil(t, gotIntErr)
1221+
assert.Nil(t, gotIntErr2)
1222+
assert.NotNil(t, gotBoolErr)
1223+
assert.NotNil(t, gotBoolErr2)
1224+
assert.NotNil(t, gotDoubleErr)
1225+
assert.NotNil(t, gotDoubleErr2)
1226+
assert.Nil(t, gotUnsetErr)
1227+
assert.Equal(t, 0, gotStrAsIntValue) // non-nil err
1228+
assert.Equal(t, 0, gotEmptyStrAsIntValue) // non-nil err
1229+
assert.Equal(t, 245, gotStrIntAsIntValue)
1230+
assert.Equal(t, -34678, gotStrIntAsIntValue2)
1231+
assert.Equal(t, 0, gotStrBoolAsIntValue) // non-nil err
1232+
assert.Equal(t, 0, gotStrBoolAsIntValue2) // non-nil err
1233+
assert.Equal(t, 0, gotStrDoubleAsIntValue) // non-nil err
1234+
assert.Equal(t, 0, gotStrDoubleAsIntValue2) // non-nil err
1235+
assert.Equal(t, 0, gotUnsetAsIntValue)
1236+
1237+
// Get the string properties back as bool.
1238+
gotStrAsBoolValue, gotStringErr := rcvMsg.GetBooleanProperty(stringOfStringPropName)
1239+
gotEmptyStrAsBoolValue, gotEmptyStrErr := rcvMsg.GetBooleanProperty(stringOfEmptyStrPropName)
1240+
gotStrIntAsBoolValue, gotIntErr := rcvMsg.GetBooleanProperty(stringOfIntPropName)
1241+
gotStrIntAsBoolValue2, gotIntErr2 := rcvMsg.GetBooleanProperty(stringOfIntPropName2)
1242+
gotStrBoolAsBoolValue, gotBoolErr := rcvMsg.GetBooleanProperty(stringOfBoolPropName)
1243+
gotStrBoolAsBoolValue2, gotBoolErr2 := rcvMsg.GetBooleanProperty(stringOfBoolPropName2)
1244+
gotStrDoubleAsBoolValue, gotDoubleErr := rcvMsg.GetBooleanProperty(stringOfDoublePropName)
1245+
gotStrDoubleAsBoolValue2, gotDoubleErr2 := rcvMsg.GetBooleanProperty(stringOfDoublePropName2)
1246+
gotUnsetAsBoolValue, gotUnsetErr := rcvMsg.GetBooleanProperty(unsetPropName)
1247+
assert.NotNil(t, gotStringErr)
1248+
assert.Equal(t, "1055", gotStringErr.GetErrorCode())
1249+
assert.Equal(t, "MQJMS_E_BAD_TYPE", gotStringErr.GetReason())
1250+
assert.NotNil(t, gotEmptyStrErr)
1251+
assert.NotNil(t, gotIntErr)
1252+
assert.NotNil(t, gotIntErr2)
1253+
assert.Nil(t, gotBoolErr)
1254+
assert.Nil(t, gotBoolErr2)
1255+
assert.NotNil(t, gotDoubleErr)
1256+
assert.NotNil(t, gotDoubleErr2)
1257+
assert.Nil(t, gotUnsetErr)
1258+
assert.Equal(t, false, gotStrAsBoolValue) // non-nil err
1259+
assert.Equal(t, false, gotEmptyStrAsBoolValue) // non-nil err
1260+
assert.Equal(t, false, gotStrIntAsBoolValue) // non-nil err
1261+
assert.Equal(t, false, gotStrIntAsBoolValue2) // non-nil err
1262+
assert.Equal(t, true, gotStrBoolAsBoolValue)
1263+
assert.Equal(t, false, gotStrBoolAsBoolValue2)
1264+
assert.Equal(t, false, gotStrDoubleAsBoolValue) // non-nil err
1265+
assert.Equal(t, false, gotStrDoubleAsBoolValue2) // non-nil err
1266+
assert.Equal(t, false, gotUnsetAsBoolValue)
1267+
1268+
// Get the string properties back as double.
1269+
gotStrAsDoubleValue, gotStringErr := rcvMsg.GetDoubleProperty(stringOfStringPropName)
1270+
gotEmptyStrAsDoubleValue, gotEmptyStrErr := rcvMsg.GetDoubleProperty(stringOfEmptyStrPropName)
1271+
gotStrIntAsDoubleValue, gotIntErr := rcvMsg.GetDoubleProperty(stringOfIntPropName)
1272+
gotStrIntAsDoubleValue2, gotIntErr2 := rcvMsg.GetDoubleProperty(stringOfIntPropName2)
1273+
gotStrBoolAsDoubleValue, gotBoolErr := rcvMsg.GetDoubleProperty(stringOfBoolPropName)
1274+
gotStrBoolAsDoubleValue2, gotBoolErr2 := rcvMsg.GetDoubleProperty(stringOfBoolPropName2)
1275+
gotStrDoubleAsDoubleValue, gotDoubleErr := rcvMsg.GetDoubleProperty(stringOfDoublePropName)
1276+
gotStrDoubleAsDoubleValue2, gotDoubleErr2 := rcvMsg.GetDoubleProperty(stringOfDoublePropName2)
1277+
gotUnsetAsDoubleValue, gotUnsetErr := rcvMsg.GetDoubleProperty(unsetPropName)
1278+
assert.NotNil(t, gotStringErr)
1279+
assert.Equal(t, "1055", gotStringErr.GetErrorCode())
1280+
assert.Equal(t, "MQJMS_E_BAD_TYPE", gotStringErr.GetReason())
1281+
assert.NotNil(t, gotEmptyStrErr)
1282+
assert.Nil(t, gotIntErr)
1283+
assert.Nil(t, gotIntErr2)
1284+
assert.NotNil(t, gotBoolErr)
1285+
assert.NotNil(t, gotBoolErr2)
1286+
assert.Nil(t, gotDoubleErr)
1287+
assert.Nil(t, gotDoubleErr2)
1288+
assert.Nil(t, gotUnsetErr)
1289+
assert.Equal(t, float64(0), gotStrAsDoubleValue) // non-nil err
1290+
assert.Equal(t, float64(0), gotEmptyStrAsDoubleValue) // non-nil err
1291+
assert.Equal(t, float64(245), gotStrIntAsDoubleValue)
1292+
assert.Equal(t, float64(-34678), gotStrIntAsDoubleValue2)
1293+
assert.Equal(t, float64(0), gotStrBoolAsDoubleValue) // non-nil err
1294+
assert.Equal(t, float64(0), gotStrBoolAsDoubleValue2) // non-nil err
1295+
assert.Equal(t, float64(2.718527453), gotStrDoubleAsDoubleValue)
1296+
assert.Equal(t, float64(-25675752.212345678), gotStrDoubleAsDoubleValue2)
1297+
assert.Equal(t, float64(0), gotUnsetAsDoubleValue)
1298+
1299+
}

mqjms/MessageImpl.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
ibmmq "github.com/ibm-messaging/mq-golang/v5/ibmmq"
2222
)
2323

24+
const MessageImpl_PROPERTY_CONVERT_FAILED_REASON string = "MQJMS_E_BAD_TYPE"
25+
const MessageImpl_PROPERTY_CONVERT_FAILED_CODE string = "1055"
26+
2427
// MessageImpl contains the IBM MQ specific attributes that are
2528
// common to all types of message.
2629
type MessageImpl struct {
@@ -380,9 +383,18 @@ func (msg *MessageImpl) GetIntProperty(name string) (int, jms20subset.JMSExcepti
380383
_, value, err := msg.msgHandle.InqMP(impo, pd, name)
381384

382385
if err == nil {
386+
387+
var parseErr error
388+
383389
switch valueTyped := value.(type) {
384390
case int64:
385391
valueRet = int(valueTyped)
392+
case string:
393+
valueRet, parseErr = strconv.Atoi(valueTyped)
394+
if parseErr != nil {
395+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
396+
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
397+
}
386398
default:
387399
// TODO - other conversions
388400
//fmt.Println("Other type", value, reflect.TypeOf(value))
@@ -439,9 +451,18 @@ func (msg *MessageImpl) GetDoubleProperty(name string) (float64, jms20subset.JMS
439451
_, value, err := msg.msgHandle.InqMP(impo, pd, name)
440452

441453
if err == nil {
454+
455+
var parseErr error
456+
442457
switch valueTyped := value.(type) {
443458
case float64:
444459
valueRet = valueTyped
460+
case string:
461+
valueRet, parseErr = strconv.ParseFloat(valueTyped, 64)
462+
if parseErr != nil {
463+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
464+
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
465+
}
445466
default:
446467
// TODO - other conversions
447468
//fmt.Println("Other type", value, reflect.TypeOf(value))
@@ -498,9 +519,18 @@ func (msg *MessageImpl) GetBooleanProperty(name string) (bool, jms20subset.JMSEx
498519
_, value, err := msg.msgHandle.InqMP(impo, pd, name)
499520

500521
if err == nil {
522+
523+
var parseErr error
524+
501525
switch valueTyped := value.(type) {
502526
case bool:
503527
valueRet = valueTyped
528+
case string:
529+
valueRet, parseErr = strconv.ParseBool(valueTyped)
530+
if parseErr != nil {
531+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
532+
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
533+
}
504534
default:
505535
// TODO - other conversions
506536
//fmt.Println("Other type", value, reflect.TypeOf(value))

0 commit comments

Comments
 (0)