This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
122 lines (103 loc) · 2.99 KB
/
utils.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
package skeleton
import (
"database/sql"
"github.com/google/uuid"
"strconv"
"strings"
"time"
)
// StandardizeUSDecimalValue removes all thousands-seperators and normalizes to the form 123123.00 having the "." the decimal separator
func StandardizeUSDecimalValue(in string) string {
in = strings.ReplaceAll(in, ",", "") // remove thousdand-separator
if strings.HasPrefix(in, ".") {
in = "0" + in
}
in = strings.TrimSuffix(in, ".")
return in
}
// StandardizeUSDecimalValue removes all thousands-seperators and normalizes to the form 123123.00 having the "." the decimal separator
func StandardizeEUDecimalValue(in string) string {
in = strings.ReplaceAll(in, ".", "") // remove thousdand-separator
if strings.Count(in, ",") == 1 { // if one decimal separator is included replace that with the ".", if there are more, this is an error of confusion
in = strings.ReplaceAll(in, ",", ".")
}
if strings.HasPrefix(in, ".") {
in = "0" + in
}
in = strings.TrimSuffix(in, ".")
return in
}
// LookupResultMapping - Based on the Resultmappings (made in UI) translate a reulst value to its expected value
// by example "+" -> "pos" or "?" -> "inv" etc...
func LookupResultMapping(analyteMapping AnalyteMapping, valueFromInstrument string) string {
for _, resultMappedKeyValue := range analyteMapping.ResultMappings {
if resultMappedKeyValue.Key == valueFromInstrument {
return resultMappedKeyValue.Value
}
}
return valueFromInstrument
}
func stringPointerToString(value *string) string {
if value != nil {
return *value
}
return ""
}
func nullStringToString(value sql.NullString) string {
if value.Valid {
return value.String
}
return ""
}
func nullStringToStringPointer(value sql.NullString) *string {
if value.Valid {
return &value.String
}
return nil
}
func nullUUIDToUUIDPointer(value uuid.NullUUID) *uuid.UUID {
if value.Valid {
return &value.UUID
}
return nil
}
func nullTimeToTimePointer(value sql.NullTime) *time.Time {
if value.Valid {
return &value.Time
}
return nil
}
func timePointerToNullTime(value *time.Time) sql.NullTime {
if value != nil {
return sql.NullTime{
Time: *value,
Valid: true,
}
}
return sql.NullTime{}
}
func isSorted(pageable Pageable) bool {
return pageable.Sort != ""
}
func applyPagination(pageable Pageable, tableAlias, defaultSort string) string {
var paginationQueryPart string
if isSorted(pageable) {
var tableAliasWithDot = ""
if tableAlias != "" {
tableAliasWithDot = tableAlias + "."
}
paginationQueryPart += " ORDER BY " + tableAliasWithDot + pageable.Sort
if pageable.Direction != SortNone {
paginationQueryPart += " " + strings.ToUpper(pageable.Direction.String())
}
} else if defaultSort != "" && pageable.IsPaged() {
paginationQueryPart += " ORDER BY " + defaultSort
}
if pageable.IsPaged() {
paginationQueryPart += " LIMIT " + strconv.Itoa(pageable.PageSize)
if pageable.Page > 0 {
paginationQueryPart += " OFFSET " + strconv.Itoa(pageable.PageSize*pageable.Page)
}
}
return paginationQueryPart
}