utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package gotabulate
  2. import "strconv"
  3. import "fmt"
  4. // Create normalized Array from strings
  5. func createFromString(data [][]string) []*TabulateRow {
  6. rows := make([]*TabulateRow, len(data))
  7. for index, el := range data {
  8. rows[index] = &TabulateRow{Elements: el}
  9. }
  10. return rows
  11. }
  12. // Create normalized array of rows from mixed data (interface{})
  13. func createFromMixed(data [][]interface{}, format byte) []*TabulateRow {
  14. rows := make([]*TabulateRow, len(data))
  15. for index_1, element := range data {
  16. normalized := make([]string, len(element))
  17. for index, el := range element {
  18. switch el.(type) {
  19. case int32:
  20. quoted := strconv.QuoteRuneToASCII(el.(int32))
  21. normalized[index] = quoted[1 : len(quoted)-1]
  22. case int:
  23. normalized[index] = strconv.Itoa(el.(int))
  24. case int64:
  25. normalized[index] = strconv.FormatInt(el.(int64), 10)
  26. case bool:
  27. normalized[index] = strconv.FormatBool(el.(bool))
  28. case float64:
  29. normalized[index] = strconv.FormatFloat(el.(float64), format, -1, 64)
  30. case uint64:
  31. normalized[index] = strconv.FormatUint(el.(uint64), 10)
  32. case nil:
  33. normalized[index] = "nil"
  34. default:
  35. normalized[index] = fmt.Sprintf("%s", el)
  36. }
  37. }
  38. rows[index_1] = &TabulateRow{Elements: normalized}
  39. }
  40. return rows
  41. }
  42. // Create normalized array from ints
  43. func createFromInt(data [][]int) []*TabulateRow {
  44. rows := make([]*TabulateRow, len(data))
  45. for index_1, arr := range data {
  46. row := make([]string, len(arr))
  47. for index, el := range arr {
  48. row[index] = strconv.Itoa(el)
  49. }
  50. rows[index_1] = &TabulateRow{Elements: row}
  51. }
  52. return rows
  53. }
  54. // Create normalized array from float64
  55. func createFromFloat64(data [][]float64, format byte) []*TabulateRow {
  56. rows := make([]*TabulateRow, len(data))
  57. for index_1, arr := range data {
  58. row := make([]string, len(arr))
  59. for index, el := range arr {
  60. row[index] = strconv.FormatFloat(el, format, -1, 64)
  61. }
  62. rows[index_1] = &TabulateRow{Elements: row}
  63. }
  64. return rows
  65. }
  66. // Create normalized array from ints32
  67. func createFromInt32(data [][]int32) []*TabulateRow {
  68. rows := make([]*TabulateRow, len(data))
  69. for index_1, arr := range data {
  70. row := make([]string, len(arr))
  71. for index, el := range arr {
  72. quoted := strconv.QuoteRuneToASCII(el)
  73. row[index] = quoted[1 : len(quoted)-1]
  74. }
  75. rows[index_1] = &TabulateRow{Elements: row}
  76. }
  77. return rows
  78. }
  79. // Create normalized array from ints64
  80. func createFromInt64(data [][]int64) []*TabulateRow {
  81. rows := make([]*TabulateRow, len(data))
  82. for index_1, arr := range data {
  83. row := make([]string, len(arr))
  84. for index, el := range arr {
  85. row[index] = strconv.FormatInt(el, 10)
  86. }
  87. rows[index_1] = &TabulateRow{Elements: row}
  88. }
  89. return rows
  90. }
  91. // Create normalized array from bools
  92. func createFromBool(data [][]bool) []*TabulateRow {
  93. rows := make([]*TabulateRow, len(data))
  94. for index_1, arr := range data {
  95. row := make([]string, len(arr))
  96. for index, el := range arr {
  97. row[index] = strconv.FormatBool(el)
  98. }
  99. rows[index_1] = &TabulateRow{Elements: row}
  100. }
  101. return rows
  102. }
  103. // Create normalized array from a map of mixed elements (interface{})
  104. // Keys will be used as header
  105. func createFromMapMixed(data map[string][]interface{}, format byte) (headers []string, tData []*TabulateRow) {
  106. var dataslice [][]interface{}
  107. for key, value := range data {
  108. headers = append(headers, key)
  109. dataslice = append(dataslice, value)
  110. }
  111. return headers, createFromMixed(dataslice, format)
  112. }
  113. // Create normalized array from Map of strings
  114. // Keys will be used as header
  115. func createFromMapString(data map[string][]string) (headers []string, tData []*TabulateRow) {
  116. var dataslice [][]string
  117. for key, value := range data {
  118. headers = append(headers, key)
  119. dataslice = append(dataslice, value)
  120. }
  121. return headers, createFromString(dataslice)
  122. }
  123. // Check if element is present in a slice.
  124. func inSlice(a string, list []string) bool {
  125. for _, b := range list {
  126. if b == a {
  127. return true
  128. }
  129. }
  130. return false
  131. }