1234567891011121314151617181920212223242526272829303132333435363738 |
- package main
- import "fmt"
- type Attributes struct {
- node *Node // Ref
- Category string
- Others map[string]float64
- }
- func (a *Attributes) Parse(node *Node) error {
- if node.Key() != "attributes" {
- return fmt.Errorf("not attributes")
- }
- a.node = node
- for _, kid := range node.Children {
- if kid.Key() == "category" {
- a.Category = kid.Value()
- } else {
- a.Others[kid.Key()] = kid.ValueFloat64()
- }
- }
- return nil
- }
- func (a *Attributes) Update() error {
- if a.node == nil {
- return fmt.Errorf("no node, attributes")
- }
- a.node.RmAllChildren()
- n := a.node.NewChild()
- n.Set("category", a.Category)
- for k, v := range a.Others {
- n := a.node.NewChild()
- n.Set(k, v)
- }
- return nil
- }
|