1
+ package com.jeluchu.core.extensions
2
+
3
+ import org.bson.Document
4
+ import java.text.SimpleDateFormat
5
+ import java.util.*
6
+
7
+ fun Document.getStringSafe (key : String , defaultValue : String = ""): String {
8
+ return try {
9
+ when (val value = this [key]) {
10
+ is String -> value
11
+ is Number , is Boolean -> value.toString()
12
+ else -> defaultValue
13
+ }
14
+ } catch (e: Exception ) {
15
+ defaultValue
16
+ }
17
+ }
18
+
19
+ fun Document.getIntSafe (key : String , defaultValue : Int = 0): Int {
20
+ return try {
21
+ when (val value = this [key]) {
22
+ is Int -> value
23
+ is Number -> value.toInt()
24
+ is String -> value.toIntOrNull() ? : defaultValue
25
+ else -> defaultValue
26
+ }
27
+ } catch (e: Exception ) {
28
+ defaultValue
29
+ }
30
+ }
31
+
32
+ fun Document.getDoubleSafe (key : String , defaultValue : Double = 0.0): Double {
33
+ return try {
34
+ when (val value = this [key]) {
35
+ is Double -> value
36
+ is Number -> value.toDouble()
37
+ is String -> value.toDoubleOrNull() ? : defaultValue
38
+ else -> defaultValue
39
+ }
40
+ } catch (e: Exception ) {
41
+ defaultValue
42
+ }
43
+ }
44
+
45
+ fun Document.getFloatSafe (key : String , defaultValue : Float = 0.0f): Float {
46
+ return try {
47
+ when (val value = this [key]) {
48
+ is Float -> value
49
+ is Number -> value.toFloat()
50
+ is String -> value.toFloatOrNull() ? : defaultValue
51
+ else -> defaultValue
52
+ }
53
+ } catch (e: Exception ) {
54
+ defaultValue
55
+ }
56
+ }
57
+
58
+ fun Document.getLongSafe (key : String , defaultValue : Long = 0L): Long {
59
+ return try {
60
+ val value = this .get(key)
61
+ when (value) {
62
+ is Long -> value
63
+ is Number -> value.toLong()
64
+ is String -> value.toLongOrNull() ? : defaultValue
65
+ else -> defaultValue
66
+ }
67
+ } catch (e: Exception ) {
68
+ defaultValue
69
+ }
70
+ }
71
+
72
+ fun Document.getBooleanSafe (key : String , defaultValue : Boolean = false): Boolean {
73
+ return try {
74
+ when (val value = this [key]) {
75
+ is Boolean -> value
76
+ is String -> value.toBoolean()
77
+ is Number -> value.toInt() != 0
78
+ else -> defaultValue
79
+ }
80
+ } catch (e: Exception ) {
81
+ defaultValue
82
+ }
83
+ }
84
+
85
+ fun Document.getDateSafe (key : String , defaultValue : Date = Date (0)): Date {
86
+ return try {
87
+ when (val value = this [key]) {
88
+ is Date -> value
89
+ is String -> SimpleDateFormat (" yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ).parse(value) ? : defaultValue
90
+ else -> defaultValue
91
+ }
92
+ } catch (e: Exception ) {
93
+ defaultValue
94
+ }
95
+ }
96
+
97
+ inline fun <reified T > Document.getListSafe (key : String , defaultValue : List <T > = emptyList()): List <T > {
98
+ return try {
99
+ when (val value = this [key]) {
100
+ is List <* > -> value.filterIsInstance<T >()
101
+ else -> defaultValue
102
+ }
103
+ } catch (e: Exception ) {
104
+ defaultValue
105
+ }
106
+ }
107
+
108
+ fun Document.getDocumentSafe (key : String ): Document ? {
109
+ return try {
110
+ val value = this [key]
111
+ if (value is Document ) value else null
112
+ } catch (e: Exception ) {
113
+ null
114
+ }
115
+ }
0 commit comments