aboutsummaryrefslogtreecommitdiff
path: root/utils/mappers/mappers.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/mappers/mappers.go')
-rw-r--r--utils/mappers/mappers.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/utils/mappers/mappers.go b/utils/mappers/mappers.go
new file mode 100644
index 0000000..d03150d
--- /dev/null
+++ b/utils/mappers/mappers.go
@@ -0,0 +1,47 @@
+package mappers
+
+import "strconv"
+
+func ForceString(s any) string {
+ switch v := s.(type) {
+ case string:
+ return v
+ case int:
+ return strconv.Itoa(v)
+ case int64:
+ return strconv.FormatInt(v, 10)
+ case float64:
+ return strconv.FormatFloat(v, 'f', -1, 64)
+ case bool:
+ if v {
+ return "true"
+ }
+ return "false"
+ default:
+ return ""
+ }
+}
+
+func ForceInt(s any) int {
+ switch v := s.(type) {
+ case string:
+ i, err := strconv.Atoi(v)
+ if err != nil {
+ return 0
+ }
+ return i
+ case int:
+ return v
+ case int64:
+ return int(v)
+ case float64:
+ return int(v)
+ case bool:
+ if v {
+ return 1
+ }
+ return 0
+ default:
+ return 0
+ }
+}