github.com/gogf/gf@v1.16.9/os/gcfg/gcfg_config_api.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gcfg
     8  
     9  import (
    10  	"github.com/gogf/gf/errors/gcode"
    11  	"github.com/gogf/gf/errors/gerror"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/encoding/gjson"
    15  
    16  	"github.com/gogf/gf/container/gvar"
    17  	"github.com/gogf/gf/os/gtime"
    18  )
    19  
    20  // Set sets value with specified `pattern`.
    21  // It supports hierarchical data access by char separator, which is '.' in default.
    22  // It is commonly used for updates certain configuration value in runtime.
    23  func (c *Config) Set(pattern string, value interface{}) error {
    24  	if j := c.getJson(); j != nil {
    25  		return j.Set(pattern, value)
    26  	}
    27  	return nil
    28  }
    29  
    30  // Get retrieves and returns value by specified `pattern`.
    31  // It returns all values of current Json object if `pattern` is given empty or string ".".
    32  // It returns nil if no value found by `pattern`.
    33  //
    34  // We can also access slice item by its index number in `pattern` like:
    35  // "list.10", "array.0.name", "array.0.1.id".
    36  //
    37  // It returns a default value specified by `def` if value for `pattern` is not found.
    38  func (c *Config) Get(pattern string, def ...interface{}) interface{} {
    39  	if j := c.getJson(); j != nil {
    40  		return j.Get(pattern, def...)
    41  	}
    42  	return nil
    43  }
    44  
    45  // GetVar returns a gvar.Var with value by given `pattern`.
    46  func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var {
    47  	if j := c.getJson(); j != nil {
    48  		return j.GetVar(pattern, def...)
    49  	}
    50  	return gvar.New(nil)
    51  }
    52  
    53  // Contains checks whether the value by specified `pattern` exist.
    54  func (c *Config) Contains(pattern string) bool {
    55  	if j := c.getJson(); j != nil {
    56  		return j.Contains(pattern)
    57  	}
    58  	return false
    59  }
    60  
    61  // GetMap retrieves and returns the value by specified `pattern` as map[string]interface{}.
    62  func (c *Config) GetMap(pattern string, def ...interface{}) map[string]interface{} {
    63  	if j := c.getJson(); j != nil {
    64  		return j.GetMap(pattern, def...)
    65  	}
    66  	return nil
    67  }
    68  
    69  // GetMapStrStr retrieves and returns the value by specified `pattern` as map[string]string.
    70  func (c *Config) GetMapStrStr(pattern string, def ...interface{}) map[string]string {
    71  	if j := c.getJson(); j != nil {
    72  		return j.GetMapStrStr(pattern, def...)
    73  	}
    74  	return nil
    75  }
    76  
    77  // GetArray retrieves the value by specified `pattern`,
    78  // and converts it to a slice of []interface{}.
    79  func (c *Config) GetArray(pattern string, def ...interface{}) []interface{} {
    80  	if j := c.getJson(); j != nil {
    81  		return j.GetArray(pattern, def...)
    82  	}
    83  	return nil
    84  }
    85  
    86  // GetBytes retrieves the value by specified `pattern` and converts it to []byte.
    87  func (c *Config) GetBytes(pattern string, def ...interface{}) []byte {
    88  	if j := c.getJson(); j != nil {
    89  		return j.GetBytes(pattern, def...)
    90  	}
    91  	return nil
    92  }
    93  
    94  // GetString retrieves the value by specified `pattern` and converts it to string.
    95  func (c *Config) GetString(pattern string, def ...interface{}) string {
    96  	if j := c.getJson(); j != nil {
    97  		return j.GetString(pattern, def...)
    98  	}
    99  	return ""
   100  }
   101  
   102  // GetStrings retrieves the value by specified `pattern` and converts it to []string.
   103  func (c *Config) GetStrings(pattern string, def ...interface{}) []string {
   104  	if j := c.getJson(); j != nil {
   105  		return j.GetStrings(pattern, def...)
   106  	}
   107  	return nil
   108  }
   109  
   110  // GetInterfaces is alias of GetArray.
   111  // See GetArray.
   112  func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{} {
   113  	if j := c.getJson(); j != nil {
   114  		return j.GetInterfaces(pattern, def...)
   115  	}
   116  	return nil
   117  }
   118  
   119  // GetBool retrieves the value by specified `pattern`,
   120  // converts and returns it as bool.
   121  // It returns false when value is: "", 0, false, off, nil;
   122  // or returns true instead.
   123  func (c *Config) GetBool(pattern string, def ...interface{}) bool {
   124  	if j := c.getJson(); j != nil {
   125  		return j.GetBool(pattern, def...)
   126  	}
   127  	return false
   128  }
   129  
   130  // GetFloat32 retrieves the value by specified `pattern` and converts it to float32.
   131  func (c *Config) GetFloat32(pattern string, def ...interface{}) float32 {
   132  	if j := c.getJson(); j != nil {
   133  		return j.GetFloat32(pattern, def...)
   134  	}
   135  	return 0
   136  }
   137  
   138  // GetFloat64 retrieves the value by specified `pattern` and converts it to float64.
   139  func (c *Config) GetFloat64(pattern string, def ...interface{}) float64 {
   140  	if j := c.getJson(); j != nil {
   141  		return j.GetFloat64(pattern, def...)
   142  	}
   143  	return 0
   144  }
   145  
   146  // GetFloats retrieves the value by specified `pattern` and converts it to []float64.
   147  func (c *Config) GetFloats(pattern string, def ...interface{}) []float64 {
   148  	if j := c.getJson(); j != nil {
   149  		return j.GetFloats(pattern, def...)
   150  	}
   151  	return nil
   152  }
   153  
   154  // GetInt retrieves the value by specified `pattern` and converts it to int.
   155  func (c *Config) GetInt(pattern string, def ...interface{}) int {
   156  	if j := c.getJson(); j != nil {
   157  		return j.GetInt(pattern, def...)
   158  	}
   159  	return 0
   160  }
   161  
   162  // GetInt8 retrieves the value by specified `pattern` and converts it to int8.
   163  func (c *Config) GetInt8(pattern string, def ...interface{}) int8 {
   164  	if j := c.getJson(); j != nil {
   165  		return j.GetInt8(pattern, def...)
   166  	}
   167  	return 0
   168  }
   169  
   170  // GetInt16 retrieves the value by specified `pattern` and converts it to int16.
   171  func (c *Config) GetInt16(pattern string, def ...interface{}) int16 {
   172  	if j := c.getJson(); j != nil {
   173  		return j.GetInt16(pattern, def...)
   174  	}
   175  	return 0
   176  }
   177  
   178  // GetInt32 retrieves the value by specified `pattern` and converts it to int32.
   179  func (c *Config) GetInt32(pattern string, def ...interface{}) int32 {
   180  	if j := c.getJson(); j != nil {
   181  		return j.GetInt32(pattern, def...)
   182  	}
   183  	return 0
   184  }
   185  
   186  // GetInt64 retrieves the value by specified `pattern` and converts it to int64.
   187  func (c *Config) GetInt64(pattern string, def ...interface{}) int64 {
   188  	if j := c.getJson(); j != nil {
   189  		return j.GetInt64(pattern, def...)
   190  	}
   191  	return 0
   192  }
   193  
   194  // GetInts retrieves the value by specified `pattern` and converts it to []int.
   195  func (c *Config) GetInts(pattern string, def ...interface{}) []int {
   196  	if j := c.getJson(); j != nil {
   197  		return j.GetInts(pattern, def...)
   198  	}
   199  	return nil
   200  }
   201  
   202  // GetUint retrieves the value by specified `pattern` and converts it to uint.
   203  func (c *Config) GetUint(pattern string, def ...interface{}) uint {
   204  	if j := c.getJson(); j != nil {
   205  		return j.GetUint(pattern, def...)
   206  	}
   207  	return 0
   208  }
   209  
   210  // GetUint8 retrieves the value by specified `pattern` and converts it to uint8.
   211  func (c *Config) GetUint8(pattern string, def ...interface{}) uint8 {
   212  	if j := c.getJson(); j != nil {
   213  		return j.GetUint8(pattern, def...)
   214  	}
   215  	return 0
   216  }
   217  
   218  // GetUint16 retrieves the value by specified `pattern` and converts it to uint16.
   219  func (c *Config) GetUint16(pattern string, def ...interface{}) uint16 {
   220  	if j := c.getJson(); j != nil {
   221  		return j.GetUint16(pattern, def...)
   222  	}
   223  	return 0
   224  }
   225  
   226  // GetUint32 retrieves the value by specified `pattern` and converts it to uint32.
   227  func (c *Config) GetUint32(pattern string, def ...interface{}) uint32 {
   228  	if j := c.getJson(); j != nil {
   229  		return j.GetUint32(pattern, def...)
   230  	}
   231  	return 0
   232  }
   233  
   234  // GetUint64 retrieves the value by specified `pattern` and converts it to uint64.
   235  func (c *Config) GetUint64(pattern string, def ...interface{}) uint64 {
   236  	if j := c.getJson(); j != nil {
   237  		return j.GetUint64(pattern, def...)
   238  	}
   239  	return 0
   240  }
   241  
   242  // GetTime retrieves the value by specified `pattern` and converts it to time.Time.
   243  func (c *Config) GetTime(pattern string, format ...string) time.Time {
   244  	if j := c.getJson(); j != nil {
   245  		return j.GetTime(pattern, format...)
   246  	}
   247  	return time.Time{}
   248  }
   249  
   250  // GetDuration retrieves the value by specified `pattern` and converts it to time.Duration.
   251  func (c *Config) GetDuration(pattern string, def ...interface{}) time.Duration {
   252  	if j := c.getJson(); j != nil {
   253  		return j.GetDuration(pattern, def...)
   254  	}
   255  	return 0
   256  }
   257  
   258  // GetGTime retrieves the value by specified `pattern` and converts it to *gtime.Time.
   259  func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time {
   260  	if j := c.getJson(); j != nil {
   261  		return j.GetGTime(pattern, format...)
   262  	}
   263  	return nil
   264  }
   265  
   266  // GetJson gets the value by specified `pattern`,
   267  // and converts it to a un-concurrent-safe Json object.
   268  func (c *Config) GetJson(pattern string, def ...interface{}) *gjson.Json {
   269  	if j := c.getJson(); j != nil {
   270  		return j.GetJson(pattern, def...)
   271  	}
   272  	return nil
   273  }
   274  
   275  // GetJsons gets the value by specified `pattern`,
   276  // and converts it to a slice of un-concurrent-safe Json object.
   277  func (c *Config) GetJsons(pattern string, def ...interface{}) []*gjson.Json {
   278  	if j := c.getJson(); j != nil {
   279  		return j.GetJsons(pattern, def...)
   280  	}
   281  	return nil
   282  }
   283  
   284  // GetJsonMap gets the value by specified `pattern`,
   285  // and converts it to a map of un-concurrent-safe Json object.
   286  func (c *Config) GetJsonMap(pattern string, def ...interface{}) map[string]*gjson.Json {
   287  	if j := c.getJson(); j != nil {
   288  		return j.GetJsonMap(pattern, def...)
   289  	}
   290  	return nil
   291  }
   292  
   293  // GetStruct retrieves the value by specified `pattern` and converts it to specified object
   294  // `pointer`. The `pointer` should be the pointer to an object.
   295  func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
   296  	if j := c.getJson(); j != nil {
   297  		return j.GetStruct(pattern, pointer, mapping...)
   298  	}
   299  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   300  }
   301  
   302  // GetStructs converts any slice to given struct slice.
   303  func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
   304  	if j := c.getJson(); j != nil {
   305  		return j.GetStructs(pattern, pointer, mapping...)
   306  	}
   307  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   308  }
   309  
   310  // GetMapToMap retrieves the value by specified `pattern` and converts it to specified map variable.
   311  // See gconv.MapToMap.
   312  func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
   313  	if j := c.getJson(); j != nil {
   314  		return j.GetMapToMap(pattern, pointer, mapping...)
   315  	}
   316  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   317  }
   318  
   319  // GetMapToMaps retrieves the value by specified `pattern` and converts it to specified map slice
   320  // variable.
   321  // See gconv.MapToMaps.
   322  func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
   323  	if j := c.getJson(); j != nil {
   324  		return j.GetMapToMaps(pattern, pointer, mapping...)
   325  	}
   326  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   327  }
   328  
   329  // GetMapToMapsDeep retrieves the value by specified `pattern` and converts it to specified map slice
   330  // variable recursively.
   331  // See gconv.MapToMapsDeep.
   332  func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
   333  	if j := c.getJson(); j != nil {
   334  		return j.GetMapToMapsDeep(pattern, pointer, mapping...)
   335  	}
   336  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   337  }
   338  
   339  // Map converts current Json object to map[string]interface{}. It returns nil if fails.
   340  func (c *Config) Map() map[string]interface{} {
   341  	if j := c.getJson(); j != nil {
   342  		return j.Map()
   343  	}
   344  	return nil
   345  }
   346  
   347  // Array converts current Json object to []interface{}.
   348  // It returns nil if fails.
   349  func (c *Config) Array() []interface{} {
   350  	if j := c.getJson(); j != nil {
   351  		return j.Array()
   352  	}
   353  	return nil
   354  }
   355  
   356  // Struct converts current Json object to specified object.
   357  // The `pointer` should be a pointer type of *struct.
   358  func (c *Config) Struct(pointer interface{}, mapping ...map[string]string) error {
   359  	if j := c.getJson(); j != nil {
   360  		return j.Struct(pointer, mapping...)
   361  	}
   362  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   363  }
   364  
   365  // Structs converts current Json object to specified object slice.
   366  // The `pointer` should be a pointer type of []struct/*struct.
   367  func (c *Config) Structs(pointer interface{}, mapping ...map[string]string) error {
   368  	if j := c.getJson(); j != nil {
   369  		return j.Structs(pointer, mapping...)
   370  	}
   371  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   372  }
   373  
   374  // MapToMap converts current Json object to specified map variable.
   375  // The parameter of `pointer` should be type of *map.
   376  func (c *Config) MapToMap(pointer interface{}, mapping ...map[string]string) error {
   377  	if j := c.getJson(); j != nil {
   378  		return j.MapToMap(pointer, mapping...)
   379  	}
   380  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   381  }
   382  
   383  // MapToMaps converts current Json object to specified map variable slice.
   384  // The parameter of `pointer` should be type of []map/*map.
   385  func (c *Config) MapToMaps(pointer interface{}, mapping ...map[string]string) error {
   386  	if j := c.getJson(); j != nil {
   387  		return j.MapToMaps(pointer, mapping...)
   388  	}
   389  	return gerror.NewCode(gcode.CodeMissingConfiguration, "configuration not found")
   390  }
   391  
   392  // Clear removes all parsed configuration files content cache,
   393  // which will force reload configuration content from file.
   394  func (c *Config) Clear() {
   395  	c.jsonMap.Clear()
   396  }
   397  
   398  // Dump prints current Json object with more manually readable.
   399  func (c *Config) Dump() {
   400  	if j := c.getJson(); j != nil {
   401  		j.Dump()
   402  	}
   403  }