unmarshal.go 872 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package cli_util
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "yunion.io/x/ovsdb/types"
  6. )
  7. type List struct {
  8. Headings ListHeadings
  9. Data []ListDataRow
  10. }
  11. type ListHeadings []string
  12. type ListDataRow []ListDataColumn
  13. type ListDataColumn = interface{}
  14. func (h ListHeadings) getNth(i int) (string, error) {
  15. if i < len(h) {
  16. return h[i], nil
  17. }
  18. return "", fmt.Errorf("bad index into list headings (%d>=%d)", i, len(h))
  19. }
  20. func UnmarshalJSON(data []byte, irows types.ITable) error {
  21. list := &List{}
  22. if err := json.Unmarshal(data, list); err != nil {
  23. return err
  24. }
  25. for _, row := range list.Data {
  26. irow := irows.NewRow()
  27. for colI := range row {
  28. col := row[colI]
  29. colName, err := list.Headings.getNth(colI)
  30. if err != nil {
  31. return err
  32. }
  33. if err := irow.SetColumn(colName, col); err != nil {
  34. return err
  35. }
  36. }
  37. irows.AppendRow(irow)
  38. }
  39. return nil
  40. }