Skip to content

Commit

Permalink
chore: Make Builder Parameters NonNull
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Mar 22, 2024
1 parent deb3b95 commit 2a86551
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,20 @@ internal class YamlVssParser(private val elementDelimiter: String = "") : VssPar
.substringAfter(delimiter) // Remove vssPath (already parsed)
.prependIndent(delimiter.toString()) // So the parsing is consistent for the first element

// The VSSPath is an exception because it is parsed from the top level name.

// Parse (example: "description: Antilock Braking System signals.") into name + value for all .yaml lines
val uuid = fetchValue(KEY_DATA_UUID, yamlElementJoined, delimiter)
?: throw FileParseException("Could not parse '$KEY_DATA_UUID' for '$vssPath'")
val uuid = fetchValue(KEY_DATA_UUID, yamlElementJoined, delimiter).ifEmpty {
throw FileParseException("Could not parse '$KEY_DATA_UUID' for '$vssPath'")
}

val type = fetchValue(KEY_DATA_TYPE, yamlElementJoined, delimiter)
?: throw FileParseException("Could not parse '$KEY_DATA_TYPE' for '$vssPath'")
val type = fetchValue(KEY_DATA_TYPE, yamlElementJoined, delimiter).ifEmpty {
throw FileParseException("Could not parse '$KEY_DATA_TYPE' for '$vssPath'")
}

val description = fetchValue(KEY_DATA_DESCRIPTION, yamlElementJoined, delimiter) ?: ""
val datatype = fetchValue(KEY_DATA_DATATYPE, yamlElementJoined, delimiter) ?: ""
val comment = fetchValue(KEY_DATA_COMMENT, yamlElementJoined, delimiter) ?: ""
val unit = fetchValue(KEY_DATA_UNIT, yamlElementJoined, delimiter) ?: ""
val min = fetchValue(KEY_DATA_MIN, yamlElementJoined, delimiter) ?: ""
val max = fetchValue(KEY_DATA_MAX, yamlElementJoined, delimiter) ?: ""
val description = fetchValue(KEY_DATA_DESCRIPTION, yamlElementJoined, delimiter)
val datatype = fetchValue(KEY_DATA_DATATYPE, yamlElementJoined, delimiter)
val comment = fetchValue(KEY_DATA_COMMENT, yamlElementJoined, delimiter)
val unit = fetchValue(KEY_DATA_UNIT, yamlElementJoined, delimiter)
val min = fetchValue(KEY_DATA_MIN, yamlElementJoined, delimiter)
val max = fetchValue(KEY_DATA_MAX, yamlElementJoined, delimiter)

val vssNodeProperties = VssNodePropertiesBuilder(uuid, type)
.withDescription(description)
Expand All @@ -114,11 +113,9 @@ private fun fetchValue(
nodeName: String,
yamlElementJoined: String,
delimiter: Char,
): String? {
): String {
// Also parse the delimiter to not confuse type != datatype
val value = yamlElementJoined
return yamlElementJoined
.substringAfter("$delimiter$nodeName: ")
.substringBefore(delimiter)

return value.ifEmpty { return null }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,81 +43,83 @@ internal class VssNodePropertiesBuilder(
nodePropertyMap[KEY_DATA_TYPE] = typeNodeProperty
}

fun withDescription(description: String?): VssNodePropertiesBuilder {
if (description == null) return this
fun withDescription(description: String): VssNodePropertiesBuilder {
if (description.isEmpty()) return this

val nodeProperty = VssNodeProperty(KEY_DATA_DESCRIPTION, description, String::class)
nodePropertyMap[KEY_DATA_DESCRIPTION] = nodeProperty

return this
}

fun withComment(comment: String?): VssNodePropertiesBuilder {
if (comment == null) return this
fun withComment(comment: String): VssNodePropertiesBuilder {
if (comment.isEmpty()) return this

val nodeProperty = VssNodeProperty(KEY_DATA_COMMENT, comment, String::class)
nodePropertyMap[KEY_DATA_COMMENT] = nodeProperty

return this
}

fun withDataType(dataType: String?): VssNodePropertiesBuilder {
if (dataType == null) return this
fun withDataType(dataType: String): VssNodePropertiesBuilder {
if (dataType.isEmpty()) return this

val vssDataType = VssDataType.find(dataType)
val valueDataType = vssDataType.valueDataType
val valueDataType = findKClass(dataType)

val signalProperty = VssSignalProperty(KEY_DATA_DATATYPE, dataType, valueDataType)
nodePropertyMap[KEY_DATA_DATATYPE] = signalProperty

return this
}

fun withUnit(unit: String?): VssNodePropertiesBuilder {
if (unit == null) return this
fun withUnit(unit: String): VssNodePropertiesBuilder {
if (unit.isEmpty()) return this

val signalProperty = VssSignalProperty(KEY_DATA_UNIT, unit, String::class)
nodePropertyMap[KEY_DATA_UNIT] = signalProperty

return this
}

fun withMin(min: String?, clazz: KClass<*>): VssNodePropertiesBuilder {
if (min == null) return this
fun withMin(min: String, clazz: KClass<*>): VssNodePropertiesBuilder {
if (min.isEmpty()) return this

val signalProperty = VssSignalProperty(KEY_DATA_MIN, min, clazz)
nodePropertyMap[KEY_DATA_MIN] = signalProperty

return this
}

fun withMin(min: String?, dataType: String): VssNodePropertiesBuilder {
if (min == null) return this
fun withMin(min: String, dataType: String): VssNodePropertiesBuilder {
if (min.isEmpty() || dataType.isEmpty()) return this

val vssDataType = VssDataType.find(dataType)
val valueDataType = vssDataType.valueDataType
val valueDataType = findKClass(dataType)

return withMin(min, valueDataType)
}

fun withMax(max: String?, clazz: KClass<*>): VssNodePropertiesBuilder {
if (max == null) return this
fun withMax(max: String, clazz: KClass<*>): VssNodePropertiesBuilder {
if (max.isEmpty()) return this

val maxSignalProperty = VssSignalProperty(KEY_DATA_MAX, max, clazz)
nodePropertyMap[KEY_DATA_MAX] = maxSignalProperty

return this
}

fun withMax(max: String?, dataType: String): VssNodePropertiesBuilder {
if (max == null) return this
fun withMax(max: String, dataType: String): VssNodePropertiesBuilder {
if (max.isEmpty() || dataType.isEmpty()) return this

val vssDataType = VssDataType.find(dataType)
val valueDataType = vssDataType.valueDataType
val valueDataType = findKClass(dataType)

return withMax(max, valueDataType)
}

private fun findKClass(dataType: String): KClass<*> {
val vssDataType = VssDataType.find(dataType)
return vssDataType.valueDataType
}

fun build(): Set<VssNodeProperty> {
return nodePropertyMap.values.toSet()
}
Expand Down

0 comments on commit 2a86551

Please sign in to comment.