github.com/polarismesh/polaris@v1.17.8/common/model/cl5.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package model
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"strconv"
    24  	"strings"
    25  )
    26  
    27  // Cl5ServerCluster cl5集群的ctx的key
    28  type Cl5ServerCluster struct{}
    29  
    30  // Cl5ServerProtocol cl5.server的协议ctx
    31  type Cl5ServerProtocol struct{}
    32  
    33  // MarshalSid Sid结构体,序列化转为sid字符串
    34  func MarshalSid(sid *Sid) string {
    35  	return fmt.Sprintf("%d:%d", sid.ModID, sid.CmdID)
    36  }
    37  
    38  // MarshalModCmd mod cmd转为sid
    39  func MarshalModCmd(modID uint32, cmdID uint32) string {
    40  	return fmt.Sprintf("%d:%d", modID, cmdID)
    41  }
    42  
    43  // UnmarshalSid 把sid字符串反序列化为结构体Sid
    44  func UnmarshalSid(sidStr string) (*Sid, error) {
    45  	items := strings.Split(sidStr, ":")
    46  	if len(items) != 2 {
    47  		return nil, errors.New("invalid format sid string")
    48  	}
    49  
    50  	modID, err := strconv.ParseUint(items[0], 10, 32)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	var cmdID uint64
    56  	cmdID, err = strconv.ParseUint(items[1], 10, 32)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	out := &Sid{
    62  		ModID: uint32(modID),
    63  		CmdID: uint32(cmdID),
    64  	}
    65  	return out, nil
    66  }