tablib_util.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package tablib
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. )
  7. // internalLoadFromDict creates a Dataset from an array of map representing columns.
  8. func internalLoadFromDict(input []map[string]interface{}) (*Dataset, error) {
  9. // retrieve columns
  10. headers := make([]string, 0, 10)
  11. for h := range input[0] {
  12. headers = append(headers, h)
  13. }
  14. ds := NewDataset(headers)
  15. for _, e := range input {
  16. row := make([]interface{}, 0, len(headers))
  17. for _, h := range headers {
  18. row = append(row, e[h])
  19. }
  20. ds.AppendValues(row...)
  21. }
  22. return ds, nil
  23. }
  24. // isTagged checks if a tag is in an array of tags.
  25. func isTagged(tag string, tags []string) bool {
  26. for _, t := range tags {
  27. if t == tag {
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. // asString returns a value as a string.
  34. func (d *Dataset) asString(vv interface{}) string {
  35. var v string
  36. switch vv.(type) {
  37. case string:
  38. v = vv.(string)
  39. case int:
  40. v = strconv.Itoa(vv.(int))
  41. case int64:
  42. v = strconv.FormatInt(vv.(int64), 10)
  43. case uint64:
  44. v = strconv.FormatUint(vv.(uint64), 10)
  45. case bool:
  46. v = strconv.FormatBool(vv.(bool))
  47. case float64:
  48. v = strconv.FormatFloat(vv.(float64), 'G', -1, 32)
  49. case time.Time:
  50. v = vv.(time.Time).Format(time.RFC3339)
  51. default:
  52. if d.EmptyValue != "" {
  53. v = d.EmptyValue
  54. } else {
  55. v = fmt.Sprintf("%s", v)
  56. }
  57. }
  58. return v
  59. }