gcfg_api.go 15 KB

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