Skip to content

Commit

Permalink
feat(bux-246): optimize structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii-4chain committed Apr 5, 2024
1 parent 60f49e1 commit 66ac26e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 76 deletions.
60 changes: 30 additions & 30 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,32 @@ const (

// AppConfig is the configuration values and associated env vars
type AppConfig struct {
// NewRelic is New Relic related settings.
NewRelic *NewRelicConfig `json:"new_relic" mapstructure:"new_relic"`
// TaskManager is a configuration for Task Manager in SPV Wallet.
TaskManager *TaskManagerConfig `json:"task_manager" mapstructure:"task_manager"`
// Authentication is the configuration for keys authentication in SPV Wallet.
Authentication *AuthenticationConfig `json:"auth" mapstructure:"auth"`
// Cache is the configuration for cache, memory or redis, and cluster cache settings.
Cache *CacheConfig `json:"cache" mapstructure:"cache"`
// Db is the configuration for database related settings.
Db *DbConfig `json:"db" mapstructure:"db"`
// Server is a general configuration for spv-wallet.
Server *ServerConfig `json:"server_config" mapstructure:"server_config"`
// TaskManager is a configuration for Task Manager in SPV Wallet.
TaskManager *TaskManagerConfig `json:"task_manager" mapstructure:"task_manager"`
// Nodes is a config for BSV nodes, mAPI and Arc.
Nodes *NodesConfig `json:"nodes" mapstructure:"nodes"`
// Metrics is a configuration for metrics in SPV Wallet.
Metrics *MetricsConfig `json:"metrics" mapstructure:"metrics"`
// ExperimentalFeatures is a configuration that allows to enable features that are considered experimental/non-production.
ExperimentalFeatures *ExperimentalConfig `json:"experimental_features" mapstructure:"experimental_features"`
// Logging is the configuration for zerolog used in SPV Wallet.
Logging *LoggingConfig `json:"logging" mapstructure:"logging"`
// NewRelic is New Relic related settings.
NewRelic *NewRelicConfig `json:"new_relic" mapstructure:"new_relic"`
// Nodes is a config for BSV nodes, mAPI and Arc.
Nodes *NodesConfig `json:"nodes" mapstructure:"nodes"`
// Notifications is a config for Notification service.
Notifications *NotificationsConfig `json:"notifications" mapstructure:"notifications"`
// Db is the configuration for database related settings.
Db *DbConfig `json:"db" mapstructure:"db"`
// Cache is the configuration for cache, memory or redis, and cluster cache settings.
Cache *CacheConfig `json:"cache" mapstructure:"cache"`
// Logging is the configuration for zerolog used in SPV Wallet.
Logging *LoggingConfig `json:"logging" mapstructure:"logging"`
// Paymail is a config for Paymail and BEEF.
Paymail *PaymailConfig `json:"paymail" mapstructure:"paymail"`
// ImportBlockHeaders is a URL from where the headers can be downloaded.
ImportBlockHeaders string `json:"import_block_headers" mapstructure:"import_block_headers"`
// Debug is a flag for enabling additional information from SPV Wallet.
Debug bool `json:"debug" mapstructure:"debug"`
// DebugProfiling is a flag for enabling additinal debug profiling.
Expand All @@ -57,20 +59,18 @@ type AppConfig struct {
DisableITC bool `json:"disable_itc" mapstructure:"disable_itc"`
// RequestLogging is flag for enabling logging in go-api-router.
RequestLogging bool `json:"request_logging" mapstructure:"request_logging"`
// ImportBlockHeaders is a URL from where the headers can be downloaded.
ImportBlockHeaders string `json:"import_block_headers" mapstructure:"import_block_headers"`
}

// AuthenticationConfig is the configuration for Authentication
type AuthenticationConfig struct {
// RequireSigning is the flag that decides if the signing is required
RequireSigning bool `json:"require_signing" mapstructure:"require_signing"`
// SigningDisabled turns off signing. NOTE: Only for development
SigningDisabled bool `json:"signing_disabled" mapstructure:"signing_disabled"`
// AdminKey is used for administrative requests
AdminKey string `json:"admin_key" mapstructure:"admin_key"`
// Scheme it the authentication scheme to use (default is: xpub)
Scheme string `json:"scheme" mapstructure:"scheme"`
// RequireSigning is the flag that decides if the signing is required
RequireSigning bool `json:"require_signing" mapstructure:"require_signing"`
// SigningDisabled turns off signing. NOTE: Only for development
SigningDisabled bool `json:"signing_disabled" mapstructure:"signing_disabled"`
}

// CacheConfig is a configuration for cachestore
Expand Down Expand Up @@ -103,10 +103,8 @@ type ClusterConfig struct {

// RedisConfig is a configuration for Redis cachestore or taskmanager
type RedisConfig struct {
// DependencyMode works only in Redis with script enabled.
DependencyMode bool `json:"dependency_mode" mapstructure:"dependency_mode"`
// UseTLS is a flag which decides whether to use TLS
UseTLS bool `json:"use_tls" mapstructure:"use_tls"`
// URL is Redis url connection string.
URL string `json:"url" mapstructure:"url"`
// MaxActiveConnections is maximum number of active redis connections.
MaxActiveConnections int `json:"max_active_connections" mapstructure:"max_active_connections"`
// MaxIdleConnections is the maximum number of idle connections.
Expand All @@ -115,8 +113,10 @@ type RedisConfig struct {
MaxConnectionLifetime time.Duration `json:"max_connection_lifetime" mapstructure:"max_connection_lifetime"`
// MaxIdleTimeout is the maximum duration of idle redis connection before timeout.
MaxIdleTimeout time.Duration `json:"max_idle_timeout" mapstructure:"max_idle_timeout"`
// URL is Redis url connection string.
URL string `json:"url" mapstructure:"url"`
// DependencyMode works only in Redis with script enabled.
DependencyMode bool `json:"dependency_mode" mapstructure:"dependency_mode"`
// UseTLS is a flag which decides whether to use TLS
UseTLS bool `json:"use_tls" mapstructure:"use_tls"`
}

// DbConfig consists of datastore config and specific dbs configs
Expand All @@ -133,32 +133,32 @@ type DbConfig struct {

// DatastoreConfig is a configuration for the datastore
type DatastoreConfig struct {
// Debug is a flag that decides whether additional output (such as sql statements) should be produced from datastore.
Debug bool `json:"debug" mapstructure:"debug"`
// TablePrefix is the prefix for all table names in the database.
TablePrefix string `json:"table_prefix" mapstructure:"table_prefix"`
// Engine is the database to be used, mysql, sqlite, postgresql.
Engine datastore.Engine `json:"engine" mapstructure:"engine"`
// Debug is a flag that decides whether additional output (such as sql statements) should be produced from datastore.
Debug bool `json:"debug" mapstructure:"debug"`
}

// NewRelicConfig is the configuration for New Relic
type NewRelicConfig struct {
// Enabled is the flag that enables New Relic service.
Enabled bool `json:"enabled" mapstructure:"enabled"`
// DomainName is used for hostname display.
DomainName string `json:"domain_name" mapstructure:"domain_name"`
// LicenseKey is the New Relic license key.
LicenseKey string `json:"license_key" mapstructure:"license_key"`
// Enabled is the flag that enables New Relic service.
Enabled bool `json:"enabled" mapstructure:"enabled"`
}

// NodesConfig consists of blockchain nodes (such as Minercraft and Arc) configuration
type NodesConfig struct {
Callback *CallbackConfig `json:"callback" mapstructure:"callback"`
Apis []*MinerAPI `json:"apis" mapstructure:"apis"`
FeeUnit *FeeUnitConfig `json:"fee_unit" mapstructure:"fee_unit"`
UseFeeQuotes bool `json:"use_fee_quotes" mapstructure:"use_fee_quotes"`
DeploymentID string `json:"deployment_id" mapstructure:"deployment_id"`
Protocol NodesProtocol `json:"protocol" mapstructure:"protocol"`
Apis []*MinerAPI `json:"apis" mapstructure:"apis"`
UseFeeQuotes bool `json:"use_fee_quotes" mapstructure:"use_fee_quotes"`
}

// FeeUnitConfig reflects the utils.FeeUnit struct with proper annotations for json and mapstructure
Expand Down
4 changes: 2 additions & 2 deletions dictionary/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

// ErrorMessage is used for standardizing the messages/codes for errors
type ErrorMessage struct {
Code ErrorCode `json:"code"`
StatusCode int `json:"status_code"`
InternalMessage string `json:"internal_message"`
PublicMessage string `json:"public_message"`
StatusCode int `json:"status_code"`
Code ErrorCode `json:"code"`
}

// errorMessages is the public
Expand Down
88 changes: 44 additions & 44 deletions dictionary/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,59 @@ func TestErrorCode_IsValid(t *testing.T) {
t.Parallel()

var tests = []struct {
expectValid bool
code ErrorCode
testCase string
code ErrorCode
expectValid bool
}{
{
true,
ErrorBadErrorCode,
"ErrorBadErrorCode",
ErrorBadErrorCode,
true,
},
{
true,
ErrorMissingEnv,
"ErrorMissingEnv",
ErrorMissingEnv,
true,
},
{
true,
ErrorInvalidEnv,
"ErrorInvalidEnv",
ErrorInvalidEnv,
true,
},
{
true,
ErrorReadingConfig,
"ErrorReadingConfig",
ErrorReadingConfig,
true,
},
{
true,
ErrorViper,
"ErrorViper",
ErrorViper,
true,
},
{
true,
ErrorConfigValidation,
"ErrorConfigValidation",
ErrorConfigValidation,
true,
},
{
true,
ErrorDecryptEnv,
"ErrorDecryptEnv",
ErrorDecryptEnv,
true,
},
{
true,
ErrorLoadingConfig,
"ErrorLoadingConfig",
ErrorLoadingConfig,
true,
},
{
true,
ErrorLoadingCache,
"ErrorLoadingCache",
ErrorLoadingCache,
true,
},
{
false,
9999,
"unknown code",
9999,
false,
},
}

Expand All @@ -82,58 +82,58 @@ func TestGetInternalMessage(t *testing.T) {

var tests = []struct {
testCase string
code ErrorCode
expectedMessage string
code ErrorCode
}{
{
"ErrorBadErrorCode",
ErrorBadErrorCode,
"failed to find internal error message from error code",
ErrorBadErrorCode,
},
{
"ErrorMissingEnv",
ErrorMissingEnv,
"missing required environment variable: %s",
ErrorMissingEnv,
},
{
"ErrorInvalidEnv",
ErrorInvalidEnv,
"invalid environment variable value: %s",
ErrorInvalidEnv,
},
{
"ErrorReadingConfig",
ErrorReadingConfig,
"error reading environment configuration: %s",
ErrorReadingConfig,
},
{
"ErrorViper",
ErrorViper,
"error in viper unmarshal into config.Values: %s",
ErrorViper,
},
{
"ErrorConfigValidation",
ErrorConfigValidation,
"error in environment configuration validation: %s",
ErrorConfigValidation,
},
{
"ErrorDecryptEnv",
ErrorDecryptEnv,
"error in decrypting %s: %s",
ErrorDecryptEnv,
},
{
"ErrorLoadingConfig",
ErrorLoadingConfig,
"fatal error loading configuration: %s",
ErrorLoadingConfig,
},
{
"ErrorLoadingCache",
ErrorLoadingCache,
"failed to enable cache: %s - cache is disabled",
ErrorLoadingCache,
},
{
"unknown code",
9999,
"failed to find internal error message from error code",
9999,
},
}

Expand All @@ -151,58 +151,58 @@ func TestGetPublicMessage(t *testing.T) {

var tests = []struct {
testCase string
code ErrorCode
expectedMessage string
code ErrorCode
}{
{
"ErrorBadErrorCode",
ErrorBadErrorCode,
"error not found",
ErrorBadErrorCode,
},
{
"ErrorMissingEnv",
ErrorMissingEnv,
"missing required environment variable",
ErrorMissingEnv,
},
{
"ErrorInvalidEnv",
ErrorInvalidEnv,
"invalid environment variable value",
ErrorInvalidEnv,
},
{
"ErrorReadingConfig",
ErrorReadingConfig,
"error reading environment configuration",
ErrorReadingConfig,
},
{
"ErrorViper",
ErrorViper,
"error in loading configuration",
ErrorViper,
},
{
"ErrorConfigValidation",
ErrorConfigValidation,
"error in environment configuration validation",
ErrorConfigValidation,
},
{
"ErrorDecryptEnv",
ErrorDecryptEnv,
"error decrypting an encrypted environment variable",
ErrorDecryptEnv,
},
{
"ErrorLoadingConfig",
ErrorLoadingConfig,
"error loading configuration",
ErrorLoadingConfig,
},
{
"ErrorLoadingCache",
ErrorLoadingCache,
"failed to enable cache",
ErrorLoadingCache,
},
{
"unknown error",
9999,
"error not found",
9999,
},
}

Expand Down

0 comments on commit 66ac26e

Please sign in to comment.