go-join——按规则快速拼接字符串的工具

前言

在接入第三方账号、支付等体系时,难免要按照平台的要求对字符串按照指定的规则进行拼接,然后再进行加密、签名等之类的处理。因不同的结构、排序规则等条件,代码处理也不尽相同,代码冗余度变高,复用率变低。为更便利地进行字符串拼接,本人对常见的规则做了封装处理,封装成一个工具go-join,仓库具体地址为:https://github.com/yuchenfw/go-join。欢迎大家使用并提出意见。

go-join

go-join是一个可以快速按照指定规则拼接字符串的工具,主要用于按照特定key顺序的数据拼接。

拼接的格式:

key1KVSepvalue1Sepkey2KVSepvalue2SepSepkeyNKVSepvalueN

get

如果使用go mod ,将以下内容添加至go.mod文件中即可。

require github.com/yuchenfw/go-join latest

如果使用gopath,使用以下方式添加。

go get -u github.com/yuchenfw/go-join

import

import "github.com/yuchenfw/go-join"

Quick Start

go-join仅有一个func,就是Join。

gojoin.Join(src interface{}, options Options) (dst string, err error) {

Join支持map(包括 url.Values)、struct、encoded的url string及它们的指针形式。

options是拼接的具体参数设置,结构如下:

type Options struct {
    Sep           string    // keys对应values(可能包含keys)间的连接符
    KVSep         string    // key和它对应value间的连接符
    IgnoreKey     bool      // 是否忽略key,如果true,则连带其对应的value一起忽略
    IgnoreEmpty   bool      // 是否忽略空值(主要针对空字符串)的key,如果是,当其值为空时忽略
    ExceptKeys    []string  // 被排除拼接的keys,直接忽略
    Order         joinOrder // 拼接规则
    DefinedOrders []string  // 当Order == Defined时,指定的keys顺序
    StructTag     string    // struct tag,用于指定field别名,如不指定则使用field name,仅支持struct且export的fields
    URLCoding     urlCoding // key对应value的处理方式
    Unwrap        bool      // 当内部存在复合类型如map,struct,是否解析,是则解析,默认false。
}

注意:

如果key对应的value是复合类型时,处理规则如下:

复合类型处理规则
slice/array如果len>=1,取第一个值;否则,内部元素类型是基本类型的取零值,其他类型直接忽略
map/struct直接忽略

如果需要解析map或者struct,需要设置Unwrap=true

另外,多个复合类型中如果出现的同名key,后出现的会覆盖之前出现的key。

examples

1.Join map

gojoin.Join(map[string]interface{}{
               "total_amount": "2.00",
               "buyer_id":     "2088102116773037",
               "body":         "大乐透2.1",
               "trade_no":     "2016071921001003030200089909",
               "refund_fee":   "0.00",
               "notify_time":  "2016-07-19 14:10:49",
               "subject":      "大乐透2.1",
               "sign_type":    "RSA2",
               "charset":      "utf-8",
               "notify_type":  "trade_status_sync",
               "out_trade_no": "0719141034-6418",
               "gmt_close":    "2016-07-19 14:10:46",
               "gmt_payment":  "2016-07-19 14:10:47",
               "trade_status": "TRADE_SUCCESS",
               "version":      "1.0",
               "sign":         "kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=",
               "gmt_create":   "2016-07-19 14:10:44",
               "app_id":       "2015102700040153",
               "seller_id":    "2088102119685838",
               "notify_id":    "4a91b7a78a503640467525113fb7d8bg8e",
            },Options{
               Sep:        "&",
               KVSep:      "=",
               Order:      ASCII,
               ExceptKeys: []string{"sign", "sign_type"},
            })

the join result is app_id=2015102700040153&body=大乐透2.1&buyer_id=2088102116773037&charset=utf-8&gmt_close=2016-07-19 14:10:46&gmt_create=2016-07-19 14:10:44&gmt_payment=2016-07-19 14:10:47&notify_id=4a91b7a78a503640467525113fb7d8bg8e&notify_time=2016-07-19 14:10:49&notify_type=trade_status_sync&out_trade_no=0719141034-6418&refund_fee=0.00&seller_id=2088102119685838&subject=大乐透2.1&total_amount=2.00&trade_no=2016071921001003030200089909&trade_status=TRADE_SUCCESS&version=1.0

2.Join struct

gojoin.Join(callbackWrap{
               &callback{
                  "2.00",
                  "2088102116773037",
                  "大乐透2.1",
                  "2016071921001003030200089909",
                  "0.00",
                  "2016-07-19 14:10:49",
                  "大乐透2.1",
                  "RSA2",
                  "utf-8",
                  "trade_status_sync",
                  "0719141034-6418",
                  "2016-07-19 14:10:46",
                  "2016-07-19 14:10:47",
                  "TRADE_SUCCESS",
                  "1.0",
                  "kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=",
                  "2016-07-19 14:10:44",
                  "2015102700040153",
                  "2088102119685838",
                  "4a91b7a78a503640467525113fb7d8bg8e",
               },
            },Options{
               Sep:        "&",
               KVSep:      "=",
               Order:      ASCII,
               ExceptKeys: []string{"sign", "sign_type"},
               Unwrap:     true,
               StructTag:  "json",
            })

type callback struct {
   TotalAmount string `json:"total_amount"`
   BuyerId     string `json:"buyer_id"`
   Body        string `json:"body"`
   TradeNo     string `json:"trade_no"`
   RefundFee   string `json:"refund_fee"`
   NotifyTime  string `json:"notify_time"`
   Subject     string `json:"subject"`
   SignType    string `json:"sign_type"`
   Charset     string `json:"charset"`
   NotifyType  string `json:"notify_type"`
   OutTradeNo  string `json:"out_trade_no"`
   GmtClose    string `json:"gmt_close"`
   GmtPayment  string `json:"gmt_payment"`
   TradeStatus string `json:"trade_status"`
   Version     string `json:"version"`
   Sign        string `json:"sign"`
   GmtCreate   string `json:"gmt_create"`
   AppId       string `json:"app_id"`
   SellerId    string `json:"seller_id"`
   NotifyId    string `json:"notify_id"`
}

type callbackWrap struct {
   Callback *callback `json:"callback"`
}

the join result is app_id=2015102700040153&body=大乐透2.1&buyer_id=2088102116773037&charset=utf-8&gmt_close=2016-07-19 14:10:46&gmt_create=2016-07-19 14:10:44&gmt_payment=2016-07-19 14:10:47&notify_id=4a91b7a78a503640467525113fb7d8bg8e&notify_time=2016-07-19 14:10:49&notify_type=trade_status_sync&out_trade_no=0719141034-6418&refund_fee=0.00&seller_id=2088102119685838&subject=大乐透2.1&total_amount=2.00&trade_no=2016071921001003030200089909&trade_status=TRADE_SUCCESS&version=1.0

3.Join url encoded string

gojoin.Join("https://api.xx.com/receive_notify.htm?total_amount=2.00&buyer_id=2088102116773037&body=大乐透2.1&trade_no=2016071921001003030200089909&refund_fee=0.00&notify_time=2016-07-19 14:10:49&subject=大乐透2.1&sign_type=RSA2&charset=utf-8&notify_type=trade_status_sync&out_trade_no=0719141034-6418&gmt_close=2016-07-19 14:10:46&gmt_payment=2016-07-19 14:10:47&trade_status=TRADE_SUCCESS&version=1.0&sign=kPbQIjX%2bxQc8F0%2fA6%2fAocELIjhhZnGbcBN6G4MM%2fHmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo%2blHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l%2fKL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM%3d&gmt_create=2016-07-19 14:10:44&app_id=2015102700040153&seller_id=2088102119685838&notify_id=4a91b7a78a503640467525113fb7d8bg8e",Options{
               Sep:        "&",
               KVSep:      "=",
               Order:      ASCII,
               StructTag:  "json",
               ExceptKeys: []string{"sign", "sign_type"},
            })

the join result is app_id=2015102700040153&body=大乐透2.1&buyer_id=2088102116773037&charset=utf-8&gmt_close=2016-07-19 14:10:46&gmt_create=2016-07-19 14:10:44&gmt_payment=2016-07-19 14:10:47&notify_id=4a91b7a78a503640467525113fb7d8bg8e&notify_time=2016-07-19 14:10:49&notify_type=trade_status_sync&out_trade_no=0719141034-6418&refund_fee=0.00&seller_id=2088102119685838&subject=大乐透2.1&total_amount=2.00&trade_no=2016071921001003030200089909&trade_status=TRADE_SUCCESS&version=1.0

公众号

鄙人刚刚开通了公众号,专注于分享Go开发相关内容,望大家感兴趣的支持一下,在此特别感谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值