generated from koddr/template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
141 lines (121 loc) Β· 3.56 KB
/
helpers.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
_ "embed"
"fmt"
"strconv"
"github.com/charmbracelet/lipgloss"
"github.com/koddr/gosl"
)
const (
// WikiPageURL link to project's Wiki page.
WikiPageURL string = "https://github.com/koddr/csv2api/wiki"
)
var (
//go:embed embed/config.yml
embedConfigYAMLFile []byte
//go:embed embed/data.csv
embedDataCSVFile []byte
// Flag for generate init config.
initConfig bool
// Flags for config, data file path, env prefix.
configFilePath, inputDataFilePath, envPrefix string
)
// removeDuplicates provides process to remove all duplicates in the given slice
// of slices, and return cleared slice ot slices.
func removeDuplicates(source [][]string) [][]string {
// Create a temp map and slice.
seen := make(map[string]bool, 0)
result := make([][]string, 0)
// Run loop for all slices in the given source.
for _, item := range source {
// Check, if current key is not in source slice.
if _, ok := seen[fmt.Sprint(item)]; !ok {
// Collect element into temp slice.
result = append(result, item)
// Set marker.
seen[fmt.Sprint(item)] = true
}
}
return result
}
// matchIndexes provides process to matching indexes for mapping columns.
func matchIndexes(wanted, source []string) map[string]int {
// Create a temp map.
indexes := make(map[string]int, 0)
// Loop for all strings in the given wanted slice.
for _, columnName := range wanted {
// Loop for all strings in the given source slice.
for sourceIndex, sourceName := range source {
// Check, if names of the source and wanted slices were match.
if sourceName == columnName {
// Collect index of the source slice to the temp map.
indexes[columnName] = sourceIndex
break
}
}
}
return indexes
}
// matchValues provides process to matching two values with a condition.
func matchValues(value1, value2, condition string) (bool, error) {
// Check condition.
switch condition {
case "EQ":
// Equals to the given value (==).
return value1 == value2, nil
case "NEQ":
// Not equal to the given value (!=).
return value1 != value2, nil
case "GT", "LT", "GTE", "LTE":
// Convert value1 from string to int, or error.
val1, err := strconv.Atoi(value1)
if err != nil {
return false, fmt.Errorf(
"wrong value '%s' to convert input value to type 'int' for this condition '%s'",
value1, condition,
)
}
// Convert value2 from string to int, or error.
val2, err := strconv.Atoi(value2)
if err != nil {
return false, fmt.Errorf(
"wrong value '%s' to convert filter value to type 'int' for this condition '%s'",
value2, condition,
)
}
// Check condition.
switch condition {
case "GT":
// Greater than the given value (>).
return val1 > val2, nil
case "LT":
// Less than the given value (<).
return val1 < val2, nil
case "GTE":
// Greater than or equal to the given value (>=).
return val1 >= val2, nil
case "LTE":
// Less than or equal to the given value (<=).
return val1 <= val2, nil
}
}
return false, fmt.Errorf("unknown condition: %s", condition)
}
// printStyled provides a beauty output for console.
func printStyled(s, style string) {
// Create a new blank style or the lipgloss.
lp := lipgloss.NewStyle()
// Switch between styles.
switch style {
case "margin-top-bottom":
fmt.Println(gosl.RenderStyled(s, lp.MarginTop(1).MarginBottom(1)))
case "margin-top":
fmt.Println(gosl.RenderStyled(s, lp.MarginTop(1)))
case "margin-bottom":
fmt.Println(gosl.RenderStyled(s, lp.MarginBottom(1)))
case "margin-left":
fmt.Println(gosl.RenderStyled(s, lp.MarginLeft(1)))
default:
fmt.Println(s)
}
}