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

Any way to specify additional formats to use when unmarshalling strfmt.DateTime values? #70

Closed
padamstx opened this issue Nov 10, 2020 · 5 comments · Fixed by #71
Closed

Comments

@padamstx
Copy link
Contributor

Hi, I have a question about the strfmt.DateTime type.
Is there any way that I can leverage the unmarshal functionality of strfmt.DateTime while supporting one or more additional formats for date-time values? Logically, what I'd like to do is add one or more formats to the "dateTimeFormats" list that's defined in time.go.

Failing that, is there perhaps a way that I could define my own UnmarshalJSON() function that would be used instead of the one defined in the strfmt package?

Thanks

@casualjim
Copy link
Member

You can replace registered type in the default dictionary to a new datetime type.
Other than that you can submit a PR that provides a hook to add more date formats (like export the dateTimeFormats field).

This is a very common ask.

@padamstx
Copy link
Contributor Author

You can replace registered type in the default dictionary to a new datetime type.

Hi @casualjim, thanks for the quick reply... Could you perhaps provide a brief example of how I might do this so that I could "override" the Unmarshal-related functions to support additional formats? I'm not familiar with replacing types in the default dictionary. Thanks in advance!

@casualjim
Copy link
Member

Every one of the strfmt types registers itself into a dictionary. https://github.com/go-openapi/strfmt/blob/master/time.go#L31-L34
Simply replacing the value for the key in the dictionary replaces the serializer/deserializer provided that it implements the right set of interfaces.

IMO the right way to solve is by exporting this: https://github.com/go-openapi/strfmt/blob/master/time.go#L75

padamstx added a commit to padamstx/strfmt that referenced this issue Nov 15, 2020
Closes go-openapi#70

This commit exports the DateTimeFormats variable so that users
can modify the set of formats used by ParseDateTime, as in this
example:
    strfmt.DateTimeFormats = append(strfmt.DateTimeFormats, "2006-01-02T15:04Z0700")

Signed-off-by: Phil Adams <[email protected]>
@padamstx
Copy link
Contributor Author

@casualjim PR opened. Let me know if anything needs to change. Thanks for your help on this.

@padamstx
Copy link
Contributor Author

Thanks for getting that merged in so quickly!

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

Successfully merging a pull request may close this issue.

2 participants