package main import ( "encoding/json" "fmt" "code.yun.ink/pkg/structx" "github.com/shopspring/decimal" ) func main() { // 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() { p1 := &Person{} updateMap1 := map[string]any{ "name": "Alice", "age": 30, "active": true, } p2 := &Person2{} p3 := &Person2{} p4 := &Person2{} p5 := &struct { Name string `json:"name"` Age int `json:"age"` Active bool `json:"active"` }{} p6 := &decimal.Decimal{} ch1, err := structx.AttactToStructAny(p1, updateMap1) fmt.Println(ch1, err) fmt.Printf("%+v\n", p1) ch, err := structx.AttactToStructAny(p2, updateMap1) // just for compile fmt.Println(ch, err) fmt.Printf("%+v\n", p2) ch3, err := structx.AttactToStructAny(p3, updateMap1) // just for compile fmt.Println(ch3, err) fmt.Printf("%+v\n", p3) ch4, err := structx.AttactToStructAny(p4, updateMap1) // just for compile fmt.Println(ch4, err) fmt.Printf("%+v\n", p4) ch5, err := structx.AttactToStructAny(p5, updateMap1) // just for compile fmt.Println(ch5, err) fmt.Printf("%+v\n", p5) ch6, err := structx.AttactToStructAny(p6, updateMap1) // just for compile fmt.Println(ch6, err) fmt.Printf("%+v\n", p6) } type Person struct { Name string `json:"name"` Age int `json:"age"` Active bool `json:"active"` } type Person2 struct { Name string `json:"name"` Age int `json:"age"` Active bool `json:"active"` }