docs: added more & unified documentation format

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-11-15 14:41:16 +01:00
parent 9cee99ecb5
commit 8c662f78fd
4 changed files with 61 additions and 29 deletions

View File

@ -1,4 +1,4 @@
package utils 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 const E621_MAX_POST_COUNT = 320

View File

@ -3,6 +3,12 @@ package utils
import "fmt" import "fmt"
// StatusCodesToError maps HTTP status codes to corresponding error types. // 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 { func StatusCodesToError(statusCode int) error {
var err error var err error
switch statusCode { switch statusCode {
@ -31,6 +37,7 @@ func StatusCodesToError(statusCode int) error {
// AccessDeniedError represents an "Access Denied" error. // AccessDeniedError represents an "Access Denied" error.
type AccessDeniedError struct{} type AccessDeniedError struct{}
// Error returns the error message for AccessDeniedError.
func (_ AccessDeniedError) Error() string { func (_ AccessDeniedError) Error() string {
return "access denied" return "access denied"
} }
@ -38,6 +45,7 @@ func (_ AccessDeniedError) Error() string {
// NotFoundError represents a "Not Found" error. // NotFoundError represents a "Not Found" error.
type NotFoundError struct{} type NotFoundError struct{}
// Error returns the error message for NotFoundError.
func (_ NotFoundError) Error() string { func (_ NotFoundError) Error() string {
return "not found" return "not found"
} }
@ -45,6 +53,7 @@ func (_ NotFoundError) Error() string {
// PreconditionFailedError represents a "Precondition Failed" error. // PreconditionFailedError represents a "Precondition Failed" error.
type PreconditionFailedError struct{} type PreconditionFailedError struct{}
// Error returns the error message for PreconditionFailedError.
func (_ PreconditionFailedError) Error() string { func (_ PreconditionFailedError) Error() string {
return "precondition failed" return "precondition failed"
} }
@ -52,6 +61,7 @@ func (_ PreconditionFailedError) Error() string {
// RateLimitReachedError represents a "Rate Limit Reached" error. // RateLimitReachedError represents a "Rate Limit Reached" error.
type RateLimitReachedError struct{} type RateLimitReachedError struct{}
// Error returns the error message for RateLimitReachedError.
func (_ RateLimitReachedError) Error() string { func (_ RateLimitReachedError) Error() string {
return "rate limit reached" return "rate limit reached"
} }
@ -59,6 +69,7 @@ func (_ RateLimitReachedError) Error() string {
// InvalidParametersError represents an "Invalid Parameters" error. // InvalidParametersError represents an "Invalid Parameters" error.
type InvalidParametersError struct{} type InvalidParametersError struct{}
// Error returns the error message for InvalidParametersError.
func (_ InvalidParametersError) Error() string { func (_ InvalidParametersError) Error() string {
return "invalid parameters" return "invalid parameters"
} }
@ -66,6 +77,7 @@ func (_ InvalidParametersError) Error() string {
// InternalServerError represents an "Internal Server Error" error. // InternalServerError represents an "Internal Server Error" error.
type InternalServerError struct{} type InternalServerError struct{}
// Error returns the error message for InternalServerError.
func (_ InternalServerError) Error() string { func (_ InternalServerError) Error() string {
return "internal server error" return "internal server error"
} }
@ -73,6 +85,7 @@ func (_ InternalServerError) Error() string {
// BadGatewayError represents a "Bad Gateway" error. // BadGatewayError represents a "Bad Gateway" error.
type BadGatewayError struct{} type BadGatewayError struct{}
// Error returns the error message for BadGatewayError.
func (_ BadGatewayError) Error() string { func (_ BadGatewayError) Error() string {
return "bad gateway" return "bad gateway"
} }
@ -80,6 +93,7 @@ func (_ BadGatewayError) Error() string {
// ServiceUnavailableError represents a "Service Unavailable" error. // ServiceUnavailableError represents a "Service Unavailable" error.
type ServiceUnavailableError struct{} type ServiceUnavailableError struct{}
// Error returns the error message for ServiceUnavailableError.
func (_ ServiceUnavailableError) Error() string { func (_ ServiceUnavailableError) Error() string {
return "service unavailable" return "service unavailable"
} }
@ -87,6 +101,7 @@ func (_ ServiceUnavailableError) Error() string {
// UnknownError represents an "Unknown" error. // UnknownError represents an "Unknown" error.
type UnknownError struct{} type UnknownError struct{}
// Error returns the error message for UnknownError.
func (_ UnknownError) Error() string { func (_ UnknownError) Error() string {
return "unknown error" return "unknown error"
} }
@ -94,6 +109,7 @@ func (_ UnknownError) Error() string {
// OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error. // OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error.
type OriginConnectionTimeOutError struct{} type OriginConnectionTimeOutError struct{}
// Error returns the error message for OriginConnectionTimeOutError.
func (_ OriginConnectionTimeOutError) Error() string { func (_ OriginConnectionTimeOutError) Error() string {
return "origin connection time-out" return "origin connection time-out"
} }
@ -101,6 +117,7 @@ func (_ OriginConnectionTimeOutError) Error() string {
// SSLHandshakeFailedError represents an "SSL Handshake Failed" error. // SSLHandshakeFailedError represents an "SSL Handshake Failed" error.
type SSLHandshakeFailedError struct{} type SSLHandshakeFailedError struct{}
// Error returns the error message for SSLHandshakeFailedError.
func (_ SSLHandshakeFailedError) Error() string { func (_ SSLHandshakeFailedError) Error() string {
return "ssl handshake failed" return "ssl handshake failed"
} }

View File

@ -1,5 +1,18 @@
package utils 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) { func SliceFilter[T any](slice []T, filter func(T) bool) (ret []T) {
for _, s := range slice { for _, s := range slice {
if filter(s) { if filter(s) {
@ -8,3 +21,33 @@ func SliceFilter[T any](slice []T, filter func(T) bool) (ret []T) {
} }
return 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
}

View File

@ -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
}