diff --git a/init.go b/init.go index 31ad349..eb5b550 100644 --- a/init.go +++ b/init.go @@ -427,236 +427,3 @@ func (p *pagination) Respond() map[string]interface{} { m["is_last"] = p.isLast 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. -// It returns `true` if debugging information is available; otherwise, it returns `false`. -// -// Returns: -// - A boolean value indicating whether debugging information is present: -// - `true` if `debug` is not nil and contains data. -// - `false` if `debug` is nil or empty. -func (w *wrapper) IsDebuggingPresent() bool { - return len(w.debug) > 0 -} - -// IsDebuggingKeyPresent checks whether a specific key exists in the `debug` information. -// -// This function first checks if debugging information is present using `IsDebuggingPresent()`. -// Then it uses `unify4g.MapContainsKey` to verify if the given key is present within the `debug` map. -// -// Parameters: -// - `key`: The key to search for within the `debug` field. -// -// Returns: -// - A boolean value indicating whether the specified key is present in the `debug` map: -// - `true` if the `debug` field is present and contains the specified key. -// - `false` if `debug` is nil or does not contain the key. -func (w *wrapper) IsDebuggingKeyPresent(key string) bool { - return w.IsDebuggingPresent() && unify4g.MapContainsKey(w.debug, key) -} - -// IsBodyPresent checks whether the body data is present in the `wrapper` instance. -// -// This function checks if the `data` field of the `wrapper` is not nil, indicating that the body contains data. -// -// Returns: -// - A boolean value indicating whether the body data is present: -// - `true` if `data` is not nil. -// - `false` if `data` is nil. -func (w *wrapper) IsBodyPresent() bool { - return w.data != nil -} - -// IsHeaderPresent checks whether header information is present in the `wrapper` instance. -// -// This function checks if the `header` field of the `wrapper` is not nil, indicating that header information is included. -// -// Returns: -// - A boolean value indicating whether header information is present: -// - `true` if `header` is not nil. -// - `false` if `header` is nil. -func (w *wrapper) IsHeaderPresent() bool { - return w.header != nil -} - -// IsMetaPresent checks whether metadata information is present in the `wrapper` instance. -// -// This function checks if the `meta` field of the `wrapper` is not nil, indicating that metadata is available. -// -// Returns: -// - A boolean value indicating whether metadata is present: -// - `true` if `meta` is not nil. -// - `false` if `meta` is nil. -func (w *wrapper) IsMetaPresent() bool { - return w.meta != nil -} - -// IsPagingPresent checks whether pagination information is present in the `wrapper` instance. -// -// This function checks if the `pagination` field of the `wrapper` is not nil, indicating that pagination details are included. -// -// Returns: -// - A boolean value indicating whether pagination information is present: -// - `true` if `pagination` is not nil. -// - `false` if `pagination` is nil. -func (w *wrapper) IsPagingPresent() bool { - return w.pagination != nil -} - -// IsErrorPresent checks whether an error is present in the `wrapper` instance. -// -// This function checks if the `errors` field of the `wrapper` is not nil, indicating that an error has occurred. -// -// Returns: -// - A boolean value indicating whether an error is present: -// - `true` if `errors` is not nil. -// - `false` if `errors` is nil. -func (w *wrapper) IsErrorPresent() bool { - return w.errors != nil -} - -// IsTotalPresent checks whether the total number of items is present in the `wrapper` instance. -// -// This function checks if the `total` field of the `wrapper` is greater than or equal to 0, -// indicating that a valid total number of items has been set. -// -// Returns: -// - A boolean value indicating whether the total is present: -// - `true` if `total` is greater than or equal to 0. -// - `false` if `total` is negative (indicating no total value). -func (w *wrapper) IsTotalPresent() bool { - return w.total >= 0 -} - -// IsStatusCodePresent checks whether a valid status code is present in the `wrapper` instance. -// -// This function checks if the `statusCode` field of the `wrapper` is greater than 0, -// indicating that a valid HTTP status code has been set. -// -// Returns: -// - A boolean value indicating whether the status code is present: -// - `true` if `statusCode` is greater than 0. -// - `false` if `statusCode` is less than or equal to 0. -func (w *wrapper) IsStatusCodePresent() bool { - return w.statusCode > 0 -} - -// IsError checks whether there is an error present in the `wrapper` instance. -// -// This function returns `true` if the `wrapper` contains an error, which can be any of the following: -// - An error present in the `errors` field. -// - A client error (4xx status code) or a server error (5xx status code). -// -// Returns: -// - A boolean value indicating whether there is an error: -// - `true` if there is an error present, either in the `errors` field or as an HTTP client/server error. -// - `false` if no error is found. -func (w *wrapper) IsError() bool { - return w.IsErrorPresent() || w.IsClientError() || w.IsServerError() -} - -// IsSuccess checks whether the HTTP status code indicates a successful response. -// -// This function checks if the `statusCode` is between 200 and 299, inclusive, which indicates a successful HTTP response. -// -// Returns: -// - A boolean value indicating whether the HTTP response was successful: -// - `true` if the status code is between 200 and 299 (inclusive). -// - `false` if the status code is outside of this range. -func (w *wrapper) IsSuccess() bool { - return (200 <= w.statusCode) && (w.statusCode <= 299) -} - -// IsRedirection checks whether the HTTP status code indicates a redirection response. -// -// This function checks if the `statusCode` is between 300 and 399, inclusive, which indicates a redirection HTTP response. -// -// Returns: -// - A boolean value indicating whether the HTTP response is a redirection: -// - `true` if the status code is between 300 and 399 (inclusive). -// - `false` if the status code is outside of this range. -func (w *wrapper) IsRedirection() bool { - return (300 <= w.statusCode) && (w.statusCode <= 399) -} - -// IsClientError checks whether the HTTP status code indicates a client error. -// -// This function checks if the `statusCode` is between 400 and 499, inclusive, which indicates a client error HTTP response. -// -// Returns: -// - A boolean value indicating whether the HTTP response is a client error: -// - `true` if the status code is between 400 and 499 (inclusive). -// - `false` if the status code is outside of this range. -func (w *wrapper) IsClientError() bool { - return (400 <= w.statusCode) && (w.statusCode <= 499) -} - -// IsServerError checks whether the HTTP status code indicates a server error. -// -// This function checks if the `statusCode` is between 500 and 599, inclusive, which indicates a server error HTTP response. -// -// Returns: -// - A boolean value indicating whether the HTTP response is a server error: -// - `true` if the status code is between 500 and 599 (inclusive). -// - `false` if the status code is outside of this range. -func (w *wrapper) IsServerError() bool { - return (500 <= w.statusCode) && (w.statusCode <= 599) -} - -// IsLast checks whether the current pagination represents the last page. -// -// This function checks the `isLast` field of the `pagination` instance to determine if the current page is the last one. -// The `isLast` field is typically set to `true` when there are no more pages of data available. -// -// Returns: -// - A boolean value indicating whether the current page is the last: -// - `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 -} - -// IsLastPage checks whether the current page is the last page of results. -// -// This function verifies that pagination information is present and then checks if the current page is the last page. -// It combines the checks of `IsPagingPresent()` and `IsLast()` to ensure that the pagination structure exists -// and that it represents the last page. -// -// Returns: -// - A boolean value indicating whether the current page is the last page: -// - `true` if pagination is present and the current page is the last one. -// - `false` if pagination is not present or the current page is not the last. -func (w *wrapper) IsLastPage() bool { - return w.IsPagingPresent() && w.pagination.IsLast() -} diff --git a/wrap.go b/wrap.go index ba85fbe..40d5752 100644 --- a/wrap.go +++ b/wrap.go @@ -1,5 +1,20 @@ package wrapify +import "github.com/sivaosorg/unify4g" + +// 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 +} + // Error retrieves the error associated with the `wrapper` instance. // // This function returns the `errors` field of the `wrapper`, which contains @@ -86,6 +101,210 @@ func (w *wrapper) Debugging() map[string]interface{} { return w.debug } +// 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. +// It returns `true` if debugging information is available; otherwise, it returns `false`. +// +// Returns: +// - A boolean value indicating whether debugging information is present: +// - `true` if `debug` is not nil and contains data. +// - `false` if `debug` is nil or empty. +func (w *wrapper) IsDebuggingPresent() bool { + return len(w.debug) > 0 +} + +// IsDebuggingKeyPresent checks whether a specific key exists in the `debug` information. +// +// This function first checks if debugging information is present using `IsDebuggingPresent()`. +// Then it uses `unify4g.MapContainsKey` to verify if the given key is present within the `debug` map. +// +// Parameters: +// - `key`: The key to search for within the `debug` field. +// +// Returns: +// - A boolean value indicating whether the specified key is present in the `debug` map: +// - `true` if the `debug` field is present and contains the specified key. +// - `false` if `debug` is nil or does not contain the key. +func (w *wrapper) IsDebuggingKeyPresent(key string) bool { + return w.IsDebuggingPresent() && unify4g.MapContainsKey(w.debug, key) +} + +// IsBodyPresent checks whether the body data is present in the `wrapper` instance. +// +// This function checks if the `data` field of the `wrapper` is not nil, indicating that the body contains data. +// +// Returns: +// - A boolean value indicating whether the body data is present: +// - `true` if `data` is not nil. +// - `false` if `data` is nil. +func (w *wrapper) IsBodyPresent() bool { + return w.data != nil +} + +// IsHeaderPresent checks whether header information is present in the `wrapper` instance. +// +// This function checks if the `header` field of the `wrapper` is not nil, indicating that header information is included. +// +// Returns: +// - A boolean value indicating whether header information is present: +// - `true` if `header` is not nil. +// - `false` if `header` is nil. +func (w *wrapper) IsHeaderPresent() bool { + return w.header != nil +} + +// IsMetaPresent checks whether metadata information is present in the `wrapper` instance. +// +// This function checks if the `meta` field of the `wrapper` is not nil, indicating that metadata is available. +// +// Returns: +// - A boolean value indicating whether metadata is present: +// - `true` if `meta` is not nil. +// - `false` if `meta` is nil. +func (w *wrapper) IsMetaPresent() bool { + return w.meta != nil +} + +// IsPagingPresent checks whether pagination information is present in the `wrapper` instance. +// +// This function checks if the `pagination` field of the `wrapper` is not nil, indicating that pagination details are included. +// +// Returns: +// - A boolean value indicating whether pagination information is present: +// - `true` if `pagination` is not nil. +// - `false` if `pagination` is nil. +func (w *wrapper) IsPagingPresent() bool { + return w.pagination != nil +} + +// IsErrorPresent checks whether an error is present in the `wrapper` instance. +// +// This function checks if the `errors` field of the `wrapper` is not nil, indicating that an error has occurred. +// +// Returns: +// - A boolean value indicating whether an error is present: +// - `true` if `errors` is not nil. +// - `false` if `errors` is nil. +func (w *wrapper) IsErrorPresent() bool { + return w.errors != nil +} + +// IsTotalPresent checks whether the total number of items is present in the `wrapper` instance. +// +// This function checks if the `total` field of the `wrapper` is greater than or equal to 0, +// indicating that a valid total number of items has been set. +// +// Returns: +// - A boolean value indicating whether the total is present: +// - `true` if `total` is greater than or equal to 0. +// - `false` if `total` is negative (indicating no total value). +func (w *wrapper) IsTotalPresent() bool { + return w.total >= 0 +} + +// IsStatusCodePresent checks whether a valid status code is present in the `wrapper` instance. +// +// This function checks if the `statusCode` field of the `wrapper` is greater than 0, +// indicating that a valid HTTP status code has been set. +// +// Returns: +// - A boolean value indicating whether the status code is present: +// - `true` if `statusCode` is greater than 0. +// - `false` if `statusCode` is less than or equal to 0. +func (w *wrapper) IsStatusCodePresent() bool { + return w.statusCode > 0 +} + +// IsError checks whether there is an error present in the `wrapper` instance. +// +// This function returns `true` if the `wrapper` contains an error, which can be any of the following: +// - An error present in the `errors` field. +// - A client error (4xx status code) or a server error (5xx status code). +// +// Returns: +// - A boolean value indicating whether there is an error: +// - `true` if there is an error present, either in the `errors` field or as an HTTP client/server error. +// - `false` if no error is found. +func (w *wrapper) IsError() bool { + return w.IsErrorPresent() || w.IsClientError() || w.IsServerError() +} + +// IsSuccess checks whether the HTTP status code indicates a successful response. +// +// This function checks if the `statusCode` is between 200 and 299, inclusive, which indicates a successful HTTP response. +// +// Returns: +// - A boolean value indicating whether the HTTP response was successful: +// - `true` if the status code is between 200 and 299 (inclusive). +// - `false` if the status code is outside of this range. +func (w *wrapper) IsSuccess() bool { + return (200 <= w.statusCode) && (w.statusCode <= 299) +} + +// IsRedirection checks whether the HTTP status code indicates a redirection response. +// +// This function checks if the `statusCode` is between 300 and 399, inclusive, which indicates a redirection HTTP response. +// +// Returns: +// - A boolean value indicating whether the HTTP response is a redirection: +// - `true` if the status code is between 300 and 399 (inclusive). +// - `false` if the status code is outside of this range. +func (w *wrapper) IsRedirection() bool { + return (300 <= w.statusCode) && (w.statusCode <= 399) +} + +// IsClientError checks whether the HTTP status code indicates a client error. +// +// This function checks if the `statusCode` is between 400 and 499, inclusive, which indicates a client error HTTP response. +// +// Returns: +// - A boolean value indicating whether the HTTP response is a client error: +// - `true` if the status code is between 400 and 499 (inclusive). +// - `false` if the status code is outside of this range. +func (w *wrapper) IsClientError() bool { + return (400 <= w.statusCode) && (w.statusCode <= 499) +} + +// IsServerError checks whether the HTTP status code indicates a server error. +// +// This function checks if the `statusCode` is between 500 and 599, inclusive, which indicates a server error HTTP response. +// +// Returns: +// - A boolean value indicating whether the HTTP response is a server error: +// - `true` if the status code is between 500 and 599 (inclusive). +// - `false` if the status code is outside of this range. +func (w *wrapper) IsServerError() bool { + return (500 <= w.statusCode) && (w.statusCode <= 599) +} + +// IsLastPage checks whether the current page is the last page of results. +// +// This function verifies that pagination information is present and then checks if the current page is the last page. +// It combines the checks of `IsPagingPresent()` and `IsLast()` to ensure that the pagination structure exists +// and that it represents the last page. +// +// Returns: +// - A boolean value indicating whether the current page is the last page: +// - `true` if pagination is present and the current page is the last one. +// - `false` if pagination is not present or the current page is not the last. +func (w *wrapper) IsLastPage() bool { + return w.IsPagingPresent() && w.pagination.IsLast() +} + +// 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 +} + // Page retrieves the current page number from the `pagination` instance. // // This function checks if the `pagination` instance is available (non-nil) before @@ -149,3 +368,19 @@ func (p *pagination) TotalItems() int { } return p.totalItems } + +// IsLast checks whether the current pagination represents the last page. +// +// This function checks the `isLast` field of the `pagination` instance to determine if the current page is the last one. +// The `isLast` field is typically set to `true` when there are no more pages of data available. +// +// Returns: +// - A boolean value indicating whether the current page is the last: +// - `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 +}