123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package main
- import (
- "fmt"
- "github.com/beanzilla/gonode"
- )
- type Profile struct {
- N *gonode.Node `json:"-"`
- Pilot string `json:"pilot"`
- Playtime float64 `json:"playtime"`
- Date Date `json:"date"`
- Location Location `json:"location"`
- Travel TravelPath `json:"travel"`
- }
- func (p *Profile) Parse(n *gonode.Node) error {
- if !n.HasTag("root") {
- return fmt.Errorf("expected 'root' node, got a regular node, invalid node")
- }
- for kid := range n.Iter() {
- txt := fmt.Sprintf("%v", kid.Data())
- switch Key(txt) {
- case "pilot":
- p.Pilot = Value(txt)
- case "date":
- err := p.Date.Parse(kid)
- if err != nil {
- return err
- }
- case "system", "planet":
- err := p.Location.Parse(kid)
- if err != nil {
- return err
- }
- case "travel", "travel destination", "playtime":
- if Key(txt) == "playtime" {
- if !IsFloat(Value(txt)) {
- return fmt.Errorf("failed parsing playtime, expected float")
- }
- p.Playtime = ValueFloat64(txt)
- }
- err := p.Travel.Parse(kid)
- if err != nil {
- return err
- }
- }
- }
- if !p.Location.Valid() {
- return fmt.Errorf("didn't find both nodes for location, missing data")
- }
- p.N = n
- return nil
- }
- func (p *Profile) Update() error {
- if p.N == nil {
- return fmt.Errorf("missing node, can't update")
- }
- for kid := range p.N.Iter() {
- txt := fmt.Sprintf("%v", kid.Data())
- switch Key(txt) {
- case "pilot":
- kid.SetData(fmt.Sprintf("pilot %s", p.Pilot))
- }
- }
- err := p.Date.Update()
- if err != nil {
- return err
- }
- err = p.Location.Update()
- if err != nil {
- return err
- }
- err = p.Travel.Update()
- if err != nil {
- return err
- }
- return nil
- }
|