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

how to use Echo#BindUnmarshaler to bind date time in v4 #1571

Closed
3 tasks done
shyandsy opened this issue May 12, 2020 · 2 comments
Closed
3 tasks done

how to use Echo#BindUnmarshaler to bind date time in v4 #1571

shyandsy opened this issue May 12, 2020 · 2 comments
Labels

Comments

@shyandsy
Copy link

Issue Description

the frontend post expired time to server-side, like "2020-11-12 13:14:15"

the server-side try to bind this value to the expired field in the model class

u := &models.User{}
if err := c.Bind(u); err != nil {
	panic(err)
}

the error message shows below

{"message":"parsing time \"\"2020-11-12 13:11:12\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \" 13:11:12\"\" as \"T\""}

I checked the document for Echo#BindUnmarshaler

https://echo.labstack.com/guide/request

unfortunately, the document only mentioned it, and no details for how to use it

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

  1. are there any way to change post data before bind to model, like below
c.FormValue("expired") = c.FormValue("expired") + "Z"
  1. are there any way to bind the time with customize format, rather than time.RFC3339

Version/commit

V4

@matcornic
Copy link
Contributor

matcornic commented May 19, 2020

Universal solution

One universal solution would be to use a custom UnmarshalJson on your time inside models.User type and basically parse the time with your format.

See Custom unmarshall JSON

I already did something like this with the XML unmarshaller (principle is the same):

type CustomTime time.Time

func (ct *CustomTime) String() string {
	return time.Time(*st).String()
}

func (ct *CustomTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var s string
	err := d.DecodeElement(&s, &start)
	if err != nil {
		return err
	}
	t, err := time.Parse(`2006-01-02 15:04:05 MST`, s)
	if err != nil {
		return err
	}
	*st = CustomTime(t)
	return nil
}

Another solution (if possible) would be to use Moment.js on the front-end to send the right format to the server.


Echo solution

Echo solution is to implement the following interface:

	BindUnmarshaler interface {
		// UnmarshalParam decodes and assigns a value from an form or query param.
		UnmarshalParam(param string) error
	}

So you need to add a UnmarshalParam method to the custom time inside models.User type.

type CustomTime time.Time

func (ct *CustomTime) UnmarshalParam(param string) error {
        t, err := time.Parse(`2006-01-02 15:04:05 MST`, param)
	if err != nil {
		return err
	}
	*st = CustomTime(t)
	return nil
}

I did not test this solution though.

@stale
Copy link

stale bot commented Jul 18, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 18, 2020
@stale stale bot closed this as completed Jul 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants