-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.go
More file actions
101 lines (85 loc) · 2.15 KB
/
Copy pathutilities.go
File metadata and controls
101 lines (85 loc) · 2.15 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package formvalidator
import (
"bytes"
"encoding/csv"
"fmt"
)
/*
Custom error type
Str can be accessed to do translation, it has the raw data so Sprintf flags are included (%d,%s,etc.)
Example:
var e = FormError{"Integer must be between %d and %d", []interface{}{5, 10}}
i18n(e.Str, e.Data) -> "Escriba Usted un valor entre 5 y 10"
*/
type FormError struct {
Str string
Data []interface{}
}
func (e *FormError) Error() string {
if len(e.Data) > 0 { // if 'Data' is not empty format the string
return fmt.Sprintf(e.Str, e.Data...)
}
return e.Str
}
// appendError(err, errors...) do not forget the dots for slices
func appendError(err []FormError, arg ...*FormError) []FormError {
for _, e := range arg {
err = append(err, *e)
}
return err
}
// Is some value in the slice? (string type only)
func inSlice(slice []string, val string) bool {
for _, j := range slice {
if j == val {
return true
}
}
return false
}
// check for duplicate entries in a slice
func hasDuplicates(slice []string) bool {
encounteredItems := make(map[string]bool)
for _, val := range slice {
// new entry, add it to the found list
if _, found := encounteredItems[val]; !found {
encounteredItems[val] = true
} else {
// already has item set in map, duplicate found
return true
}
}
// no duplicates found
return false
}
// Compare two slices and return the difference (string types only for slices)
func sliceDiff(slice1, slice2 []string) (difference []string) {
for _, j := range slice1 {
if !inSlice(slice2, j) {
difference = append(difference, j)
}
}
return
}
// some validator rules load data from a csv file
func mustFormatCSVBytes(b []byte) []string {
buf := bytes.NewBuffer(b)
r := csv.NewReader(buf)
var record [][]string
var err error
if record, err = r.ReadAll(); err != nil {
panic(err.Error())
}
var list []string
if len(record) == 1 { // the file should have only one line, the multi-dimensional slice is for rows and columns, see docs on "encoding/csv"
list = record[0]
}
return list
}
// get the first key from a slice, []string
func getFirstKey(values []string) string {
if len(values) > 0 {
return values[0]
}
return ""
}