fix: tolerate numeric recharge timestamps

This commit is contained in:
ZuoZuo 2026-05-01 16:02:35 +08:00
parent 157307a474
commit ba8ed01b99
2 changed files with 53 additions and 2 deletions

View File

@ -211,8 +211,8 @@ type UserTotalRecharge struct {
UserID Int64Value `json:"userId"`
Type string `json:"type"`
Amount DecimalString `json:"amount"`
CreateTime string `json:"createTime"`
UpdateTime string `json:"updateTime"`
CreateTime StringValue `json:"createTime"`
UpdateTime StringValue `json:"updateTime"`
}
type FreightSellerPage struct {
@ -343,6 +343,26 @@ func (v *DecimalString) UnmarshalJSON(data []byte) error {
return nil
}
type StringValue string
func (v *StringValue) UnmarshalJSON(data []byte) error {
raw := strings.TrimSpace(string(data))
if raw == "" || raw == "null" {
*v = ""
return nil
}
if strings.HasPrefix(raw, "\"") && strings.HasSuffix(raw, "\"") {
var parsed string
if err := json.Unmarshal(data, &parsed); err != nil {
return err
}
*v = StringValue(parsed)
return nil
}
*v = StringValue(raw)
return nil
}
func New(cfg config.Config) *Client {
return &Client{
cfg: cfg,

View File

@ -0,0 +1,31 @@
package integration
import (
"encoding/json"
"testing"
)
func TestUserTotalRechargeAcceptsNumericTimestamps(t *testing.T) {
var item UserTotalRecharge
raw := []byte(`{
"id":"2049097310624870401",
"userId":"2042274349343506434",
"type":"SHIPPING_AGENT",
"amount":1.60,
"createTime":1777377844000,
"updateTime":1777556451000
}`)
if err := json.Unmarshal(raw, &item); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if item.CreateTime != "1777377844000" {
t.Fatalf("CreateTime = %q, want numeric timestamp string", item.CreateTime)
}
if item.UpdateTime != "1777556451000" {
t.Fatalf("UpdateTime = %q, want numeric timestamp string", item.UpdateTime)
}
if item.Amount != "1.60" {
t.Fatalf("Amount = %q, want 1.60", item.Amount)
}
}