-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Is your feature request related to a problem? Please describe.
Yes, it's, so often when you've a time.Time value or even a pointer *time.Time on your struct to expose the tipical timestamp field, you aim to choose how this date will be finally marshalled / serialized on its JSON representation being the most common cases RFC3339 or ISO8601 formats respectively.
Nowadays within the available marshalling methods provided by this library we don't have an specific way to indicate how we'd like to treat those time.Time fields in serialization time.
Describe the solution you'd like
I wouldn't like to make an obnoxious comparison between this library and the ones provided by google (currently archived), but digging on its commits and using it during a long period of time in several projects, I've found really interesting the way that they handle the time.Time formatting through the jsonapi tag indicating as a second or third parameter the date format desired, being the values allowed rfc339 or iso8601 formats respectively.
Here you can find the commit where the implementation has been done by @aren55555 at the time this library was actively maintained and used.
Describe alternatives you've considered
- Provide formats through
jsonapi:attr,<field_name:optional>,<format:optional>and by default if there's not format provided simply calltime.String()or even usetime.RFC3339Nano.
type User struct {
ID string `jsonapi:"primary,user"`
Name string `jsonapi:"attr" json:"name"`
CreatedAt time.Time `jsonapi:"attr,rfc3339" json:"created_at"`
}- Provide a global config API to set globally at package level how
time.Timemust be serialized
package jsonapi
import (
"errors"
"time"
)
var (
// defaultTimeFormat is the default time format for the messaging package.
// It is used to serialize time.Time values.
// The default format is empty time.RFC3339Nano
defaultTimeFormat string = time.RFC3339Nano
)
// SetTimeFormat sets the time format for the messaging package.
func SetTimeFormat(format TimeFormatProvider) error {
providedFormat := format()
if validateTimeFormat(providedFormat) {
defaultTimeFormat = providedFormat
return nil
}
return errors.New("invalid time format provided")
}
func validateTimeFormat(format string) bool {
_, err := time.Now().Format(format), error(nil)
return err == nil
}
// TimeFormatProvider is an interface for providing time format information.
type TimeFormatProvider func() string
// GetTimeFormat returns the current time format used by the messaging package.
func GetTimeFormat() string {
return defaultTimeFormat
}
// ResetTimeFormat resets the time format to the default value.
func ResetTimeFormat() {
defaultTimeFormat = time.RFC3339Nano
}
// RFC3339TimeFormatProvider returns a time format provider that provides RFC3339 format.
func RFC3339TimeFormatProvider() TimeFormatProvider {
return func() string {
return time.RFC3339
}
}
// ISO8601TimeFormatProvider returns a time format provider that provides ISO8601 format.
func ISO8601TimeFormatProvider() TimeFormatProvider {
return func() string {
return "2006-01-02T15:04:05Z"
}
}Additional context
- I'll be more tha glad to help adding this feature to the library through a PR and even collaborate with someone else who also aims to get this feature available within this library.