package main import "fmt" type OutfitList struct { node *Node // Ref List []OutfitListItem } func (ol *OutfitList) Parse(node *Node) error { if node.Key() != "outfits" { return fmt.Errorf("not a outfit list") } ol.node = node ol.List = []OutfitListItem{} for _, kid := range node.Children { o := OutfitListItem{} o.Parse(kid) ol.AddOutfit(o) } return nil } func (ol *OutfitList) AddOutfit(outfit OutfitListItem) { ol.List = append(ol.List, outfit) } func (ol *OutfitList) Update() error { if ol.node == nil { return fmt.Errorf("invalid node") } ol.node.RmAllChildren() for _, outfit := range ol.List { outfit.Generate(ol.node) } return nil } type OutfitListItem struct { Name string Count int } func (o *OutfitListItem) Parse(node *Node) error { o.Name = node.Key() if CanInt(node.Value()) { o.Count = node.ValueInt() } else { o.Count = 1 } return nil } func (o *OutfitListItem) Generate(node *Node) { n := node.NewChild() if o.Count > 1 { n.Set(o.Name, o.Count) } else { n.Line = n.Format(o.Name) } }