Hello, I am trying to test a slightly modified example of coder directive
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/ggicci/httpin"
"github.com/justinas/alice"
)
type MyDate time.Time // adapted time.Time to MyDate, MyDate must implement httpin_core.Stringable
func (t MyDate) ToString() (string, error) {
return time.Time(t).Format("2006-01-02"), nil
}
func (t *MyDate) FromString(value string) error {
fmt.Println("FromString called with value:", value)
v, err := time.Parse("2006-01-02", value)
if err != nil {
return fmt.Errorf("invalid date: %w", err)
}
*t = MyDate(v)
return nil
}
type ListUsersInput struct {
Payload struct {
Birthday MyDate `json:"birthday"`
} `in:"body=json"`
}
func ListUsers(rw http.ResponseWriter, r *http.Request) {
input := r.Context().Value(httpin.Input).(*ListUsersInput)
fmt.Printf("input: %#v\n", input)
}
func main() {
mux := http.NewServeMux()
mux.Handle("/users", alice.New(
httpin.NewInput(ListUsersInput{}),
).ThenFunc(ListUsers))
r, _ := http.NewRequest("POST", "/users", nil)
rw := httptest.NewRecorder()
mux.ServeHTTP(rw, r)
}
From the comment, I see
// By default, the decoder is auto-selected by the field type, which is of time.Time. so I would have expected the FromString to be called automatically (since I implemented the Stringable interface on the type MyDate). How is it supposed to work?
Hello, I am trying to test a slightly modified example of coder directive
From the comment, I see
// By default, the decoder is auto-selected by the field type, which is of time.Time.so I would have expected the FromString to be called automatically (since I implemented theStringableinterface on the typeMyDate). How is it supposed to work?