| 53 | } |
| 54 | |
| 55 | func ExampleValueBinder_BindError() { |
| 56 | // example route function that binds query params to different destinations and stops binding on first bind error |
| 57 | failFastRouteFunc := func(c *echo.Context) error { |
| 58 | var opts struct { |
| 59 | IDs []int64 |
| 60 | Active bool |
| 61 | } |
| 62 | length := int64(50) // default length is 50 |
| 63 | |
| 64 | // create binder that stops binding at first error |
| 65 | b := echo.QueryParamsBinder(c) |
| 66 | |
| 67 | err := b.Int64("length", &length). |
| 68 | Int64s("ids", &opts.IDs). |
| 69 | Bool("active", &opts.Active). |
| 70 | BindError() // returns first binding error |
| 71 | if err != nil { |
| 72 | bErr := err.(*echo.BindingError) |
| 73 | return fmt.Errorf("my own custom error for field: %s values: %v", bErr.Field, bErr.Values) |
| 74 | } |
| 75 | fmt.Printf("active = %v, length = %v, ids = %v\n", opts.Active, length, opts.IDs) |
| 76 | |
| 77 | return c.JSON(http.StatusOK, opts) |
| 78 | } |
| 79 | |
| 80 | e := echo.New() |
| 81 | c := e.NewContext( |
| 82 | httptest.NewRequest(http.MethodGet, "/api/endpoint?active=true&length=25&ids=1&ids=2&ids=3", nil), |
| 83 | httptest.NewRecorder(), |
| 84 | ) |
| 85 | |
| 86 | _ = failFastRouteFunc(c) |
| 87 | |
| 88 | // Output: active = true, length = 25, ids = [1 2 3] |
| 89 | } |
| 90 | |
| 91 | func ExampleValueBinder_CustomFunc() { |
| 92 | // example route function that binds query params using custom function closure |