Skip to content

Commit

Permalink
🔇 silent changes: add base functions #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Dec 7, 2024
1 parent 088e25a commit 2aa305b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
31 changes: 30 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (w *wrapper) WithDebuggingKV(key string, value interface{}) *wrapper {
// - A `map[string]interface{}` containing the structured response data.
func (w *wrapper) Respond() map[string]interface{} {
m := make(map[string]interface{})
if w == nil {
if !w.Available() {
return m
}
if w.IsBodyPresent() {
Expand Down Expand Up @@ -428,6 +428,32 @@ func (p *pagination) Respond() map[string]interface{} {
return m
}

// Available checks whether the `wrapper` instance is non-nil.
//
// This function ensures that the `wrapper` object exists and is not nil.
// It serves as a safety check to avoid null pointer dereferences when accessing the instance's fields or methods.
//
// Returns:
// - A boolean value indicating whether the `wrapper` instance is non-nil:
// - `true` if the `wrapper` instance is non-nil.
// - `false` if the `wrapper` instance is nil.
func (w *wrapper) Available() bool {
return w != nil
}

// Available checks whether the `pagination` instance is non-nil.
//
// This function ensures that the `pagination` object exists and is not nil.
// It serves as a safety check to avoid null pointer dereferences when accessing the instance's fields or methods.
//
// Returns:
// - A boolean value indicating whether the `pagination` instance is non-nil:
// - `true` if the `pagination` instance is non-nil.
// - `false` if the `pagination` instance is nil.
func (p *pagination) Available() bool {
return p != nil
}

// IsDebuggingPresent checks whether debugging information is present in the `wrapper` instance.
//
// This function verifies if the `debug` field of the `wrapper` is not nil and contains at least one entry.
Expand Down Expand Up @@ -615,6 +641,9 @@ func (w *wrapper) IsServerError() bool {
// - `true` if `isLast` is `true`, indicating this is the last page of results.
// - `false` if `isLast` is `false`, indicating more pages are available.
func (p *pagination) IsLast() bool {
if !p.Available() {
return true
}
return p.isLast
}

Expand Down
99 changes: 97 additions & 2 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ package wrapify
//
// Returns:
// - An error object, or `nil` if no errors are present.
func (w *wrapper) Error() error {
return w.errors
func (w *wrapper) Error() string {
if !w.Available() {
return ""
}
return w.errors.Error()
}

// StateCode retrieves the HTTP status code associated with the `wrapper` instance.
Expand All @@ -19,6 +22,9 @@ func (w *wrapper) Error() error {
// Returns:
// - An integer representing the HTTP status code.
func (w *wrapper) StateCode() int {
if !w.Available() {
return 0
}
return w.statusCode
}

Expand All @@ -30,6 +36,9 @@ func (w *wrapper) StateCode() int {
// Returns:
// - A string representing the message.
func (w *wrapper) Message() string {
if !w.Available() {
return ""
}
return w.message
}

Expand All @@ -41,6 +50,9 @@ func (w *wrapper) Message() string {
// Returns:
// - An integer representing the total number of items.
func (w *wrapper) Total() int {
if !w.Available() {
return 0
}
return w.total
}

Expand All @@ -52,5 +64,88 @@ func (w *wrapper) Total() int {
// Returns:
// - The body data (of any type), or `nil` if no body data is present.
func (w *wrapper) Body() interface{} {
if !w.Available() {
return nil
}
return w.data
}

// Debugging retrieves the debugging information from the `wrapper` instance.
//
// This function checks if the `wrapper` instance is available (non-nil) before returning
// the value of the `debug` field. If the `wrapper` is not available, it returns an
// empty map to ensure safe usage.
//
// Returns:
// - A `map[string]interface{}` containing the debugging information.
// - An empty map if the `wrapper` instance is not available.
func (w *wrapper) Debugging() map[string]interface{} {
if !w.Available() {
return map[string]interface{}{}
}
return w.debug
}

// Page retrieves the current page number from the `pagination` instance.
//
// This function checks if the `pagination` instance is available (non-nil) before
// returning the value of the `page` field. If the instance is not available, it
// returns a default value of `0`.
//
// Returns:
// - An integer representing the current page number.
// - `0` if the `pagination` instance is not available.
func (p *pagination) Page() int {
if !p.Available() {
return 0
}
return p.page
}

// PerPage retrieves the number of items per page from the `pagination` instance.
//
// This function checks if the `pagination` instance is available (non-nil) before
// returning the value of the `perPage` field. If the instance is not available, it
// returns a default value of `0`.
//
// Returns:
// - An integer representing the number of items per page.
// - `0` if the `pagination` instance is not available.
func (p *pagination) PerPage() int {
if !p.Available() {
return 0
}
return p.perPage
}

// TotalPages retrieves the total number of pages from the `pagination` instance.
//
// This function checks if the `pagination` instance is available (non-nil) before
// returning the value of the `totalPages` field. If the instance is not available, it
// returns a default value of `0`.
//
// Returns:
// - An integer representing the total number of pages.
// - `0` if the `pagination` instance is not available.
func (p *pagination) TotalPages() int {
if !p.Available() {
return 0
}
return p.totalPages
}

// TotalItems retrieves the total number of items from the `pagination` instance.
//
// This function checks if the `pagination` instance is available (non-nil) before
// returning the value of the `totalItems` field. If the instance is not available, it
// returns a default value of `0`.
//
// Returns:
// - An integer representing the total number of items.
// - `0` if the `pagination` instance is not available.
func (p *pagination) TotalItems() int {
if !p.Available() {
return 0
}
return p.totalItems
}

0 comments on commit 2aa305b

Please sign in to comment.