util

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SLogger *zap.SugaredLogger
	Logger  *zap.Logger
)

Functions

func DeserializeJson

func DeserializeJson[T any](data []byte) (T, error)

DeserializeJson unmarshal the provided JSON data into a value of the specified generic type T.

Parameters:

  • data: A byte slice containing the JSON data to be deserialized.

Returns:

  • A value of type T, populated with the deserialized data.
  • An error if the deserialization process fails.

This function uses the `json.Unmarshal` function to convert the JSON data in the 'data' byte slice into a value of the specified generic type 'T'. If the unmarshalling process encounters an error, it logs the error using the `LogError` function and returns the error along with the zero value of type 'T'. If the unmarshalling is successful, it returns the deserialized value of type 'T' and a nil error.

func InitLogger

func InitLogger(isRelease bool, minimumLevel zapcore.Level) func()

InitLogger initializes a global logger based on the provided configuration.

Parameters:

  • isRelease: If true, it sets up a production-ready JSON logger formatted for Cloud Run. If false, it uses a development-friendly console logger with color-coded levels.
  • minimumLevel: Sets the minimum log level to be captured. Logs below this level will be ignored.

Returns:

  • A function that can be called to flush any remaining log messages before the program terminates.

This function sets up the global `Logger` and `SLogger` variables, which can then be used throughout the application for logging. The logger's behavior and output format are determined by the `isRelease` flag. In release mode, logs are structured in JSON format, suitable for Cloud Run's logging infrastructure. In development mode, logs are output to the console with color-coded levels for easier readability.

func LoadConfiguration

func LoadConfiguration[T Configuration](configFilePath string, config T) error

LoadConfiguration loads and unmarshals configuration from a JSON file. It also performs validation on the configuration if the `Validate()` method is implemented.

Parameters:

  • configFilePath: The path to the JSON configuration file.
  • config: A pointer to the configuration struct to be populated.

Returns:

  • An error if there's an issue reading the file, unmarshaling the JSON, or validating the configuration.

func LoadEnvironmentVariables

func LoadEnvironmentVariables[T Configuration](config T) error

LoadEnvironmentVariables loads configuration values from environment variables into the provided configuration struct. It utilizes the `envconfig` package to process the environment variables and populate the configuration struct accordingly. Additionally, if the configuration struct implements the `Validate` method, it performs validation on the loaded configuration.

Parameters:

  • config: A pointer to the configuration struct to be populated with values from environment variables.

Returns:

  • An error if there's an issue processing environment variables or validating the configuration.

func Log

func Log(lvl zapcore.Level, template string, args ...interface{})

func LogError

func LogError(template string, args ...interface{})

func LogFatal added in v0.6.1

func LogFatal(template string, args ...interface{})

func LogInfo

func LogInfo(template string, args ...interface{})

func LogWarn

func LogWarn(template string, args ...interface{})

func SerializeJson

func SerializeJson(data interface{}) (string, error)

SerializeJson converts the provided data into a JSON-formatted string.

Parameters:

  • data: The data to be serialized. It must be of a type supported by the `json.Marshal` function. The `isSerializable` function is used to check this beforehand.

Returns:

  • A string containing the JSON representation of the data.
  • An error if the serialization process fails or if the data type is unsupported.

This function first checks if the provided data is serializable using the `isSerializable` helper function. If it is, it uses `json.Marshal` to convert the data into a byte slice representing the JSON format. Any errors during marshaling are logged and returned. Finally, the byte slice is converted into a string and returned along with a nil error if the process was successful.

Types

type Configuration

type Configuration interface {
	Validate() error
}

Configuration represents a type that can be loaded with configuration data and optionally validated.

Directories

Path Synopsis
jwt
jwe
jws

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL