Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Date format type from api calls #41

Open
erezKaufmann opened this issue Sep 12, 2024 · 0 comments
Open

Unhandled Date format type from api calls #41

erezKaufmann opened this issue Sep 12, 2024 · 0 comments

Comments

@erezKaufmann
Copy link

From the Selling Partner api, finances api returns Date format of type ISO 8601, but type.gen.go files do not handle it correctly, causing processes such as Parse function (ParseListFinancialEventsResp, ParseListFinancialEventsByOrderIdResp etc.) to fail:

Error: json: cannot unmarshal string into Go struct field ShipmentEvent.payload.FinancialEvents.ShipmentEventList.PostedDate of type finances.Date

In order to fix that, we should define Date as struct, and implement an unmarshalJson for Date:

// Date defines model for Date.
type Date struct {
    time.Time
}


// UnmarshalJSON implements the json.Unmarshaler interface for Date
func (d *Date) UnmarshalJSON(data []byte) error {
    // Remove quotes
    s := strings.Trim(string(data), "\"")
    
    // Parse the time using the correct layout
    t, err := time.Parse(time.RFC3339, s)
    if err != nil {
        layouts := []string{
            "2006-01-02T15:04:05Z07:00",
            "2006-01-02T15:04:05Z",
            "2006-01-02T15:04:05",
            "2006-01-02",
        }

        for _, layout := range layouts {
            if t, err = time.Parse(layout, s); err == nil {
                break
            }
        }

        if err != nil {
            return err
        }
    }
    
    d.Time = t
    return nil
}

I noticed this issue when trying to implement a new call for the finances api, but this could also happen on other apis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant