Skip to content

Commit 5cd2845

Browse files
committed
♻️ refactor: update codebase #2
1 parent 1455916 commit 5cd2845

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

norm.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

replify.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,22 +1660,6 @@ func (w *wrapper) WithIsLast(v bool) *wrapper {
16601660
return w
16611661
}
16621662

1663-
// NormPagination normalizes the pagination information in the wrapper.
1664-
//
1665-
// If the pagination object is not already initialized, it creates a new one
1666-
// using the `NewPagination` function. It then calls the `Normalize` method
1667-
// on the pagination instance to ensure its values are consistent.
1668-
//
1669-
// Returns:
1670-
// - A pointer to the updated `wrapper` instance.
1671-
func (w *wrapper) NormPagination() *wrapper {
1672-
if !w.IsPagingPresent() {
1673-
w.pagination = Pages()
1674-
}
1675-
w.pagination.Normalize()
1676-
return w
1677-
}
1678-
16791663
// Hash256 generates a hash string for the `wrapper` instance.
16801664
//
16811665
// This method concatenates the values of the `statusCode`, `message`, `data`, and `meta` fields

0 commit comments

Comments
 (0)