github.com/mailru/activerecord@v1.12.2/pkg/activerecord/config.go (about)

     1  package activerecord
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  type DefaultConfig struct {
    10  	cfg     map[string]interface{}
    11  	created time.Time
    12  }
    13  
    14  func NewDefaultConfig() *DefaultConfig {
    15  	return &DefaultConfig{
    16  		cfg: make(map[string]interface{}),
    17  	}
    18  }
    19  
    20  func NewDefaultConfigFromMap(cfg map[string]interface{}) *DefaultConfig {
    21  	return &DefaultConfig{
    22  		cfg:     cfg,
    23  		created: time.Now(),
    24  	}
    25  }
    26  
    27  func (dc *DefaultConfig) GetLastUpdateTime() time.Time {
    28  	return dc.created
    29  }
    30  
    31  func (dc *DefaultConfig) GetBool(ctx context.Context, confPath string, dfl ...bool) bool {
    32  	if ret, ok := dc.GetBoolIfExists(ctx, confPath); ok {
    33  		return ret
    34  	}
    35  
    36  	if len(dfl) != 0 {
    37  		return dfl[0]
    38  	}
    39  
    40  	return false
    41  }
    42  
    43  func (dc *DefaultConfig) GetBoolIfExists(ctx context.Context, confPath string) (value bool, ok bool) {
    44  	if param, ex := dc.cfg[confPath]; ex {
    45  		if ret, ok := param.(bool); ok {
    46  			return ret, true
    47  		}
    48  
    49  		Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want bool", confPath, param))
    50  	}
    51  
    52  	return false, false
    53  }
    54  
    55  func (dc *DefaultConfig) GetInt(ctx context.Context, confPath string, dfl ...int) int {
    56  	if ret, ok := dc.GetIntIfExists(ctx, confPath); ok {
    57  		return ret
    58  	}
    59  
    60  	if len(dfl) != 0 {
    61  		return dfl[0]
    62  	}
    63  
    64  	return 0
    65  }
    66  
    67  func (dc *DefaultConfig) GetIntIfExists(ctx context.Context, confPath string) (int, bool) {
    68  	if param, ex := dc.cfg[confPath]; ex {
    69  		if ret, ok := param.(int); ok {
    70  			return ret, true
    71  		}
    72  
    73  		Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want int", confPath, param))
    74  	}
    75  
    76  	return 0, false
    77  }
    78  
    79  func (dc *DefaultConfig) GetDuration(ctx context.Context, confPath string, dfl ...time.Duration) time.Duration {
    80  	if ret, ok := dc.GetDurationIfExists(ctx, confPath); ok {
    81  		return ret
    82  	}
    83  
    84  	if len(dfl) != 0 {
    85  		return dfl[0]
    86  	}
    87  
    88  	return 0
    89  }
    90  
    91  func (dc *DefaultConfig) GetDurationIfExists(ctx context.Context, confPath string) (time.Duration, bool) {
    92  	if param, ex := dc.cfg[confPath]; ex {
    93  		if ret, ok := param.(time.Duration); ok {
    94  			return ret, true
    95  		}
    96  
    97  		Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want time.Duration", confPath, param))
    98  	}
    99  
   100  	return 0, false
   101  }
   102  
   103  func (dc *DefaultConfig) GetString(ctx context.Context, confPath string, dfl ...string) string {
   104  	if ret, ok := dc.GetStringIfExists(ctx, confPath); ok {
   105  		return ret
   106  	}
   107  
   108  	if len(dfl) != 0 {
   109  		return dfl[0]
   110  	}
   111  
   112  	return ""
   113  }
   114  
   115  func (dc *DefaultConfig) GetStringIfExists(ctx context.Context, confPath string) (string, bool) {
   116  	if param, ex := dc.cfg[confPath]; ex {
   117  		if ret, ok := param.(string); ok {
   118  			return ret, true
   119  		}
   120  
   121  		Logger().Warn(ctx, fmt.Sprintf("param %s has type %T, want string", confPath, param))
   122  	}
   123  
   124  	return "", false
   125  }
   126  
   127  func (dc *DefaultConfig) GetStrings(ctx context.Context, confPath string, dfl []string) []string {
   128  	return []string{}
   129  }
   130  
   131  func (dc *DefaultConfig) GetStruct(ctx context.Context, confPath string, valuePtr interface{}) (bool, error) {
   132  	return false, nil
   133  }