-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoc.go
More file actions
698 lines (697 loc) · 21.2 KB
/
doc.go
File metadata and controls
698 lines (697 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// Copyright 2025 The Rivaas Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package binding provides request data binding for HTTP handlers.
//
// The binding package maps values from various sources (query parameters,
// form data, JSON bodies, headers, cookies, path parameters) into Go structs
// using struct tags. It supports nested structs, slices, maps, pointers,
// custom types, default values, and type conversion.
//
// Note: For validation (required fields, enum constraints, etc.), use the
// rivaas.dev/validation package separately after binding.
//
// The binding package follows the functional options pattern:
// - Options apply to an internal config struct
// - New() validates the config and builds the Binder from it
// - New() returns (*Binder, error); MustNew() panics on error
//
// # Quick Start
//
// The package provides both generic and non-generic APIs:
//
// // Generic (preferred when type is known)
// user, err := binding.JSON[CreateUserRequest](body)
//
// // Non-generic (when type comes from variable)
// var user CreateUserRequest
// err := binding.JSONTo(body, &user)
//
// # Source-Specific Functions
//
// Each binding source has dedicated functions:
//
// // Query parameters
// params, err := binding.Query[ListParams](r.URL.Query())
//
// // Path parameters
// params, err := binding.Path[GetUserParams](pathParams)
//
// // Form data
// data, err := binding.Form[FormData](r.PostForm)
//
// // HTTP headers
// headers, err := binding.Header[RequestHeaders](r.Header)
//
// // Cookies
// session, err := binding.Cookie[SessionData](r.Cookies())
//
// // Multipart form data (with file uploads)
// r.ParseMultipartForm(32 << 20)
// req, err := binding.Multipart[UploadRequest](r.MultipartForm)
//
// // JSON body
// user, err := binding.JSON[CreateUserRequest](body)
//
// // XML body
// user, err := binding.XML[CreateUserRequest](body)
//
// # Multi-Source Binding
//
// Bind from multiple sources using From* options:
//
// req, err := binding.Bind[CreateOrderRequest](
// binding.FromPath(pathParams),
// binding.FromQuery(r.URL.Query()),
// binding.FromHeader(r.Header),
// binding.FromJSON(body),
// )
//
// # Multipart Form Binding
//
// Bind multipart forms with file uploads directly to structs using the form tag.
// This enables handling file uploads and form values in a single struct:
//
// type UploadRequest struct {
// Avatar *File `form:"avatar"` // Single file
// Attachments []*File `form:"attachments"` // Multiple files
// Title string `form:"title"` // Regular form field
// Settings Config `form:"settings"` // JSON string auto-parsed
// }
//
// r.ParseMultipartForm(32 << 20) // Parse with 32MB max memory
// req, err := binding.Multipart[UploadRequest](r.MultipartForm)
// if err != nil {
// return err
// }
//
// // Access file properties
// fmt.Printf("File: %s (%d bytes, %s)\n",
// req.Avatar.Name, req.Avatar.Size, req.Avatar.ContentType)
//
// // Save file to disk
// req.Avatar.Save("./uploads/" + req.Avatar.Name)
//
// // Read file into memory (for small files)
// data, _ := req.Avatar.Bytes()
//
// // Stream large files
// reader, _ := req.Avatar.Open()
// defer reader.Close()
// io.Copy(destination, reader)
//
// File fields use the *File or []*File types and support the same struct tag syntax
// as other binding sources (defaults, aliases, etc.). The File type provides
// methods for reading, streaming, and saving uploaded files with built-in
// security (filename sanitization, path cleaning).
//
// ## JSON in Form Fields
//
// Multipart binding automatically detects and parses JSON strings in form fields:
//
// type PrintSettings struct {
// Orientation string `json:"orientation"`
// ColorMode string `json:"color_mode"`
// }
//
// type Request struct {
// Document *File `form:"document"`
// Settings PrintSettings `form:"settings"` // JSON auto-parsed
// }
//
// // HTML form:
// // <input type="file" name="document">
// // <input type="hidden" name="settings" value='{"orientation":"landscape","color_mode":"color"}'>
//
// If the form field value starts with { or [ and ends with } or ], the binding
// automatically attempts JSON unmarshaling. If JSON parsing fails, it falls back
// to dot-notation parsing (settings.orientation=landscape).
//
// ## File Security
//
// The File type includes built-in security features:
//
// - Filename sanitization: Removes path components (../, ..\, etc.)
// - Path cleaning: Save() validates destination paths
// - Content type detection: Extracts MIME type from headers
//
// Best practices for file handling:
//
// // Generate unique filenames to prevent overwrites
// uniqueName := uuid.New().String() + file.Ext()
// file.Save("./uploads/" + uniqueName)
//
// // Validate file size and type before processing
// if file.Size > maxSize {
// return errors.New("file too large")
// }
// if !allowedTypes[file.ContentType] {
// return errors.New("unsupported file type")
// }
//
// # Configuration
//
// Use functional options to customize binding behavior:
//
// user, err := binding.JSON[User](body,
// binding.WithUnknownFields(binding.UnknownError),
// binding.WithMaxDepth(16),
// )
//
// # Reusable Binder
//
// For shared configuration, create a Binder instance:
//
// binder := binding.MustNew(
// binding.WithConverter[uuid.UUID](uuid.Parse),
// binding.WithTimeLayouts("2006-01-02", "01/02/2006"),
// )
//
// // Use across handlers
// user, err := binder.JSON[CreateUserRequest](body)
// params, err := binder.Query[ListParams](r.URL.Query())
//
// # Struct Tags
//
// The package uses struct tags to map values:
//
// type Request struct {
// // Query parameters
// Page int `query:"page" default:"1"`
// Limit int `query:"limit" default:"20"`
//
// // Path parameters
// UserID string `path:"user_id"`
//
// // Headers
// Auth string `header:"Authorization"`
//
// // JSON body fields
// Name string `json:"name"`
// Email string `json:"email"`
//
// // For validation, use the validation package
// Status string `json:"status" validate:"required,oneof=active pending disabled"`
// }
//
// # Supported Tag Types
//
// - query: URL query parameters (?name=value)
// - path: URL path parameters (/users/:id)
// - form: Form data (application/x-www-form-urlencoded)
// - header: HTTP headers
// - cookie: HTTP cookies
// - json: JSON body fields
// - xml: XML body fields
//
// # Additional Serialization Formats
//
// The following formats are available as sub-packages:
//
// - rivaas.dev/binding/yaml: YAML support (gopkg.in/yaml.v3)
// - rivaas.dev/binding/toml: TOML support (github.com/BurntSushi/toml)
// - rivaas.dev/binding/msgpack: MessagePack support (github.com/vmihailenco/msgpack/v5)
// - rivaas.dev/binding/proto: Protocol Buffers support (google.golang.org/protobuf)
//
// Example with YAML:
//
// import "rivaas.dev/binding/yaml"
//
// config, err := yaml.YAML[Config](body)
//
// Example with TOML:
//
// import "rivaas.dev/binding/toml"
//
// config, err := toml.TOML[Config](body)
//
// Example with MessagePack:
//
// import "rivaas.dev/binding/msgpack"
//
// msg, err := msgpack.MsgPack[Message](body)
//
// Example with Protocol Buffers:
//
// import "rivaas.dev/binding/proto"
//
// user, err := proto.Proto[*pb.User](body)
//
// # Special Tags
//
// - default:"value": Default value when field is not present in the input.
// Works for all binding sources: query, path, header, cookie, form, and JSON body.
// Supported for all primitive types (string, int*, uint*, float*, bool),
// time.Time, time.Duration, net.IP, url.URL, and their pointer variants.
// For slices, use comma-separated values: default:"a,b,c"
// Whitespace around comma-separated elements is trimmed.
// Not supported for map fields.
//
// For JSON body binding, defaults are applied after decoding. A field
// explicitly sent as its zero value (e.g. {"active": false}) is NOT
// overwritten by the default — only fields absent from the JSON payload
// receive their default value. This also works for nested structs when the
// parent key is present in the JSON.
//
// For validation constraints (required, enum, etc.), use the rivaas.dev/validation
// package with the `validate` struct tag.
//
// # Type Conversion
//
// Built-in support for common types:
//
// - Primitives: string, int*, uint*, float*, bool
// - Time: time.Time, time.Duration
// - Network: net.IP, net.IPNet, url.URL
// - Slices: []T for any supported T
// - Maps: map[string]T for any supported T
// - Pointers: *T for any supported T
// - encoding.TextUnmarshaler implementations
//
// Register custom converters:
//
// binding.MustNew(
// binding.WithConverter[uuid.UUID](uuid.Parse),
// binding.WithConverter[decimal.Decimal](decimal.NewFromString),
// )
//
// # Common Converter Patterns
//
// The package provides factory functions for common converter patterns:
//
// ## Custom Time Layouts
//
// Use [TimeConverter] to parse times in non-standard formats:
//
// binder := binding.MustNew(
// binding.WithConverter(binding.TimeConverter(
// "01/02/2006", // US format
// "02/01/2006", // European format
// "2006-01-02 15:04", // Custom datetime
// )),
// )
//
// ## Duration Aliases
//
// Use [DurationConverter] to provide user-friendly duration names:
//
// binder := binding.MustNew(
// binding.WithConverter(binding.DurationConverter(map[string]time.Duration{
// "fast": 100 * time.Millisecond,
// "normal": 1 * time.Second,
// "slow": 5 * time.Second,
// "default": 30 * time.Second,
// })),
// )
//
// // Query: ?timeout=fast -> 100ms
// // Query: ?timeout=1h30m -> 1h30m (standard duration strings still work)
//
// ## Enum Validation
//
// Use [EnumConverter] to validate against allowed values:
//
// type Status string
// const (
// StatusActive Status = "active"
// StatusPending Status = "pending"
// StatusDisabled Status = "disabled"
// )
//
// binder := binding.MustNew(
// binding.WithConverter(binding.EnumConverter(
// StatusActive, StatusPending, StatusDisabled,
// )),
// )
//
// // Query: ?status=ACTIVE -> StatusActive (case-insensitive)
// // Query: ?status=unknown -> error
//
// ## Custom Boolean Values
//
// Use [BoolConverter] to accept non-standard boolean representations:
//
// binder := binding.MustNew(
// binding.WithConverter(binding.BoolConverter(
// []string{"enabled", "active", "on"}, // truthy values
// []string{"disabled", "inactive", "off"}, // falsy values
// )),
// )
//
// // Query: ?feature=enabled -> true
// // Query: ?feature=off -> false
//
// ## Combining Multiple Converters
//
// You can use multiple converter factories together:
//
// binder := binding.MustNew(
// binding.WithConverter(binding.TimeConverter("01/02/2006")),
// binding.WithConverter(binding.DurationConverter(map[string]time.Duration{
// "fast": 100 * time.Millisecond,
// "slow": 5 * time.Second,
// })),
// binding.WithConverter(binding.EnumConverter(StatusActive, StatusInactive)),
// binding.WithConverter(binding.BoolConverter(
// []string{"yes", "on"},
// []string{"no", "off"},
// )),
// )
//
// ## Third-Party Type Examples
//
// For types from third-party packages, use [WithConverter]:
//
// import (
// "github.com/google/uuid"
// "github.com/shopspring/decimal"
// )
//
// binder := binding.MustNew(
// binding.WithConverter[uuid.UUID](uuid.Parse),
// binding.WithConverter[decimal.Decimal](decimal.NewFromString),
// )
//
// Or with a custom wrapper for error handling:
//
// binder := binding.MustNew(
// binding.WithConverter[MyCustomType](func(s string) (MyCustomType, error) {
// // Custom parsing logic
// return parseMyCustomType(s)
// }),
// )
//
// # Error Handling
//
// Errors provide detailed context:
//
// user, err := binding.JSON[User](body)
// if err != nil {
// var bindErr *binding.BindError
// if errors.As(err, &bindErr) {
// fmt.Printf("Field: %s, Source: %s, Value: %s\n",
// bindErr.Field, bindErr.Source, bindErr.Value)
// }
// }
//
// Collect all errors instead of failing on first:
//
// user, err := binding.JSON[User](body, binding.WithAllErrors())
// if err != nil {
// var multi *binding.MultiError
// if errors.As(err, &multi) {
// for _, e := range multi.Errors {
// // Handle each error
// }
// }
// }
//
// # Observability
//
// Capture binding metrics with [WithResult]:
//
// var result binding.Result
// user, err := binding.JSON[User](body, binding.WithResult(&result))
// // result.FieldsBound, result.Errors, result.Duration, result.Unknown
//
// # Security Limits
//
// Built-in limits prevent resource exhaustion:
//
// - MaxDepth: Maximum struct nesting depth (default: 32)
// - MaxSliceLen: Maximum slice elements (default: 10,000)
// - MaxMapSize: Maximum map entries (default: 1,000)
//
// Configure limits:
//
// binding.MustNew(
// binding.WithMaxDepth(16),
// binding.WithMaxSliceLen(1000),
// binding.WithMaxMapSize(500),
// )
//
// # Configuration Options
//
// The package provides extensive configuration through functional options:
//
// ## Security Limits
//
// WithMaxDepth(n int) // Max struct nesting depth (default: 32)
// WithMaxSliceLen(n int) // Max slice elements (default: 10,000)
// WithMaxMapSize(n int) // Max map entries (default: 1,000)
//
// ## Unknown Fields
//
// WithStrictJSON() // Convenience: fail on unknown fields
// WithUnknownFields(policy UnknownFieldPolicy)
// - UnknownIgnore: Ignore unknown fields (default)
// - UnknownWarn: Collect unknown paths into Result.Unknown
// - UnknownError: Return error on unknown fields (same as WithStrictJSON)
//
// ## Slice Parsing
//
// WithSliceMode(mode SliceParseMode)
// - SliceRepeat: Parse "tags=go&tags=rust" (default)
// - SliceCSV: Parse "tags=go,rust,python"
//
// ## Time Formats
//
// WithTimeLayouts(layouts ...string) // Custom time.Time parsing formats
// // Default layouts exported as DefaultTimeLayouts: RFC3339, RFC3339Nano, DateOnly, DateTime
// // Extend defaults: WithTimeLayouts(append(DefaultTimeLayouts, "01/02/2006")...)
//
// ## Type Converters
//
// WithConverter[T any](converter TypeConverter[T])
// // Register custom type conversion function
//
// ## Error Handling
//
// WithAllErrors() // Collect all errors instead of failing on first
//
// ## Observability
//
// WithResult(r *Result) // Capture binding metrics
// - FieldsBound: Number of fields successfully bound
// - Errors: Number of errors encountered
// - Duration: Wall-clock time of the binding call
// - Unknown: Unknown field paths (with UnknownWarn/UnknownError)
//
// ## Key Normalization
//
// WithKeyNormalizer(normalizer KeyNormalizer)
// // Custom key transformation for lookups
//
// # Advanced Tag Syntax
//
// ## Tag Aliases
//
// Provide multiple lookup names for a field:
//
// type Request struct {
// UserID int `query:"user_id,id"` // Looks for "user_id" or "id"
// }
//
// ## Nested Structs
//
// Use dot notation for nested fields:
//
// type Address struct {
// Street string `query:"street"`
// City string `query:"city"`
// }
//
// type User struct {
// Address Address `query:"address"`
// }
//
// // Query: ?address.street=123+Main&address.city=Boston
//
// ## Bracket Notation
//
// Arrays can use bracket notation:
//
// type Request struct {
// Tags []string `query:"tags"`
// }
//
// // Both work: ?tags=go&tags=rust or ?tags[]=go&tags[]=rust
//
// # Streaming with io.Reader
//
// For large payloads, use Reader variants to avoid loading entire body into memory:
//
// // JSON from reader
// user, err := binding.JSONReader[User](r.Body)
//
// // XML from reader
// doc, err := binding.XMLReader[Document](r.Body)
//
// Reader variants are available for all body-based sources (JSON, XML, and sub-packages).
//
// # Performance Characteristics
//
// ## Caching
//
// Struct reflection information is cached automatically:
//
// - First binding of a type: ~500ns overhead for reflection
// - Subsequent bindings: ~50ns overhead (cache lookup)
// - Cache is thread-safe and has no size limit
// - Cache key includes both struct type and tag name
//
// ## Memory Allocation
//
// - Query/Path/Form/Header/Cookie: Zero allocations for primitive types
// - JSON/XML: Allocations depend on encoding/json and encoding/xml
// - Nested structs: One allocation per nesting level
// - Slices/Maps: Pre-allocated with capacity hints when possible
//
// ## Multi-Source Binding Precedence
//
// When using [Bind] with multiple sources, later sources override earlier ones:
//
// req, err := binding.Bind[Request](
// binding.FromPath(pathParams), // Applied first
// binding.FromQuery(r.URL.Query()), // Overrides path params
// binding.FromJSON(body), // Overrides query params
// )
//
// This allows for flexible request handling where body data takes precedence
// over URL parameters.
//
// # Custom ValueGetter
//
// For simple map-based sources, use the convenience helpers:
//
// // Single-value map
// getter := binding.MapGetter(map[string]string{"name": "Alice", "age": "30"})
// result, err := binding.RawInto[User](getter, "custom")
//
// // Multi-value map (for slices)
// getter := binding.MultiMapGetter(map[string][]string{"tags": {"go", "rust"}})
// result, err := binding.RawInto[User](getter, "custom")
//
// For more complex sources, implement the [ValueGetter] interface:
//
// type CustomGetter struct {
// data map[string]string
// }
//
// func (g *CustomGetter) Get(key string) string {
// return g.data[key]
// }
//
// func (g *CustomGetter) GetAll(key string) []string {
// if val, ok := g.data[key]; ok {
// return []string{val}
// }
// return nil
// }
//
// func (g *CustomGetter) Has(key string) bool {
// _, ok := g.data[key]
// return ok
// }
//
// // Use with RawInto
// result, err := binding.RawInto[MyStruct](&CustomGetter{data: myData}, "custom")
//
// Alternatively, use [GetterFunc] for a function-based adapter:
//
// getter := binding.GetterFunc(func(key string) ([]string, bool) {
// if val, ok := myMap[key]; ok {
// return []string{val}, true
// }
// return nil, false
// })
//
// # Integration Examples
//
// ## With net/http
//
// func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
// body, _ := io.ReadAll(r.Body)
// defer r.Body.Close()
//
// user, err := binding.JSON[CreateUserRequest](body)
// if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// return
// }
// // Process user...
// }
//
// ## With rivaas.dev/router
//
// func CreateUserHandler(c *router.Context) {
// user, err := binding.JSON[CreateUserRequest](c.Body())
// if err != nil {
// c.JSON(http.StatusBadRequest, map[string]any{"error": err.Error()})
// return
// }
// c.JSON(http.StatusCreated, user)
// }
//
// ## With rivaas.dev/app
//
// func CreateUserHandler(c *app.Context) {
// var user CreateUserRequest
// if err := c.Bind(&user); err != nil {
// return // Error automatically handled
// }
// c.JSON(http.StatusCreated, user)
// }
//
// // Multipart file upload with app.Context
// func UploadHandler(c *app.Context) {
// type Request struct {
// File *binding.File `form:"file"`
// Title string `form:"title"`
// }
//
// var req Request
// if err := c.Bind(&req); err != nil {
// return // Error automatically handled
// }
//
// req.File.Save("./uploads/" + req.File.Name)
// c.JSON(http.StatusOK, map[string]string{"uploaded": req.File.Name})
// }
//
// # Best Practices
//
// - Use generic API ([JSON], [Query], etc.) for compile-time type safety
// - Create reusable [Binder] instances for shared configuration
// - Set security limits ([WithMaxDepth], [WithMaxSliceLen], [WithMaxMapSize])
// - Use Reader variants for large payloads (>1MB)
// - Use rivaas.dev/validation package for validation (required, enum, etc.)
// - Capture binding metrics with [WithResult] for observability
// - Collect all errors with [WithAllErrors] for better UX
//
// # Error Types
//
// The package provides detailed error types for different failure scenarios:
//
// - [BindError]: Field-level binding errors with context
// - [UnknownFieldError]: Unknown fields in strict JSON mode
// - [MultiError]: Multiple errors collected with [WithAllErrors]
//
// All error types implement standard error interfaces and integrate with
// rivaas.dev/errors for HTTP status code mapping.
//
// # Standalone Usage
//
// This package works independently without the full Rivaas framework. Use it
// with any Go HTTP handler (net/http, Gin, Echo, etc.).
package binding