upgradehelper.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 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. "fmt"
  19. "os"
  20. "strings"
  21. "gopkg.in/yaml.v3"
  22. )
  23. type YAMLMap map[string]YAMLNode
  24. type YAMLList []YAMLNode
  25. type YAMLNode struct {
  26. *yaml.Node
  27. Map YAMLMap
  28. List YAMLList
  29. Key *yaml.Node
  30. }
  31. type YAMLType uint32
  32. const (
  33. Null YAMLType = 1 << iota
  34. Bool
  35. Str
  36. Int
  37. Float
  38. Timestamp
  39. List
  40. Map
  41. Binary
  42. )
  43. func (t YAMLType) String() string {
  44. switch t {
  45. case Null:
  46. return NullTag
  47. case Bool:
  48. return BoolTag
  49. case Str:
  50. return StrTag
  51. case Int:
  52. return IntTag
  53. case Float:
  54. return FloatTag
  55. case Timestamp:
  56. return TimestampTag
  57. case List:
  58. return SeqTag
  59. case Map:
  60. return MapTag
  61. case Binary:
  62. return BinaryTag
  63. default:
  64. panic(fmt.Errorf("can't convert type %d to string", t))
  65. }
  66. }
  67. func tagToType(tag string) YAMLType {
  68. switch tag {
  69. case NullTag:
  70. return Null
  71. case BoolTag:
  72. return Bool
  73. case StrTag:
  74. return Str
  75. case IntTag:
  76. return Int
  77. case FloatTag:
  78. return Float
  79. case TimestampTag:
  80. return Timestamp
  81. case SeqTag:
  82. return List
  83. case MapTag:
  84. return Map
  85. case BinaryTag:
  86. return Binary
  87. default:
  88. return 0
  89. }
  90. }
  91. const (
  92. NullTag = "!!null"
  93. BoolTag = "!!bool"
  94. StrTag = "!!str"
  95. IntTag = "!!int"
  96. FloatTag = "!!float"
  97. TimestampTag = "!!timestamp"
  98. SeqTag = "!!seq"
  99. MapTag = "!!map"
  100. BinaryTag = "!!binary"
  101. )
  102. func fromNode(node, key *yaml.Node) YAMLNode {
  103. switch node.Kind {
  104. case yaml.DocumentNode:
  105. return fromNode(node.Content[0], nil)
  106. case yaml.AliasNode:
  107. return fromNode(node.Alias, nil)
  108. case yaml.MappingNode:
  109. return YAMLNode{
  110. Node: node,
  111. Map: parseYAMLMap(node),
  112. Key: key,
  113. }
  114. case yaml.SequenceNode:
  115. return YAMLNode{
  116. Node: node,
  117. List: parseYAMLList(node),
  118. }
  119. default:
  120. return YAMLNode{Node: node, Key: key}
  121. }
  122. }
  123. func (yn *YAMLNode) toNode() *yaml.Node {
  124. switch {
  125. case yn.Map != nil && yn.Node.Kind == yaml.MappingNode:
  126. yn.Content = yn.Map.toNodes()
  127. case yn.List != nil && yn.Node.Kind == yaml.SequenceNode:
  128. yn.Content = yn.List.toNodes()
  129. }
  130. return yn.Node
  131. }
  132. func parseYAMLList(node *yaml.Node) YAMLList {
  133. data := make(YAMLList, len(node.Content))
  134. for i, item := range node.Content {
  135. data[i] = fromNode(item, nil)
  136. }
  137. return data
  138. }
  139. func (yl YAMLList) toNodes() []*yaml.Node {
  140. nodes := make([]*yaml.Node, len(yl))
  141. for i, item := range yl {
  142. nodes[i] = item.toNode()
  143. }
  144. return nodes
  145. }
  146. func parseYAMLMap(node *yaml.Node) YAMLMap {
  147. if len(node.Content)%2 != 0 {
  148. panic(fmt.Errorf("uneven number of items in YAML map (%d)", len(node.Content)))
  149. }
  150. data := make(YAMLMap, len(node.Content)/2)
  151. for i := 0; i < len(node.Content); i += 2 {
  152. key := node.Content[i]
  153. value := node.Content[i+1]
  154. if key.Kind == yaml.ScalarNode {
  155. data[key.Value] = fromNode(value, key)
  156. }
  157. }
  158. return data
  159. }
  160. func (ym YAMLMap) toNodes() []*yaml.Node {
  161. nodes := make([]*yaml.Node, len(ym)*2)
  162. i := 0
  163. for key, value := range ym {
  164. nodes[i] = makeStringNode(key)
  165. nodes[i+1] = value.toNode()
  166. i += 2
  167. }
  168. return nodes
  169. }
  170. func makeStringNode(val string) *yaml.Node {
  171. var node yaml.Node
  172. node.SetString(val)
  173. return &node
  174. }
  175. type UpgradeHelper struct {
  176. base YAMLNode
  177. cfg YAMLNode
  178. }
  179. func NewUpgradeHelper(base, cfg *yaml.Node) *UpgradeHelper {
  180. return &UpgradeHelper{
  181. base: fromNode(base, nil),
  182. cfg: fromNode(cfg, nil),
  183. }
  184. }
  185. func (helper *UpgradeHelper) Copy(allowedTypes YAMLType, path ...string) {
  186. base, cfg := helper.base, helper.cfg
  187. var ok bool
  188. for _, item := range path {
  189. base = base.Map[item]
  190. cfg, ok = cfg.Map[item]
  191. if !ok {
  192. return
  193. }
  194. }
  195. if allowedTypes&tagToType(cfg.Tag) == 0 {
  196. _, _ = fmt.Fprintf(os.Stderr, "Ignoring incorrect config field type %s at %s\n", cfg.Tag, strings.Join(path, "->"))
  197. return
  198. }
  199. base.Tag = cfg.Tag
  200. base.Style = cfg.Style
  201. switch base.Kind {
  202. case yaml.ScalarNode:
  203. base.Value = cfg.Value
  204. case yaml.SequenceNode, yaml.MappingNode:
  205. base.Content = cfg.Content
  206. }
  207. }
  208. func getNode(cfg YAMLNode, path []string) *YAMLNode {
  209. var ok bool
  210. for _, item := range path {
  211. cfg, ok = cfg.Map[item]
  212. if !ok {
  213. return nil
  214. }
  215. }
  216. return &cfg
  217. }
  218. func (helper *UpgradeHelper) GetNode(path ...string) *YAMLNode {
  219. return getNode(helper.cfg, path)
  220. }
  221. func (helper *UpgradeHelper) GetBaseNode(path ...string) *YAMLNode {
  222. return getNode(helper.base, path)
  223. }
  224. func (helper *UpgradeHelper) Get(tag YAMLType, path ...string) (string, bool) {
  225. node := helper.GetNode(path...)
  226. if node == nil || node.Kind != yaml.ScalarNode || tag&tagToType(node.Tag) == 0 {
  227. return "", false
  228. }
  229. return node.Value, true
  230. }
  231. func (helper *UpgradeHelper) GetBase(path ...string) string {
  232. return helper.GetBaseNode(path...).Value
  233. }
  234. func (helper *UpgradeHelper) Set(tag YAMLType, value string, path ...string) {
  235. base := helper.base
  236. for _, item := range path {
  237. base = base.Map[item]
  238. }
  239. base.Tag = tag.String()
  240. base.Value = value
  241. }
  242. func (helper *UpgradeHelper) SetMap(value YAMLMap, path ...string) {
  243. base := helper.base
  244. for _, item := range path {
  245. base = base.Map[item]
  246. }
  247. if base.Tag != MapTag || base.Kind != yaml.MappingNode {
  248. panic(fmt.Errorf("invalid target for SetMap(%+v): tag:%s, kind:%d", path, base.Tag, base.Kind))
  249. }
  250. base.Content = value.toNodes()
  251. }