From 2aa305b0507dc9ebb930f71406b259647bb01a78 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sat, 7 Dec 2024 20:25:34 +0700 Subject: [PATCH] :mute: silent changes: add base functions #4 --- init.go | 31 +++++++++++++++++- wrap.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 127 insertions(+), 3 deletions(-) diff --git a/init.go b/init.go index bfd0b68..31ad349 100644 --- a/init.go +++ b/init.go @@ -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() { @@ -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. @@ -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 } diff --git a/wrap.go b/wrap.go index cb503af..ba85fbe 100644 --- a/wrap.go +++ b/wrap.go @@ -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. @@ -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 } @@ -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 } @@ -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 } @@ -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 +}