Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
## [0.8.0] - Not released yet

Auto generation for additional types:

* Byte
* Char
* Short
* UUID

Dependency updates
* Auto generation for additional types:
* Byte
* Char
* Short
* UUID
* Support for Set collection type
* Dependency updates

## [0.7.4] - September 16th 2021

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Model Forge currently supports the auto generation for the following types:
### Collections

* List
* Set (snapshot)

_Can't find your data type? Feel free to create a pull request or open an issue_ :parachute:

Expand Down
36 changes: 29 additions & 7 deletions forge-core/src/main/kotlin/io/github/hellocuriosity/ModelForge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ open class ModelForge {
}.also { model ->
clazz.eligibleFields().map { field ->
field.isAccessible = true
if (field.type == List::class.java) {
field.set(model, field.getValues())
} else {
realBuild(field.type)?.also {
when (field.type) {
List::class.java -> field.set(model, field.getValues())
Set::class.java -> field.set(model, field.getValues())
else -> realBuild(field.type)?.also {
field.set(model, it)
}
}
Expand All @@ -89,6 +89,24 @@ open class ModelForge {
return list
}

/**
* Creates an automatically generated model set with
* or without a specified size.
*
* @param <T> Type to instantiate
* @param clazz Class to instantiate
* @param size Int number of models to create (defaulting to 10)
*
* @return Instance of clazz
*/
open fun <T : Any> buildSet(clazz: Class<T>, size: Int = 10): Set<T> {
val set = mutableSetOf<T>()
for (i in 0 until size) {
set.add(build(clazz))
}
return set
}

/**
* Adds a custom provider for generating models
*
Expand Down Expand Up @@ -142,17 +160,21 @@ open class ModelForge {
}

/**
* Auto generate list values
* Auto generate values
*
* @param Field Field needed to determine list type
* @param size Int number of items to create (defaulting to 10)
*
* @return Autogenerated list values
* @return Autogenerated values
*/
private fun Field.getValues(size: Int = 10): Any {
val type = genericType as ParameterizedType
val clazz = type.actualTypeArguments.first() as Class<*>

return buildList(clazz, size)
return when (this.type) {
List::class.java -> buildList(clazz, size)
Set::class.java -> buildSet(clazz, size)
else -> error("")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ data class TestObject(
val calendarOptional: Calendar?,
val charValue: Char,
val charOptional: Char?,
val complexObject: ComplexObject,
val complexOptional: ComplexObject?,
val dateValue: Date,
val dateOptional: Date?,
val doubleValue: Double,
Expand All @@ -34,18 +36,18 @@ data class TestObject(
val intValueOptional: Int?,
val instantValue: Instant,
val instantOptional: Instant?,
val listValues: List<ComplexObject>,
val listOptional: List<ComplexObject>?,
val longValue: Long,
val longOptional: Long?,
val setValues: Set<ComplexObject>,
val setOptional: Set<ComplexObject>?,
val shortValue: Short,
val shortOptional: Short?,
val stringValue: String,
val stringOptional: String?,
val uuidValue: UUID,
val uuidOptional: UUID?,
val listValues: List<ComplexObject>,
val listOptional: List<ComplexObject>?,
val complexObject: ComplexObject,
val complexOptional: ComplexObject?
)

enum class TestEnum {
Expand Down Expand Up @@ -88,6 +90,8 @@ fun TestObject.assert() {
assertNotNull(calendarOptional)
assertNotNull(charValue)
assertNotNull(charOptional)
assertNotNull(complexObject)
assertNotNull(complexOptional)
assertNotNull(dateValue)
assertNotNull(dateOptional)
assertNotNull(doubleValue)
Expand All @@ -110,6 +114,10 @@ fun TestObject.assert() {
assertEquals(10, listValues.size)
assertNotNull(listOptional)
assertEquals(10, listOptional.size)
assertNotNull(setValues)
assertEquals(10, setValues.size)
assertNotNull(setOptional)
assertEquals(10, setOptional.size)
assertNotNull(shortValue)
assertNotNull(shortOptional)
assertNotNull(stringValue)
Expand All @@ -118,8 +126,6 @@ fun TestObject.assert() {
assertTrue(stringOptional.isNotBlank())
assertNotNull(uuidValue)
assertNotNull(uuidOptional)
assertNotNull(complexObject)
assertNotNull(complexOptional)

// Validate populated values for ComplexObject
assertNotNull(complexObject.booleanValue)
Expand Down