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/16 15:09:29 UTC

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

ShannonDing commented on a change in pull request #210: fix unmarhsal messageID bug
URL: https://github.com/apache/rocketmq-client-go/pull/210#discussion_r324730177
 
 

 ##########
 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:
   IMO, we can remove the func string2Bytes, and be instead of:
   ipBytes := make([]byte, hex.DecodedLen(len(id[0:8])))
   _,  _ = hex.Decode(ipBytes, id[0:8])
   How do you think about it?

----------------------------------------------------------------
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