|
| 1 | +package replify |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | +) |
| 6 | + |
| 7 | +// NormPagination normalizes the pagination information in the wrapper. |
| 8 | +// |
| 9 | +// If the pagination object is not already initialized, it creates a new one |
| 10 | +// using the `NewPagination` function. It then calls the `Normalize` method |
| 11 | +// on the pagination instance to ensure its values are consistent. |
| 12 | +// |
| 13 | +// Returns: |
| 14 | +// - A pointer to the updated `wrapper` instance. |
| 15 | +func (w *wrapper) NormPagination() *wrapper { |
| 16 | + if !w.IsPagingPresent() { |
| 17 | + w.pagination = Pages() |
| 18 | + } else { |
| 19 | + w.pagination.Normalize() |
| 20 | + w.RandRequestID() // Indicate that a change has occurred |
| 21 | + } |
| 22 | + return w |
| 23 | +} |
| 24 | + |
| 25 | +// NormHSC normalizes the relationship between the header and status code. |
| 26 | +// |
| 27 | +// If the status code is not present but the header is, it sets the status code |
| 28 | +// from the header's code. If the header is not present but the status code is, |
| 29 | +// it creates a new header with the status code and its corresponding text. |
| 30 | +// |
| 31 | +// If both the status code and header are present, it ensures the status code |
| 32 | +// matches the header's code. |
| 33 | +// |
| 34 | +// Returns: |
| 35 | +// - A pointer to the updated `wrapper` instance. |
| 36 | +func (w *wrapper) NormHSC() *wrapper { |
| 37 | + hasChanges := false |
| 38 | + switch { |
| 39 | + case !w.IsStatusCodePresent() && w.IsHeaderPresent(): |
| 40 | + w.statusCode = w.header.Code() |
| 41 | + hasChanges = true |
| 42 | + case !w.IsHeaderPresent() && w.IsStatusCodePresent(): |
| 43 | + w.header = Header().WithCode(w.statusCode).WithText(http.StatusText(w.statusCode)) |
| 44 | + hasChanges = true |
| 45 | + case w.IsStatusCodePresent() && w.IsHeaderPresent(): |
| 46 | + if w.statusCode == w.header.Code() { |
| 47 | + break |
| 48 | + } |
| 49 | + w.statusCode = w.header.Code() |
| 50 | + hasChanges = true |
| 51 | + } |
| 52 | + |
| 53 | + if hasChanges { |
| 54 | + w.RandRequestID() // Indicate that a change has occurred |
| 55 | + } |
| 56 | + return w |
| 57 | +} |
0 commit comments