(b *testing.B)
| 160 | } |
| 161 | |
| 162 | func BenchmarkRawBunRouter(b *testing.B) { |
| 163 | type GreetingInput struct { |
| 164 | Suffix string `json:"suffix" maxLength:"5"` |
| 165 | } |
| 166 | |
| 167 | type GreetingOutput struct { |
| 168 | Schema string `json:"$schema"` |
| 169 | Greeting string `json:"greeting"` |
| 170 | Suffix string `json:"suffix"` |
| 171 | Length int `json:"length"` |
| 172 | ContentType string `json:"content_type"` |
| 173 | Num int `json:"num"` |
| 174 | } |
| 175 | |
| 176 | registry := huma.NewMapRegistry("#/components/schemas/", |
| 177 | func(t reflect.Type, hint string) string { |
| 178 | return t.Name() |
| 179 | }) |
| 180 | schema := registry.Schema(reflect.TypeFor[GreetingInput](), false, "") |
| 181 | |
| 182 | strSchema := registry.Schema(reflect.TypeFor[string](), false, "") |
| 183 | numSchema := registry.Schema(reflect.TypeFor[int](), false, "") |
| 184 | |
| 185 | r := bunrouter.New() |
| 186 | |
| 187 | r.POST("/foo/:id", func(w http.ResponseWriter, r bunrouter.Request) error { |
| 188 | pb := huma.NewPathBuffer([]byte{}, 0) |
| 189 | res := &huma.ValidateResult{} |
| 190 | |
| 191 | // Read and validate params |
| 192 | id := r.Param("id") |
| 193 | huma.Validate(registry, strSchema, pb, huma.ModeReadFromServer, id, res) |
| 194 | |
| 195 | ct := r.Header.Get("Content-Type") |
| 196 | huma.Validate(registry, strSchema, pb, huma.ModeReadFromServer, ct, res) |
| 197 | |
| 198 | num, err := strconv.Atoi(r.URL.Query().Get("num")) |
| 199 | if err != nil { |
| 200 | return err |
| 201 | } |
| 202 | huma.Validate(registry, numSchema, pb, huma.ModeReadFromServer, num, res) |
| 203 | |
| 204 | // Read and validate body |
| 205 | defer r.Body.Close() |
| 206 | data, err := io.ReadAll(r.Body) |
| 207 | if err != nil { |
| 208 | return err |
| 209 | } |
| 210 | |
| 211 | var tmp any |
| 212 | if err := json.Unmarshal(data, &tmp); err != nil { |
| 213 | return err |
| 214 | } |
| 215 | |
| 216 | huma.Validate(registry, schema, pb, huma.ModeWriteToServer, tmp, res) |
| 217 | if len(res.Errors) > 0 { |
| 218 | return fmt.Errorf("%v", res.Errors) |
| 219 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…