()
| 15 | ) |
| 16 | |
| 17 | func ExampleValueBinder_BindErrors() { |
| 18 | // example route function that binds query params to different destinations and returns all bind errors in one go |
| 19 | routeFunc := func(c *echo.Context) error { |
| 20 | var opts struct { |
| 21 | IDs []int64 |
| 22 | Active bool |
| 23 | } |
| 24 | length := int64(50) // default length is 50 |
| 25 | |
| 26 | b := echo.QueryParamsBinder(c) |
| 27 | |
| 28 | errs := b.Int64("length", &length). |
| 29 | Int64s("ids", &opts.IDs). |
| 30 | Bool("active", &opts.Active). |
| 31 | BindErrors() // returns all errors |
| 32 | if errs != nil { |
| 33 | for _, err := range errs { |
| 34 | bErr := err.(*echo.BindingError) |
| 35 | log.Printf("in case you want to access what field: %s values: %v failed", bErr.Field, bErr.Values) |
| 36 | } |
| 37 | return fmt.Errorf("%v fields failed to bind", len(errs)) |
| 38 | } |
| 39 | fmt.Printf("active = %v, length = %v, ids = %v", opts.Active, length, opts.IDs) |
| 40 | |
| 41 | return c.JSON(http.StatusOK, opts) |
| 42 | } |
| 43 | |
| 44 | e := echo.New() |
| 45 | c := e.NewContext( |
| 46 | httptest.NewRequest(http.MethodGet, "/api/endpoint?active=true&length=25&ids=1&ids=2&ids=3", nil), |
| 47 | httptest.NewRecorder(), |
| 48 | ) |
| 49 | |
| 50 | _ = routeFunc(c) |
| 51 | |
| 52 | // Output: active = true, length = 25, ids = [1 2 3] |
| 53 | } |
| 54 | |
| 55 | func ExampleValueBinder_BindError() { |
| 56 | // example route function that binds query params to different destinations and stops binding on first bind error |
nothing calls this directly
no test coverage detected
searching dependent graphs…