Refactor retrieving DataType's name

This commit is contained in:
2020-03-14 13:18:01 +01:00
parent eb893c28f3
commit d8744670ed
13 changed files with 97 additions and 93 deletions

View File

@@ -18,7 +18,7 @@ object ActualSignatureFormatter {
DataType.MAP -> mapTypes(
argument.value as Map<Value, Value>
)
else -> argument.type.name.toLowerCase()
else -> argument.typeName
})
}
@@ -34,7 +34,7 @@ object ActualSignatureFormatter {
DataType.MAP -> mapTypes(
item.value as Map<Value, Value>
)
else -> item.type.name.toLowerCase()
else -> item.typeName
})
}
@@ -43,14 +43,14 @@ object ActualSignatureFormatter {
private fun mapTypes(map: Map<Value, Value>, output: MutableMap<String, String> = mutableMapOf()): String {
for ((k, v) in map) {
output[k.type.toString()] = when (v.type) {
output[k.typeName] = when (v.type) {
DataType.LIST -> listTypes(
v.value as List<Value>
)
DataType.MAP -> mapTypes(
v.value as Map<Value, Value>
)
else -> v.type.toString()
else -> v.typeName
}
}

View File

@@ -100,7 +100,7 @@ class Matcher(val type: DataType?, private val matcher: (Value) -> Boolean, priv
return Matcher(
DataType.LIST,
{ list -> (list.value as List<Value>).all { it.type in types } },
"list<${types.joinToString(", ") { it.name.toLowerCase() }}>"
"list<${types.joinToString(", ") { it.toString() }}>"
)
}
@@ -116,12 +116,12 @@ class Matcher(val type: DataType?, private val matcher: (Value) -> Boolean, priv
return Matcher(
null,
{ it.type in types },
"<${types.joinToString(", ") { it.name.toLowerCase() }}>"
"<${types.joinToString(", ") { it.toString() }}>"
)
}
fun ofType(type: DataType): Matcher {
return Matcher(null, { it.type == type }, type.name.toLowerCase())
return Matcher(null, { it.type == type }, type.toString())
}
fun oneOf(vararg matchers: Matcher): Matcher {

View File

@@ -5,87 +5,88 @@ import io.smnp.error.ShouldNeverReachThisLineException
import io.smnp.type.enumeration.DataType
data class Value(val type: DataType, val value: Any?, val properties: Map<String, Value> = emptyMap()) {
init {
if(!type.isInstance(value)) {
throw RuntimeException("'$value' is not of type $type")
}
}
init {
if (!type.isInstance(value)) {
throw RuntimeException("'$value' is not of type $type")
}
}
fun stringify() = type.stringifier(value)
fun stringify() = type.stringifier(value)
fun typeName() = type.toString()
val typeName: String
get() = type.toString()
override fun toString(): String {
return "$type($value)"
}
override fun toString(): String {
return "$type($value)"
}
companion object {
fun int(value: Int): Value {
return Value(DataType.INT, value)
}
companion object {
fun int(value: Int): Value {
return Value(DataType.INT, value)
}
fun float(value: Float): Value {
return Value(
DataType.FLOAT,
value
fun float(value: Float): Value {
return Value(
DataType.FLOAT,
value
)
}
fun numeric(value: Number): Value {
return when (value::class) {
Int::class -> int(value.toInt())
Float::class -> float(value.toFloat())
else -> throw ShouldNeverReachThisLineException()
}
}
fun string(value: String): Value {
return Value(
DataType.STRING, value, hashMapOf(
Pair("length", int(value.length))
)
}
)
}
fun numeric(value: Number): Value {
return when(value::class) {
Int::class -> int(value.toInt())
Float::class -> float(value.toFloat())
else -> throw ShouldNeverReachThisLineException()
}
}
fun string(value: String): Value {
return Value(
DataType.STRING, value, hashMapOf(
Pair("length", int(value.length))
)
fun list(value: List<Value>): Value {
return Value(
DataType.LIST, value, hashMapOf(
Pair("size", int(value.size))
)
}
)
}
fun list(value: List<Value>): Value {
return Value(
DataType.LIST, value, hashMapOf(
Pair("size", int(value.size))
)
fun map(value: Map<Value, Value>): Value {
return Value(
DataType.MAP, value, hashMapOf(
Pair("size", int(value.size)),
Pair("keys", list(value.keys.toList())),
Pair("values", list(value.values.toList()))
)
}
)
}
fun map(value: Map<Value, Value>): Value {
return Value(
DataType.MAP, value, hashMapOf(
Pair("size", int(value.size)),
Pair("keys", list(value.keys.toList())),
Pair("values", list(value.values.toList()))
)
fun note(value: Note): Value {
return Value(
DataType.NOTE, value, hashMapOf(
Pair("pitch", string(value.pitch.toString())),
Pair("octave", int(value.octave)),
Pair("duration", int(value.duration)),
Pair("dot", bool(value.dot))
)
}
)
}
fun note(value: Note): Value {
return Value(
DataType.NOTE, value, hashMapOf(
Pair("pitch", string(value.pitch.toString())),
Pair("octave", int(value.octave)),
Pair("duration", int(value.duration)),
Pair("dot", bool(value.dot))
)
)
}
fun bool(value: Boolean): Value {
return Value(DataType.BOOL, value)
}
fun bool(value: Boolean): Value {
return Value(DataType.BOOL, value)
}
fun type(value: DataType): Value {
return Value(DataType.TYPE, value)
}
fun type(value: DataType): Value {
return Value(DataType.TYPE, value)
}
fun void(): Value {
return Value(DataType.VOID, null)
}
}
fun void(): Value {
return Value(DataType.VOID, null)
}
}
}