• 函数处理结果返回给调用函数,实现代码模块化,便于单元测试


    func wsRouter(clientID string, req ClientRequest) {
    
    	switch req.Method {
    	//--------------------------------------------------dy
    	case "/createGroup", "/addGroupMember", "/addGroupMemberList", "/deleteGroupMember":
    		dyRouter(req.Method, clientID, req.Args)
    		//--------------------------------------------------Ficow Shen
    	case "/chatMessageList":
    		chatMessageList(clientID, req.Args)
    	case "/sessionList":
    		sessionList(clientID, req.Args)
    	case "/sendTextMessage":
    		sendTextMessage(clientID, req.Args)
    
    		//--------------------------------------------------Pismery
    	case "/getObjectData":
    		getObjectData(clientID, req.Args)
    	case "/deleteGroup":
    		deleteGroup(clientID, req.Args)
    	case "/alterObjectData":
    		alterObjectData(clientID, req.Args)
    	case "/searchFriend":
    		searchFriend(clientID, req.Args)
    	case "/groupManagerList":
    		groupManagerList(clientID, req.Args)
    		//--------------------------------------------------Decaf
    	case "/getFriendList":
    		getFriendList(clientID, req.Args)
    	case "/getGroupList":
    		getGroupList(clientID, req.Args)
    	case "/getGroupMember":
    		getGroupMember(clientID, req.Args)
    	case "/setGroupManager":
    		setGroupManager(clientID, req.Args)
    	case "/searchGroup":
    		searchGroup(clientID, req.Args)
    	default:
    		dispatchSystemMsgToClient(clientID, req.Method, errBadAPI, mBadAPI)
    		// case "/链接":
    		// 	yourFunc(req.Args)
    		// }
    	}
    }
    

      

    package Network
    
    import (
    	"logs"
    )
    
    func dyRouter(URL string, userID string, req interface{}) {
    	var (
    		code   int         = Success
    		msg    string      = ""
    		result interface{} = nil
    	)
    	defer func() {
    		writeResponseToClient(userID, ServerResponse{
    			Method:  URL,
    			Code:    code,
    			Message: msg,
    			Result:  result,
    		})
    		if code != Success {
    			logs.Print("code:", code, "	msg:", msg, "	result", result)
    		}
    	}()
    	dataMap, ok := req.(map[string]interface{})
    	if !ok {
    		code = errAssertType
    		msg = "type isn't map[string]interface{}"
    		return
    	}
    	var data dy_judge = dataMap
    	switch URL {
    	case "/createGroup":
    		code, msg, result = createGroup(userID, data)
    		break
    	case "/addGroupMember":
    		code, msg, result = addGroupMember(userID, data)
    		break
    	case "/addGroupMemberList":
    		code, msg, result = addGroupMemberList(userID, data)
    		break
    	case "/deleteGroupMember":
    		code, msg, result = deleteGroupMember(userID, data)
    		break
    	}
    }
    

      

     

    package Network
    
    import (
    	"SQL/FSDB"
    
    	"logs"
    
    	_ "github.com/lib/pq"
    )
    
    type SessionInfo struct {
    	FriendSessions interface{} `json:"FriendSessions"`
    	GroupSessions  interface{} `json:"GroupSessions"`
    }
    
    func sessionList(clientID string, args interface{}) {
    
    	var errCode = Success
    	var errMsg = ""
    	var result interface{}
    	defer func() {
    		err := writeResponseToClient(clientID, ServerResponse{
    			Method:  "/sessionList",
    			Code:    errCode,
    			Message: errMsg,
    			Result:  result,
    		})
    		if err != nil {
    			logs.Print("/sessionList writeResponseToClient,Err:", err.Error())
    			return
    		}
    	}()
    
    	tx, db, err := FSDB.BeginTx()
    	if err != nil {
    		errCode = errEndTx
    		errMsg = "FSDB.BeginTx 执行错误:" + err.Error()
    		return
    	}
    	defer func() {
    		defer closeDB(db)
    		if errCode == Success {
    			err := tx.Commit()
    			if err != nil {
    				errCode = errEndTx
    				errMsg = err.Error()
    				logs.Print("/sessionList tx.Commit err:", err.Error())
    			}
    			return
    		}
    		err := tx.Rollback()
    		if err != nil {
    			errCode = errEndTx
    			errMsg = err.Error()
    			logs.Print("*sessionList tx.Rollback err:", err.Error())
    		}
    	}()
    
    	var friendSessions interface{}
    	errCode, errMsg, friendSessions = friendSessionInContext(tx, clientID)
    	if errCode != Success {
    		return
    	}
    	var groupSessions interface{}
    	errCode, errMsg, groupSessions = groupSessionInContext(tx, clientID)
    	if errCode != Success {
    		return
    	}
    
    	sessionInfo := SessionInfo{
    		FriendSessions: friendSessions,
    		GroupSessions:  groupSessions,
    	}
    	result = sessionInfo
    
    	// // 更新用户在线时间
    	// updateUserOnlineTime(tx, clientID, currentTime())
    }
    

      

    上面是一个数据分流处理的函数,然后每个分流函数处理完后的数据都调用了writeResponseToClient,但这样在对某个分流函数进行分流测试时无法将数据返回到调用处,单元测试时就无法得到测试返回值,所以应统一将结果返回到函数调用处进行处理

    修改后:

    // websocket接口都在这里部署路由
    func wsRouter(clientID string, req ClientRequest) {
    	var res *ServerResponse
    	switch req.Method {
    	//--------------------------------------------------dy
    	case "/createGroup", "/addGroupMember", "/addGroupMemberList", "/deleteGroupMember":
    		res = dyRouter(req.Method, clientID, req.Args)
    		//--------------------------------------------------Ficow Shen
    	case "/chatMessageList":
    		res = chatMessageList(clientID, req.Args)
    	case "/sessionList":
    		res = sessionList(clientID, req.Args)
    	case "/sendTextMessage":
    		res = sendTextMessage(clientID, req.Args)
    
    		//--------------------------------------------------Pismery
    	case "/getObjectData":
    		res = getObjectData(clientID, req.Args)
    	case "/deleteGroup":
    		res = deleteGroup(clientID, req.Args)
    	case "/alterObjectData":
    		res = alterObjectData(clientID, req.Args)
    	case "/searchFriend":
    		res = searchFriend(clientID, req.Args)
    	case "/groupManagerList":
    		res = groupManagerList(clientID, req.Args)
    		//--------------------------------------------------Decaf
    	case "/getFriendList":
    		res = getFriendList(clientID, req.Args)
    	case "/getGroupList":
    		res = getGroupList(clientID, req.Args)
    	case "/getGroupMember":
    		res = getGroupMember(clientID, req.Args)
    	case "/setGroupManager":
    		res = setGroupManager(clientID, req.Args)
    	case "/searchGroup":
    		res = searchGroup(clientID, req.Args)
    	default:
    		res = &ServerResponse{
    			Method:  req.Method,
    			Code:    errBadAPI,
    			Message: mBadAPI,
    		}
    		// case "/链接":
    		// 	yourFunc(req.Args)
    		// }
    	}
    	if res.Code == errBadAPI {
    		dispatchSystemMsgToClient(clientID, req.Method, errBadAPI, mBadAPI)
    	} else {
    		writeResponseToClient(clientID, res)
    	}
    }
    

      

    func dyRouter(URL string, userID string, req interface{}) (response *ServerResponse) {
    	var (
    		code   int         = Success
    		msg    string      = ""
    		result interface{} = nil
    	)
    	defer func() {
    		if code != Success {
    			logs.Print("code:", code, "	msg:", msg, "	result", result)
    		}
    		response = &ServerResponse{
    			Method:  URL,
    			Code:    code,
    			Message: msg,
    			Result:  result,
    		}
    	}()
    	dataMap, ok := req.(map[string]interface{})
    	if !ok {
    		code = errAssertType
    		msg = "type isn't map[string]interface{}"
    		return
    	}
    	var data dy_judge = dataMap
    	switch URL {
    	case "/createGroup":
    		code, msg, result = createGroup(userID, data)
    		break
    	case "/addGroupMember":
    		code, msg, result = addGroupMember(userID, data)
    		break
    	case "/addGroupMemberList":
    		code, msg, result = addGroupMemberList(userID, data)
    		break
    	case "/deleteGroupMember":
    		code, msg, result = deleteGroupMember(userID, data)
    		break
    	}
    	return
    }
    

      

    func chatMessageList(clientID string, args interface{}) (response *ServerResponse) {
    
    	var errCode = Success
    	var errMsg = ""
    	var res interface{}
    	defer func() {
    		response = &ServerResponse{
    			Method:  "/chatMessageList",
    			Code:    errCode,
    			Message: errMsg,
    			Result:  res,
    		}
    	}()
    
    	var ok = false
    	var judger FSJudger
    	if ok, errCode, errMsg, judger = initJudger(args); !ok {
    		return
    	}
    
    	userID := clientID
    	talkerID := ""
    	if ok, errCode, errMsg, talkerID = judger.isValidID("talkerID"); !ok {
    		return
    	}
    	if userID == talkerID {
    		errCode = errInvalidTalker
    		errMsg = mInvalidChat
    		return
    	}
    	talkerIsUser := false
    	if talkerIsUser, ok = judger.boolValueOf("talkerIsUser"); !ok {
    		return
    	}
    	if talkerIsUser {
    		if ok, errCode, errMsg = isExistTalkerID(userID, talkerID, true); !ok {
    			return
    		}
    	} else {
    		if ok, errCode, errMsg = isExistTalkerID(userID, talkerID, false); !ok {
    			return
    		}
    	}
    	pageIndex := 0
    	if ok, errCode, errMsg, pageIndex = judger.isValidPageIndex(); !ok {
    		return
    	}
    	pageSize := 0
    	if ok, errCode, errMsg, pageSize = judger.isValidPageSize(); !ok {
    		return
    	}
    
    	errCode, errMsg, res = getMsgListFromDB(userID, talkerID, talkerIsUser, pageIndex, pageSize)
    	return
    }
    

      

  • 相关阅读:
    Httpclient请求数据(post)
    实现定位(无基本地图)
    Httpclient请求数据
    带参数路径的刷新和加载
    AsyncTask异步交互和httpurlconnection结合使用
    判断网络状态
    Android数字签名
    app加固
    定位和xml解析和gson解析加上拉加载,下拉刷新
    下载app后自动安装程序
  • 原文地址:https://www.cnblogs.com/cdyboke/p/7018272.html
Copyright © 2020-2023  润新知