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
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.9.0] - Not released yet

* Support for map collection type

## [0.8.0] - December 7th 2021

* Auto generation for additional types:
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
* Map (_SNAPSHOT_)
* Set

_Can't find your data type? Feel free to create a pull request or open an issue_ :parachute:
Expand Down
22 changes: 22 additions & 0 deletions forge-core/src/main/kotlin/io/github/hellocuriosity/ModelForge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ open class ModelForge {
field.isAccessible = true
when (field.type) {
List::class.java -> field.set(model, field.getValues())
Map::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 Down Expand Up @@ -89,6 +90,26 @@ open class ModelForge {
return list
}

/**
* Creates an automatically generated model map with
* a specified size.
*
* @param type ParameterizedType to instantiate
* @param size Int number of models to create
*
* @return Instance of clazz
*/
private fun buildMap(type: ParameterizedType, size: Int): Map<Any, Any> {
val key = type.actualTypeArguments[0] as Class<*>
val value = type.actualTypeArguments[1] as Class<*>

val map = (0 until size).map {
build(key) to build(value)
}.toMap()

return map
}

/**
* Creates an automatically generated model set with
* or without a specified size.
Expand Down Expand Up @@ -173,6 +194,7 @@ open class ModelForge {

return when (this.type) {
List::class.java -> buildList(clazz, size)
Map::class.java -> buildMap(type, size)
Set::class.java -> buildSet(clazz, size)
else -> throw ModelForgeException("Could not create values for: ${clazz.name}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ data class TestObject(
val listOptional: List<ComplexObject>?,
val longValue: Long,
val longOptional: Long?,
val mapValues: Map<String, ComplexObject>,
val mapOptional: Map<String, ComplexObject>?,
val setValues: Set<ComplexObject>,
val setOptional: Set<ComplexObject>?,
val shortValue: Short,
Expand Down Expand Up @@ -114,6 +116,10 @@ fun TestObject.assert() {
assertEquals(10, listValues.size)
assertNotNull(listOptional)
assertEquals(10, listOptional.size)
assertNotNull(mapValues)
assertTrue(mapValues.size in 0 until 11)
assertNotNull(mapOptional)
assertTrue(mapOptional.size in 0 until 11)
assertNotNull(setValues)
assertEquals(10, setValues.size)
assertNotNull(setOptional)
Expand Down