odml.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package odml
  2. import (
  3. "fmt"
  4. "encoding/json"
  5. )
  6. type Section struct {
  7. Name string `json:"-" xml:"name"`
  8. Type string `json:"-" xml:"type"`
  9. Properties [] Property `json:"-" xml:"property"`
  10. Text string `json:"text"`
  11. Sections [] Section `json:"-" xml:"section"`
  12. Children [] OdMLObject `json:"children,omitempty"`
  13. }
  14. type Property struct {
  15. Name string `json:"-" xml:"name"`
  16. Value []string `json:"-" xml:"value"`
  17. Text string `json:"text"`
  18. Icon string
  19. Definition string `xml:"definition"`
  20. }
  21. type OdMLObject struct {
  22. Prop Property
  23. Section Section
  24. Type string
  25. }
  26. type Odml struct {
  27. OdmnlSections []Section `json:"children" xml:"section"`
  28. }
  29. func (u *Property) MarshalJSON() ([]byte, error) {
  30. type Alias Property
  31. if u.Text == "" {
  32. u.Text = fmt.Sprintf("%s: %s (%s)", u.Name, u.Value, u.Definition)
  33. }
  34. return json.Marshal(Alias(*u))
  35. }
  36. func (u *Section) MarshalJSON() ([]byte, error) {
  37. type Alias Section
  38. if u.Text == "" {
  39. u.Text = fmt.Sprintf("%s", u.Name)
  40. }
  41. for _, x := range u.Properties {
  42. u.Children = append(u.Children, OdMLObject{Prop: x, Type: "property"})
  43. }
  44. for _, x := range u.Sections {
  45. u.Children = append(u.Children, OdMLObject{Section: x, Type: "section"})
  46. }
  47. return json.Marshal(Alias(*u))
  48. }
  49. func (u *OdMLObject) MarshalJSON() ([]byte, error) {
  50. if u.Type == "property" {
  51. return u.Prop.MarshalJSON()
  52. }
  53. if u.Type == "section" {
  54. return u.Section.MarshalJSON()
  55. }
  56. return nil, fmt.Errorf("Could not unmarshal odml object")
  57. }