github.com/searKing/golang/go@v1.2.74/encoding/ini/config.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package encoding is a simple tool to handle key-value encoding, like Redis's configuration:
     6  //       # commet
     7  //       key = value
     8  
     9  package ini
    10  
    11  import (
    12  	"errors"
    13  	"fmt"
    14  	"strconv"
    15  	"strings"
    16  )
    17  
    18  var (
    19  	ErrNil = errors.New("nil value")
    20  )
    21  
    22  type Config struct {
    23  	Values map[string]string
    24  }
    25  
    26  func NewConfig() *Config {
    27  	c := &Config{}
    28  	c.Values = make(map[string]string)
    29  	return c
    30  }
    31  
    32  /*
    33     bool: true, false, 0, 1, or ""
    34  */
    35  func (c *Config) GetBool(key string) (bool, error) {
    36  	v, err := c.GetString(key)
    37  	if err != nil {
    38  		return false, err
    39  	}
    40  
    41  	v = strings.ToLower(v)
    42  	switch v {
    43  	case "true", "1":
    44  		return true, nil
    45  	case "false", "0", "":
    46  		return false, nil
    47  	default:
    48  		return false, fmt.Errorf("invalid bool format %s", v)
    49  	}
    50  }
    51  
    52  /*
    53     int may be pure number or below format:
    54         # 1k => 1000 bytes
    55         # 1kb => 1024 bytes
    56         # 1m => 1000000 bytes
    57         # 1mb => 1024*1024 bytes
    58         # 1g => 1000000000 bytes
    59         # 1gb => 1024*1024*1024 bytes
    60  */
    61  func (c *Config) GetInt64(key string) (int64, error) {
    62  	v, err := c.GetString(key)
    63  	if err != nil {
    64  		return 0, err
    65  	}
    66  
    67  	if len(v) == 0 {
    68  		return 0, ErrNil
    69  	}
    70  
    71  	var scale int64 = 1
    72  	v = strings.ToLower(v)
    73  
    74  	var b bool = false
    75  	if v[len(v)-1] == 'b' {
    76  		v = v[0 : len(v)-1]
    77  		b = true
    78  	}
    79  
    80  	if len(v) == 0 {
    81  		return 0, fmt.Errorf("invalid number format %s", v)
    82  	}
    83  
    84  	switch v[len(v)-1] {
    85  	case 'k':
    86  		v = v[0 : len(v)-1]
    87  		if b {
    88  			scale = 1024
    89  		} else {
    90  			scale = 1000
    91  		}
    92  		break
    93  	case 'm':
    94  		v = v[0 : len(v)-1]
    95  		if b {
    96  			scale = 1024 * 1024
    97  		} else {
    98  			scale = 1000 * 1000
    99  		}
   100  	case 'g':
   101  		v = v[0 : len(v)-1]
   102  		if b {
   103  			scale = 1024 * 1024 * 1024
   104  		} else {
   105  			scale = 1000 * 1000 * 1000
   106  		}
   107  	}
   108  
   109  	var n int64
   110  	n, err = strconv.ParseInt(v, 10, 64)
   111  	if err != nil {
   112  		return 0, err
   113  	}
   114  
   115  	return n * scale, nil
   116  }
   117  
   118  func (c *Config) GetUint64(key string) (uint64, error) {
   119  	v, err := c.GetInt64(key)
   120  	if v < 0 {
   121  		return 0, fmt.Errorf("negative number %d", v)
   122  	}
   123  	return uint64(v), err
   124  }
   125  
   126  func (c *Config) GetInt(key string) (int, error) {
   127  	v, err := c.GetInt64(key)
   128  	return int(v), err
   129  }
   130  
   131  func (c *Config) GetString(key string) (string, error) {
   132  	v, ok := c.Values[key]
   133  	if !ok {
   134  		return "", ErrNil
   135  	} else {
   136  		return v, nil
   137  	}
   138  }
   139  
   140  func (c *Config) SetString(key string, value string) {
   141  	c.Values[key] = value
   142  }
   143  
   144  func (c *Config) SetInt64(key string, n int64) {
   145  	c.Values[key] = strconv.FormatInt(n, 10)
   146  }
   147  
   148  func (c *Config) SetUint64(key string, n uint64) {
   149  	c.Values[key] = strconv.FormatUint(n, 10)
   150  }
   151  
   152  func (c *Config) SetInt(key string, n int) {
   153  	c.SetInt64(key, int64(n))
   154  }
   155  
   156  func (c *Config) SetBool(key string, v bool) {
   157  	if v {
   158  		c.Values[key] = "true"
   159  	} else {
   160  		c.Values[key] = "false"
   161  	}
   162  }