diff --git a/pkg/e621/utils/constants.go b/pkg/e621/utils/constants.go index d7df2e8..ecfebcc 100644 --- a/pkg/e621/utils/constants.go +++ b/pkg/e621/utils/constants.go @@ -1,4 +1,4 @@ package utils -// E621_MAX_POST_COUNT is the maximum allowable post count for E621. +// E621_MAX_POST_COUNT is the maximum allowable post count for e621. const E621_MAX_POST_COUNT = 320 diff --git a/pkg/e621/utils/error.go b/pkg/e621/utils/error.go index d12b897..e8b5e4f 100644 --- a/pkg/e621/utils/error.go +++ b/pkg/e621/utils/error.go @@ -3,6 +3,12 @@ package utils import "fmt" // StatusCodesToError maps HTTP status codes to corresponding error types. +// +// Parameters: +// - statusCode: The HTTP status code to be mapped to an error type. +// +// Returns: +// - error: An error representing the mapped HTTP status code. func StatusCodesToError(statusCode int) error { var err error switch statusCode { @@ -31,6 +37,7 @@ func StatusCodesToError(statusCode int) error { // AccessDeniedError represents an "Access Denied" error. type AccessDeniedError struct{} +// Error returns the error message for AccessDeniedError. func (_ AccessDeniedError) Error() string { return "access denied" } @@ -38,6 +45,7 @@ func (_ AccessDeniedError) Error() string { // NotFoundError represents a "Not Found" error. type NotFoundError struct{} +// Error returns the error message for NotFoundError. func (_ NotFoundError) Error() string { return "not found" } @@ -45,6 +53,7 @@ func (_ NotFoundError) Error() string { // PreconditionFailedError represents a "Precondition Failed" error. type PreconditionFailedError struct{} +// Error returns the error message for PreconditionFailedError. func (_ PreconditionFailedError) Error() string { return "precondition failed" } @@ -52,6 +61,7 @@ func (_ PreconditionFailedError) Error() string { // RateLimitReachedError represents a "Rate Limit Reached" error. type RateLimitReachedError struct{} +// Error returns the error message for RateLimitReachedError. func (_ RateLimitReachedError) Error() string { return "rate limit reached" } @@ -59,6 +69,7 @@ func (_ RateLimitReachedError) Error() string { // InvalidParametersError represents an "Invalid Parameters" error. type InvalidParametersError struct{} +// Error returns the error message for InvalidParametersError. func (_ InvalidParametersError) Error() string { return "invalid parameters" } @@ -66,6 +77,7 @@ func (_ InvalidParametersError) Error() string { // InternalServerError represents an "Internal Server Error" error. type InternalServerError struct{} +// Error returns the error message for InternalServerError. func (_ InternalServerError) Error() string { return "internal server error" } @@ -73,6 +85,7 @@ func (_ InternalServerError) Error() string { // BadGatewayError represents a "Bad Gateway" error. type BadGatewayError struct{} +// Error returns the error message for BadGatewayError. func (_ BadGatewayError) Error() string { return "bad gateway" } @@ -80,6 +93,7 @@ func (_ BadGatewayError) Error() string { // ServiceUnavailableError represents a "Service Unavailable" error. type ServiceUnavailableError struct{} +// Error returns the error message for ServiceUnavailableError. func (_ ServiceUnavailableError) Error() string { return "service unavailable" } @@ -87,6 +101,7 @@ func (_ ServiceUnavailableError) Error() string { // UnknownError represents an "Unknown" error. type UnknownError struct{} +// Error returns the error message for UnknownError. func (_ UnknownError) Error() string { return "unknown error" } @@ -94,6 +109,7 @@ func (_ UnknownError) Error() string { // OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error. type OriginConnectionTimeOutError struct{} +// Error returns the error message for OriginConnectionTimeOutError. func (_ OriginConnectionTimeOutError) Error() string { return "origin connection time-out" } @@ -101,6 +117,7 @@ func (_ OriginConnectionTimeOutError) Error() string { // SSLHandshakeFailedError represents an "SSL Handshake Failed" error. type SSLHandshakeFailedError struct{} +// Error returns the error message for SSLHandshakeFailedError. func (_ SSLHandshakeFailedError) Error() string { return "ssl handshake failed" } diff --git a/pkg/e621/utils/helper.go b/pkg/e621/utils/helper.go index 802fc93..638eb5b 100644 --- a/pkg/e621/utils/helper.go +++ b/pkg/e621/utils/helper.go @@ -1,5 +1,18 @@ package utils +import ( + "encoding/json" + "os" +) + +// SliceFilter filters elements of a generic type from a slice based on a provided filter function. +// +// Parameters: +// - slice: The input slice containing elements of type T. +// - filter: A function that takes an element of type T and returns a boolean indicating whether to include the element in the result. +// +// Returns: +// - ret: A slice containing elements that satisfy the filtering condition. func SliceFilter[T any](slice []T, filter func(T) bool) (ret []T) { for _, s := range slice { if filter(s) { @@ -8,3 +21,33 @@ func SliceFilter[T any](slice []T, filter func(T) bool) (ret []T) { } return } + +// LoadJsonTestData decodes JSON data from a file and populates a variable of a generic type T. +// +// Parameters: +// - testDataPath: The file path to the JSON data. +// +// Returns: +// - T: A variable of type T populated with the decoded JSON data. +// - error: An error, if any, encountered during file opening, decoding, or closing. +func LoadJsonTestData[T any](testDataPath string) (T, error) { + // Create a variable to store the decoded JSON data + var jsonData T + + // Open the JSON file + file, err := os.Open(testDataPath) + if err != nil { + return jsonData, err + } + defer file.Close() + + // Create a decoder + decoder := json.NewDecoder(file) + + // Decode the JSON data into the struct + if err := decoder.Decode(&jsonData); err != nil { + return jsonData, err + } + + return jsonData, nil +} diff --git a/pkg/e621/utils/test.go b/pkg/e621/utils/test.go deleted file mode 100644 index 39a9a71..0000000 --- a/pkg/e621/utils/test.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "encoding/json" - "os" -) - -func LoadJsonTestData[T any](testDataPath string) (T, error) { - // Create a variable to store the decoded JSON data - var jsonData T - - // Open the JSON file - file, err := os.Open(testDataPath) - if err != nil { - return jsonData, err - } - defer file.Close() - - // Create a decoder - decoder := json.NewDecoder(file) - - // Decode the JSON data into the struct - if err := decoder.Decode(&jsonData); err != nil { - return jsonData, err - } - - return jsonData, nil -}