Skip to content

Commit 31e26bb

Browse files
committed
Use builder pattern to create configuration
1 parent 80155f8 commit 31e26bb

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

json-schema-validator-objects/src/commonMain/kotlin/io/github/optimumcode/json/schema/wrappers/objects/Wrappers.kt

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,6 @@ import kotlin.jvm.JvmInline
1313
import kotlin.jvm.JvmName
1414
import kotlin.jvm.JvmOverloads
1515

16-
@ExperimentalApi
17-
public class WrappingConfiguration internal constructor(
18-
/**
19-
* If set to `false` an exception is thrown when wrapping a [Set].
20-
* If set to `true`, [Set] is wrapped the same way as [List]
21-
*/
22-
public val allowSets: Boolean = false,
23-
/**
24-
* If set to `false` the [Char] is converted to [String].
25-
* If set to `true` the [Char] is converted to a codepoint (and then to [Long])
26-
*/
27-
public val charAsCodepoint: Boolean = false,
28-
/**
29-
* If set to `true` the [ByteArray] is encoded using Base64 encoding and wrapped as a [PrimitiveElement].
30-
* Otherwise, the [ByteArray] is wrapped as an [ArrayElement].
31-
*/
32-
public val byteArrayAsBase64String: Boolean = true,
33-
)
34-
35-
@ExperimentalApi
36-
@JvmOverloads
37-
public fun wrappingConfiguration(
38-
allowSets: Boolean = false,
39-
charAsCodepoint: Boolean = false,
40-
byteArrayAsBase64String: Boolean = true,
41-
): WrappingConfiguration =
42-
WrappingConfiguration(
43-
allowSets = allowSets,
44-
charAsCodepoint = charAsCodepoint,
45-
byteArrayAsBase64String = byteArrayAsBase64String,
46-
)
47-
4816
/**
4917
* Returns an [AbstractElement] produced by converting the [obj] value.
5018
* The [configuration] allows conversion customization.
@@ -80,7 +48,7 @@ public fun wrappingConfiguration(
8048
@ExperimentalApi
8149
public fun wrapAsElement(
8250
obj: Any?,
83-
configuration: WrappingConfiguration = WrappingConfiguration(),
51+
configuration: WrappingConfiguration = WrappingConfiguration.create(),
8452
): AbstractElement {
8553
if (obj == null) {
8654
return NullWrapper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.github.optimumcode.json.schema.wrappers.objects
2+
3+
import io.github.optimumcode.json.schema.ExperimentalApi
4+
import kotlin.jvm.JvmOverloads
5+
import kotlin.jvm.JvmStatic
6+
7+
@ExperimentalApi
8+
public class WrappingConfiguration internal constructor(
9+
/**
10+
* If set to `false` an exception is thrown when wrapping a [Set].
11+
* If set to `true`, [Set] is wrapped the same way as [List]
12+
*/
13+
public val allowSets: Boolean,
14+
/**
15+
* If set to `false` the [Char] is converted to [String].
16+
* If set to `true` the [Char] is converted to a codepoint (and then to [Long])
17+
*/
18+
public val charAsCodepoint: Boolean,
19+
/**
20+
* If set to `true` the [ByteArray] is encoded using Base64 encoding and wrapped as a [io.github.optimumcode.json.schema.model.PrimitiveElement].
21+
* Otherwise, the [ByteArray] is wrapped as an [io.github.optimumcode.json.schema.model.ArrayElement].
22+
*/
23+
public val byteArrayAsBase64String: Boolean,
24+
) {
25+
public companion object {
26+
@ExperimentalApi
27+
@JvmStatic
28+
@JvmOverloads
29+
public fun create(configuration: WrappingConfigurationBuilder.() -> Unit = {}): WrappingConfiguration =
30+
WrappingConfigurationBuilder().apply(configuration).build()
31+
}
32+
}
33+
34+
@ExperimentalApi
35+
public class WrappingConfigurationBuilder internal constructor() {
36+
public var allowSets: Boolean = false
37+
public var charAsCodepoint: Boolean = false
38+
public var byteArrayAsBase64String: Boolean = true
39+
40+
internal fun build(): WrappingConfiguration {
41+
return WrappingConfiguration(
42+
allowSets = allowSets,
43+
charAsCodepoint = charAsCodepoint,
44+
byteArrayAsBase64String = byteArrayAsBase64String,
45+
)
46+
}
47+
}

json-schema-validator-objects/src/commonTest/kotlin/io/github/optimumcode/json/schema/wrappers/objects/WrappersTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class WrappersTest : FunSpec() {
149149
}
150150

151151
test("primitive wrapper for char as codepoint") {
152-
wrapAsElement('4', wrappingConfiguration(charAsCodepoint = true))
152+
wrapAsElement('4', WrappingConfiguration.create { charAsCodepoint = true })
153153
.shouldBeInstanceOf<PrimitiveElement> { el ->
154154
assertSoftly {
155155
"isString".asClue { el.isString.shouldBeFalse() }
@@ -220,9 +220,9 @@ class WrappersTest : FunSpec() {
220220
shouldNotThrowAny {
221221
wrapAsElement(
222222
setOf("a"),
223-
wrappingConfiguration(
224-
allowSets = true,
225-
),
223+
WrappingConfiguration.create {
224+
allowSets = true
225+
},
226226
)
227227
}
228228
element.shouldBeInstanceOf<ArrayElement> {
@@ -309,9 +309,9 @@ class WrappersTest : FunSpec() {
309309
test("byte array can be wrapped as an array element") {
310310
wrapAsElement(
311311
byteArrayOf(42),
312-
wrappingConfiguration(
313-
byteArrayAsBase64String = false,
314-
),
312+
WrappingConfiguration.create {
313+
byteArrayAsBase64String = false
314+
},
315315
).shouldBeInstanceOf<ArrayElement> {
316316
it.size shouldBe 1
317317
it.single().shouldBeInstanceOf<PrimitiveElement>()

0 commit comments

Comments
 (0)