recursivemap.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package config
  17. import (
  18. "strings"
  19. )
  20. type RecursiveMap map[interface{}]interface{}
  21. func (rm RecursiveMap) GetDefault(path string, defVal interface{}) interface{} {
  22. val, ok := rm.Get(path)
  23. if !ok {
  24. return defVal
  25. }
  26. return val
  27. }
  28. func (rm RecursiveMap) GetMap(path string) RecursiveMap {
  29. val := rm.GetDefault(path, nil)
  30. if val == nil {
  31. return nil
  32. }
  33. newRM, ok := val.(map[interface{}]interface{})
  34. if ok {
  35. return RecursiveMap(newRM)
  36. }
  37. return nil
  38. }
  39. func (rm RecursiveMap) Get(path string) (interface{}, bool) {
  40. if index := strings.IndexRune(path, '.'); index >= 0 {
  41. key := path[:index]
  42. path = path[index+1:]
  43. submap := rm.GetMap(key)
  44. if submap == nil {
  45. return nil, false
  46. }
  47. return submap.Get(path)
  48. }
  49. val, ok := rm[path]
  50. return val, ok
  51. }
  52. func (rm RecursiveMap) GetIntDefault(path string, defVal int) int {
  53. val, ok := rm.GetInt(path)
  54. if !ok {
  55. return defVal
  56. }
  57. return val
  58. }
  59. func (rm RecursiveMap) GetInt(path string) (int, bool) {
  60. val, ok := rm.Get(path)
  61. if !ok {
  62. return 0, ok
  63. }
  64. intVal, ok := val.(int)
  65. return intVal, ok
  66. }
  67. func (rm RecursiveMap) GetStringDefault(path string, defVal string) string {
  68. val, ok := rm.GetString(path)
  69. if !ok {
  70. return defVal
  71. }
  72. return val
  73. }
  74. func (rm RecursiveMap) GetString(path string) (string, bool) {
  75. val, ok := rm.Get(path)
  76. if !ok {
  77. return "", ok
  78. }
  79. strVal, ok := val.(string)
  80. return strVal, ok
  81. }
  82. func (rm RecursiveMap) Set(path string, value interface{}) {
  83. if index := strings.IndexRune(path, '.'); index >= 0 {
  84. key := path[:index]
  85. path = path[index+1:]
  86. nextRM := rm.GetMap(key)
  87. if nextRM == nil {
  88. nextRM = make(RecursiveMap)
  89. rm[key] = nextRM
  90. }
  91. nextRM.Set(path, value)
  92. return
  93. }
  94. rm[path] = value
  95. }
  96. func (rm RecursiveMap) CopyFrom(otherRM RecursiveMap, path string) {
  97. val, ok := otherRM.Get(path)
  98. if ok {
  99. rm.Set(path, val)
  100. }
  101. }