style.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package xvdoc
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Style struct {
  7. JSONMap
  8. }
  9. func (s *Style) UnmarshalJSON(src []byte) (err error) {
  10. //fmt.Println("!!!!!!!UMJ")
  11. var m JSONMap
  12. if err = json.Unmarshal(src, &m); err == nil {
  13. s.JSONMap = m
  14. //fmt.Println(s.JSONMap)
  15. }
  16. return
  17. }
  18. func (s *Style) mergeStyle(m map[string]Style, rStyle JSONMap, parentName string, level int) (res JSONMap, err error) {
  19. if parent, check := m[parentName]; check {
  20. var pm JSONMap
  21. if pm, err = parent.Result(m, parentName, level+1); err == nil {
  22. res = mergeMaps(pm.Map(), rStyle.Map())
  23. }
  24. } else {
  25. res = rStyle.Copy()
  26. }
  27. return
  28. }
  29. func (s *Style) Result(m map[string]Style, childName string, cLevel ...int) (res JSONMap, err error) {
  30. //fmt.Println(">>>>>>>>>>>>>>>>>>>>", s, childName, s.JSONMap)
  31. level := 0
  32. if len(cLevel) > 0 {
  33. level = cLevel[0]
  34. }
  35. if level >= 100 {
  36. return nil, fmt.Errorf("Style loop arrived... (%v)", childName)
  37. }
  38. res = s.Copy()
  39. if s.KeyExists("parent") {
  40. if res, err = s.mergeStyle(m, res, s.StringVal("parent", ""), level); err != nil {
  41. return
  42. }
  43. }
  44. if s.KeyExists("parents") {
  45. for _, pName := range s.StringArray("parents") {
  46. if res, err = s.mergeStyle(m, res, pName, level); err != nil {
  47. return
  48. }
  49. }
  50. }
  51. delete(res, "parent")
  52. delete(res, "parents")
  53. return
  54. }