-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.go
More file actions
79 lines (58 loc) · 1.42 KB
/
convert.go
File metadata and controls
79 lines (58 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package scsv
import "fmt"
func AsCSV(parsedSCSV KeyValuePairs) string {
conversion := ""
for key, values := range parsedSCSV {
for _, value := range values {
csvValue := ""
for idx, column := range value {
csvValue += column
if idx != len(value)-1 {
csvValue += ","
}
}
conversion += fmt.Sprintf("%s,%s\n", key, csvValue)
}
}
return conversion
}
func AsJSON(parsedSCSV KeyValuePairs) string {
return "JSON conversions are incorrect since SCSV v1.0.0, they will be fixed with v1.0.1"
conversion := "{\n"
keyIdx := 0
for key, values := range parsedSCSV {
keyIdx++
arrayDelim := ",\n"
if keyIdx == len(parsedSCSV) {
arrayDelim = "\n"
}
conversion += fmt.Sprintf(" \"%s\": [\n", key)
for i, value := range values {
delim := ",\n"
if i == len(values)-1 {
delim = "\n"
}
conversion += fmt.Sprintf(" \"%s\"%s", value, delim)
}
conversion += fmt.Sprintf(" ]%s", arrayDelim)
}
conversion += "}\n"
return conversion
}
func AsYAML(parsedSCSV KeyValuePairs) string {
return "YAML conversions are incorrect since SCSV v1.0.0, they will be fixed with v1.0.1"
conversion := ""
for key, values := range parsedSCSV {
conversion += fmt.Sprintf("%s:", key)
if len(values) <= 1 {
conversion += " []\n"
continue
} else {
conversion += "\n"
}
for _, value := range values {
conversion += fmt.Sprintf(" - %s\n", value)
}
}
return conversion
}