-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDynamicConfig.kt
More file actions
141 lines (129 loc) · 5.09 KB
/
Copy pathDynamicConfig.kt
File metadata and controls
141 lines (129 loc) · 5.09 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
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 com.statsig.sdk
/**
* A helper class for interfacing with Dynamic Configs defined in the Statsig console
*/
class DynamicConfig(
val name: String,
val value: Map<String, Any>,
val ruleID: String? = null,
val groupName: String? = null,
val secondaryExposures: ArrayList<Map<String, String>> = arrayListOf(),
val evaluationDetails: EvaluationDetails? = null,
) {
companion object {
fun empty(name: String = ""): DynamicConfig {
return DynamicConfig(name, mapOf())
}
}
init { }
/**
* Gets a value from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a value from
* @param default the default value to return if the expected key does not exist in the config
* @return the value at the given key, or the default value if not found
*/
fun getString(key: String, default: String?): String? {
return when (val res = value[key]) {
is String -> res
else -> default
}
}
/**
* Gets a value from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a value from
* @param default the default value to return if the expected key does not exist in the config
* @return the value at the given key, or the default value if not found
*/
fun getBoolean(key: String, default: Boolean): Boolean {
return when (val res = value[key]) {
is Boolean -> res
else -> default
}
}
/**
* Gets a value from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a value from
* @param default the default value to return if the expected key does not exist in the config
* @return the value at the given key, or the default value if not found
*/
fun getDouble(key: String, default: Double): Double {
return when (val res = value[key]) {
is Number -> res.toDouble()
else -> default
}
}
/**
* Gets a value from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a value from
* @param default the default value to return if the expected key does not exist in the config
* @return the value at the given key, or the default value if not found
*/
fun getInt(key: String, default: Int): Int {
return when (val res = value[key]) {
is Number -> res.toInt()
else -> default
}
}
/**
* Gets a value from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a value from
* @param default the default value to return if the expected key does not exist in the config
* @return the value at the given key, or the default value if not found
*/
fun getLong(key: String, default: Long): Long {
return when (val res = value[key]) {
is Number -> res.toLong()
else -> default
}
}
/**
* Gets a value from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a value from
* @param default the default value to return if the expected key does not exist in the config
* @return the value at the given key, or the default value if not found
*/
fun getArray(key: String, default: Array<*>?): Array<*>? {
return when (val value = this.value[key]) {
is Array<*> -> value
is ArrayList<*> -> value.toTypedArray()
else -> default
}
}
/**
* Gets a dictionary from the config, falling back to the provided default value
* @param key the index within the DynamicConfig to fetch a dictionary from
* @param default the default value to return if the expected key does not exist in the config
* @return the value at the given key, or the default value if not found
*/
fun getDictionary(key: String, default: Map<String, Any>?): Map<String, Any>? {
return when (this.value[key]) {
is Map<*, *> -> this.value[key] as Map<String, Any>
else -> default
}
}
/**
* Gets a value from the config as a new DynamicConfig, or null if not found
* @param key the index within the DynamicConfig to fetch a value from
* @return the value at the given key as a DynamicConfig, or null
*/
fun getConfig(key: String): DynamicConfig? {
return when (this.value[key]) {
is Map<*, *> -> DynamicConfig(
key,
this.value[key] as Map<String, Any>,
this.ruleID,
this.groupName,
)
else -> null
}
}
fun getExposureMetadata(): String {
return Utils.PLAIN_GSON.toJson(
mapOf(
"config" to this.name,
"ruleID" to this.ruleID,
"secondaryExposures" to this.secondaryExposures,
),
)
}
}