You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2019/09/17 02:14:45 UTC

[GitHub] [rocketmq-client-go] leyafo commented on a change in pull request #210: [ISSUE #211]fix unmarhsal messageID bug

leyafo commented on a change in pull request #210: [ISSUE #211]fix unmarhsal messageID bug
URL: https://github.com/apache/rocketmq-client-go/pull/210#discussion_r324955460
 
 

 ##########
 File path: primitive/message.go
 ##########
 @@ -394,38 +392,40 @@ func createMessageId(addr []byte, port int32, offset int64) string {
 	return strings.ToUpper(hex.EncodeToString(buffer.Bytes()))
 }
 
-func UnmarshalMsgID(msgID []byte) (*MessageID, error) {
-	if len(msgID) < 32 {
-		return nil, errors.Errorf("%s len < 32", string(msgID))
+func string2Bytes(hexBytes []byte) []byte {
+	if len(hexBytes) == 0 {
+		return nil
 	}
-	ip := make([]byte, 8)
-	port := make([]byte, 8)
-	offset := make([]byte, 16)
-	var portVal int
-	var offsetVal int64
 
-	_, err := hex.Decode(ip, msgID[0:8])
-	if err != nil {
-		_, err = hex.Decode(port, msgID[8:16])
-	}
-	if err != nil {
-		_, err = hex.Decode(offset, msgID[16:32])
-	}
-	if err != nil {
-		portVal, err = strconv.Atoi(string(port))
-	}
-	if err != nil {
-		offsetVal, err = strconv.ParseInt(string(offset), 10, 0)
+	hexBytes = bytes.ToUpper(hexBytes)
+	length := len(hexBytes) / 2
+	result := make([]byte, length)
+	for i := 0; i < length; i++ {
+		pos := i * 2
+		result[i] = charToByte(hexBytes[pos])<<4 | charToByte(hexBytes[pos+1])
 	}
+	return result
+}
 
-	if err != nil {
-		return nil, err
+func charToByte(c byte) byte {
+	return byte(strings.IndexByte("0123456789ABCDEF", c))
+}
+
+func UnmarshalMsgID(msgID []byte) (*MessageID, error) {
+	if len(msgID) < 32 {
+		return nil, fmt.Errorf("%s len < 32", string(msgID))
 	}
+	ipBytes := string2Bytes(msgID[0:8])
 
 Review comment:
   Yes, I think your're right. We don't just copy code from JAVA.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services