gcfg_config_api.go 13 KB

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