location.go 954 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/beanzilla/gonode"
  5. )
  6. type Location struct {
  7. N_System *gonode.Node `json:"-"`
  8. N_Planet *gonode.Node `json:"-"`
  9. System string `json:"system"`
  10. Planet string `json:"planet"`
  11. }
  12. func (l *Location) String() string {
  13. return fmt.Sprintf("%s in %s", l.Planet, l.System)
  14. }
  15. func (l *Location) Parse(n *gonode.Node) error {
  16. txt := fmt.Sprintf("%v", n.Data())
  17. switch Key(txt) {
  18. case "system":
  19. l.N_System = n
  20. l.System = Value(txt)
  21. case "planet":
  22. l.N_Planet = n
  23. l.Planet = Value(txt)
  24. }
  25. return nil
  26. }
  27. func (l *Location) Valid() bool {
  28. return l.N_Planet != nil && l.N_System != nil
  29. }
  30. func (l *Location) Update() error {
  31. if l.N_System == nil || l.N_Planet == nil {
  32. return fmt.Errorf("missing both nodes, locations are 2 nodes, can't update")
  33. }
  34. l.N_Planet.SetData(fmt.Sprintf("planet %s", Format(l.Planet)))
  35. l.N_System.SetData(fmt.Sprintf("system %s", Format(l.System)))
  36. return nil
  37. }