Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3fb090a620 |
+53
-1
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.yun.ink/pkg/structx"
|
"code.yun.ink/pkg/structx"
|
||||||
@@ -9,10 +10,61 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
demo1()
|
// demo1()
|
||||||
|
demo2()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func demo2() {
|
||||||
|
str := `{
|
||||||
|
"appid": "1023538",
|
||||||
|
"total_fee": "100",
|
||||||
|
"key": "fIIu2UQb6jvdkOGqIx1zvXzc4grCkjb6",
|
||||||
|
"payment": "alipay.qrcode",
|
||||||
|
"notify_url": "http://admin-v2.hk.blueoceantech.co/api/demo",
|
||||||
|
"sign": "12345"
|
||||||
|
}`
|
||||||
|
p := &PayReq{}
|
||||||
|
|
||||||
|
m := map[string]any{}
|
||||||
|
json.Unmarshal([]byte(str), &m)
|
||||||
|
|
||||||
|
ch2, err := structx.NewStructProcessor(structx.AllowUnknownFields()).AttactToStructAny(p, m)
|
||||||
|
fmt.Println(ch2, err)
|
||||||
|
fmt.Printf("p:%+v\n", p)
|
||||||
|
|
||||||
|
ch1, err := structx.AttactToStructAny(p, m)
|
||||||
|
fmt.Println(ch1, err)
|
||||||
|
fmt.Printf("p:%+v\n", p)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
type PaymentMethod string
|
||||||
|
type Provider string
|
||||||
|
type OrderWallet string
|
||||||
|
type CoinType string
|
||||||
|
type PayReq struct {
|
||||||
|
Appid int64 `json:"appid"` // 必填 appid,由商户后台获取,或者登录获取
|
||||||
|
StoreId int64 `json:"storeid"` // 选填 storeid,指定门店交易
|
||||||
|
OutTradeNo string `json:"out_trade_no"` // 选填 商户自身订单号
|
||||||
|
Payment PaymentMethod `json:"payment"` // 必填 支付方式
|
||||||
|
H5RedirectUrl string `json:"h5_redirect_url"` // 选填 微信香港钱包公众号支付跳转url,支付宝WAP跳转ur
|
||||||
|
SpbillCreateIp string `json:"spbill_create_ip"` // 选填 客户IP 支付用户的IP,微信H5(MWEB)必传
|
||||||
|
TotalFee decimal.Decimal `json:"total_fee"` // 必填 订单金额(单位分)
|
||||||
|
Sign string `json:"sign"` // 必填 签名
|
||||||
|
Provider Provider `json:"provider"` // 选填 支付提供方:alipay wechat [可传但需过滤]
|
||||||
|
Code string `json:"code"` // 选填 支付授权码(micropay必填)
|
||||||
|
NotifyUrl string `json:"notify_url"` // 选填 异步通知url
|
||||||
|
// OpenId string `json:"open_id"` // 选填 openid 机构商户appid下的唯一标识。openid和sub_openid可以选传其中之一
|
||||||
|
SubAppid string `json:"sub_appid"` // 选填 子商户公众号的微信APPID
|
||||||
|
SubOpenid string `json:"sub_openid"` // 选填 openid 商户公众号、小程序获取的openid 用户在子商户appid下的唯一标识。openid和sub_openid可以选传其中之一
|
||||||
|
Body string `json:"body"` // 选填 商品名称(显示)
|
||||||
|
Attach string `json:"attach"` // 选填 附加数据(客户自己的)
|
||||||
|
Wallet OrderWallet `json:"wallet"` // 选填 钱包(支付宝需要)
|
||||||
|
Currency CoinType `json:"currency"` // 选填 客户的下单币种(需要根据不同平台指定默认值)
|
||||||
|
TimeoutSecond int64 `json:"timeout_second"` // 选填 客户的下单超时时间(需要根据不同平台指定默认值)
|
||||||
|
}
|
||||||
|
|
||||||
func demo1() {
|
func demo1() {
|
||||||
|
|
||||||
p1 := &Person{}
|
p1 := &Person{}
|
||||||
|
|||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
package structx
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
// map允许传入struct不存在的字段
|
||||||
|
allowUnknownFields bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
func defaultOptions() *Options {
|
||||||
|
return &Options{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 允许传入struct不存在的字段
|
||||||
|
func AllowUnknownFields() Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.allowUnknownFields = true
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
-3
@@ -19,13 +19,21 @@ func AttactToStruct(structxx any, updateMap map[string]string) (map[string]Chang
|
|||||||
|
|
||||||
// StructProcessor 结构体处理器
|
// StructProcessor 结构体处理器
|
||||||
type StructProcessor struct {
|
type StructProcessor struct {
|
||||||
|
options *Options
|
||||||
fieldMapper FieldMapper
|
fieldMapper FieldMapper
|
||||||
valueSetter ValueSetter
|
valueSetter ValueSetter
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStructProcessor 创建新的结构体处理器
|
// NewStructProcessor 创建新的结构体处理器
|
||||||
func NewStructProcessor() *StructProcessor {
|
func NewStructProcessor(ops ...Option) *StructProcessor {
|
||||||
|
|
||||||
|
options := defaultOptions()
|
||||||
|
for _, op := range ops {
|
||||||
|
op(options)
|
||||||
|
}
|
||||||
|
|
||||||
return &StructProcessor{
|
return &StructProcessor{
|
||||||
|
options: options,
|
||||||
fieldMapper: &defaultFieldMapper{},
|
fieldMapper: &defaultFieldMapper{},
|
||||||
valueSetter: &defaultValueSetter{},
|
valueSetter: &defaultValueSetter{},
|
||||||
}
|
}
|
||||||
@@ -55,13 +63,17 @@ func (sp *StructProcessor) AttactToStruct(structxx any, updateMap map[string]str
|
|||||||
v = v.Elem()
|
v = v.Elem()
|
||||||
|
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
|
|
||||||
fieldMap := sp.fieldMapper.GetFieldMap(t)
|
fieldMap := sp.fieldMapper.GetFieldMap(t)
|
||||||
|
|
||||||
for mapKey, mapValue := range updateMap {
|
for mapKey, mapValue := range updateMap {
|
||||||
fieldInfo, exists := fieldMap[mapKey]
|
fieldInfo, exists := fieldMap[mapKey]
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, fmt.Errorf("字段 %s 不存在", mapKey)
|
if sp.options.allowUnknownFields {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("字段 %s 不存在", mapKey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
field, err := sp.getFieldByPath(v, fieldInfo.Index)
|
field, err := sp.getFieldByPath(v, fieldInfo.Index)
|
||||||
|
|||||||
Reference in New Issue
Block a user