56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package data
|
|
|
|
import (
|
|
"actatempus_backend/internal/domain/app_error"
|
|
"actatempus_backend/internal/infrastructure/data/db"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
|
|
E "github.com/IBM/fp-go/either"
|
|
)
|
|
|
|
func TryDereference[T any](ptr *T) E.Either[error, T] {
|
|
if ptr == nil {
|
|
return E.Left[T](error(app_error.NewInternalError(fmt.Errorf("Could not dereference"))))
|
|
}
|
|
return E.Right[error](*ptr)
|
|
|
|
}
|
|
|
|
func WrapAppError(err error) error {
|
|
return app_error.NewInternalError(err)
|
|
}
|
|
|
|
func WrapDBError(notFoundMessage string) func(error) error {
|
|
return func(err error) error {
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
return app_error.NewNotFoundError(notFoundMessage)
|
|
}
|
|
return app_error.NewInternalError(err)
|
|
|
|
}
|
|
}
|
|
|
|
func NullableField[T any](getter func() (T, bool)) *T {
|
|
if value, ok := getter(); ok {
|
|
return &value
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GenerateSecureHash generates a SHA-256 hash for the given input string.
|
|
func GenerateSecureHash(input string) string {
|
|
// Convert the input string to bytes
|
|
bytes := []byte(input)
|
|
|
|
// Compute the SHA-256 hash
|
|
hash := sha256.Sum256(bytes)
|
|
|
|
// Convert the hash to a hexadecimal string
|
|
hashHex := hex.EncodeToString(hash[:])
|
|
|
|
return hashHex
|
|
}
|