Skip to content

Commit

Permalink
chore: Replace String Constants with VssDataKey Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Mar 22, 2024
1 parent 2a86551 commit 60fc507
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,24 @@
package org.eclipse.kuksa.vssprocessor.parser

const val ROOT_KEY_VEHICLE = "Vehicle"
const val KEY_CHILDREN = "children"

const val KEY_DATA_DESCRIPTION = "description"
const val KEY_DATA_TYPE = "type"
const val KEY_DATA_UUID = "uuid"
const val KEY_DATA_COMMENT = "comment"
const val KEY_DATA_DATATYPE = "datatype"
const val KEY_DATA_UNIT = "unit"
const val KEY_DATA_MIN = "min"
const val KEY_DATA_MAX = "max"
const val KEY_DATA_CHILDREN = "children"
enum class VssDataKey {
UUID,
TYPE,
DESCRIPTION,
COMMENT,
DATATYPE,
UNIT,
MIN,
MAX,
;

val VSS_DATA_KEYS = listOf(
KEY_DATA_DESCRIPTION,
KEY_DATA_TYPE,
KEY_DATA_UUID,
KEY_DATA_COMMENT,
KEY_DATA_UNIT,
KEY_DATA_DATATYPE,
KEY_DATA_MIN,
KEY_DATA_MAX,
KEY_DATA_CHILDREN,
)
val key = name.lowercase()

companion object {
fun findByKey(key: String): VssDataKey? {
return entries.find { it.key == key }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ package org.eclipse.kuksa.vssprocessor.parser.json
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParseException
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_CHILDREN
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_COMMENT
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_DATATYPE
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_DESCRIPTION
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_MAX
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_MIN
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_TYPE
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_UNIT
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_UUID
import org.eclipse.kuksa.vssprocessor.parser.KEY_CHILDREN
import org.eclipse.kuksa.vssprocessor.parser.ROOT_KEY_VEHICLE
import org.eclipse.kuksa.vssprocessor.parser.VSS_DATA_KEYS
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.COMMENT
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.DATATYPE
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.DESCRIPTION
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.MAX
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.MIN
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.TYPE
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.UNIT
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.UUID
import org.eclipse.kuksa.vssprocessor.parser.VssParser
import org.eclipse.kuksa.vssprocessor.parser.json.extension.get
import org.eclipse.kuksa.vssprocessor.spec.VssNodePropertiesBuilder
import org.eclipse.kuksa.vssprocessor.spec.VssNodeSpecModel
import java.io.File
Expand Down Expand Up @@ -72,11 +73,12 @@ internal class JsonVssParser : VssParser {
val parsedSpecModel = parseSpecModel(vssPath, jsonObject)
parsedSpecModels += parsedSpecModel

if (jsonObject.has(KEY_DATA_CHILDREN)) {
val childrenJsonElement = jsonObject.getAsJsonObject(KEY_DATA_CHILDREN)
if (jsonObject.has(KEY_CHILDREN)) {
val childrenJsonElement = jsonObject.getAsJsonObject(KEY_CHILDREN)

val filteredKeys = childrenJsonElement.asMap().keys
.filter { key -> !VSS_DATA_KEYS.contains(key) }
.filter { key -> key != KEY_CHILDREN }
.filter { key -> VssDataKey.findByKey(key) == null }

filteredKeys.forEach { key ->
val childJsonElement = childrenJsonElement.getAsJsonObject(key)
Expand All @@ -93,18 +95,18 @@ internal class JsonVssParser : VssParser {
vssPath: String,
jsonObject: JsonObject,
): VssNodeSpecModel {
val uuid = jsonObject.get(KEY_DATA_UUID)?.asString
?: throw JsonParseException("Could not parse '$KEY_DATA_UUID' for '$vssPath'")

val type = jsonObject.get(KEY_DATA_TYPE)?.asString
?: throw JsonParseException("Could not parse '$KEY_DATA_TYPE' for '$vssPath'")

val description = jsonObject.get(KEY_DATA_DESCRIPTION)?.asString ?: ""
val datatype = jsonObject.get(KEY_DATA_DATATYPE)?.asString ?: ""
val comment = jsonObject.get(KEY_DATA_COMMENT)?.asString ?: ""
val unit = jsonObject.get(KEY_DATA_UNIT)?.asString ?: ""
val min = jsonObject.get(KEY_DATA_MIN)?.asString ?: ""
val max = jsonObject.get(KEY_DATA_MAX)?.asString ?: ""
val uuid = jsonObject.get(UUID)?.asString
?: throw JsonParseException("Could not parse '${UUID.key}' for '$vssPath'")

val type = jsonObject.get(TYPE)?.asString
?: throw JsonParseException("Could not parse '${TYPE.key}' for '$vssPath'")

val description = jsonObject.get(DESCRIPTION)?.asString ?: ""
val datatype = jsonObject.get(DATATYPE)?.asString ?: ""
val comment = jsonObject.get(COMMENT)?.asString ?: ""
val unit = jsonObject.get(UNIT)?.asString ?: ""
val min = jsonObject.get(MIN)?.asString ?: ""
val max = jsonObject.get(MAX)?.asString ?: ""

val vssNodeProperties = VssNodePropertiesBuilder(uuid, type)
.withDescription(description)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 - 2024 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/

package org.eclipse.kuksa.vssprocessor.parser.json.extension

import com.google.gson.JsonElement
import com.google.gson.JsonObject
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey

internal fun JsonObject.has(vssDataKey: VssDataKey): Boolean {
return has(vssDataKey.key)
}

internal fun JsonObject.getAsJsonObject(vssDataKey: VssDataKey): JsonObject {
return getAsJsonObject(vssDataKey.key)
}

internal fun JsonObject.get(vssDataKey: VssDataKey): JsonElement? {
return get(vssDataKey.key)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
package org.eclipse.kuksa.vssprocessor.parser.yaml

import org.eclipse.kuksa.vssprocessor.parser.FileParseException
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_COMMENT
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_DATATYPE
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_DESCRIPTION
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_MAX
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_MIN
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_TYPE
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_UNIT
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_UUID
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.COMMENT
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.DATATYPE
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.DESCRIPTION
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.MAX
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.MIN
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.TYPE
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.UNIT
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.UUID
import org.eclipse.kuksa.vssprocessor.parser.VssParser
import org.eclipse.kuksa.vssprocessor.spec.VssNodePropertiesBuilder
import org.eclipse.kuksa.vssprocessor.spec.VssNodeSpecModel
Expand Down Expand Up @@ -81,20 +82,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

val uuid = fetchValue(KEY_DATA_UUID, yamlElementJoined, delimiter).ifEmpty {
throw FileParseException("Could not parse '$KEY_DATA_UUID' for '$vssPath'")
val uuid = fetchValue(UUID, yamlElementJoined, delimiter).ifEmpty {
throw FileParseException("Could not parse '${UUID.key}' for '$vssPath'")
}

val type = fetchValue(KEY_DATA_TYPE, yamlElementJoined, delimiter).ifEmpty {
throw FileParseException("Could not parse '$KEY_DATA_TYPE' for '$vssPath'")
val type = fetchValue(TYPE, yamlElementJoined, delimiter).ifEmpty {
throw FileParseException("Could not parse '${TYPE.key}' 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(DESCRIPTION, yamlElementJoined, delimiter)
val comment = fetchValue(COMMENT, yamlElementJoined, delimiter)
val datatype = fetchValue(DATATYPE, yamlElementJoined, delimiter)
val unit = fetchValue(UNIT, yamlElementJoined, delimiter)
val min = fetchValue(MIN, yamlElementJoined, delimiter)
val max = fetchValue(MAX, yamlElementJoined, delimiter)

val vssNodeProperties = VssNodePropertiesBuilder(uuid, type)
.withDescription(description)
Expand All @@ -110,12 +111,12 @@ internal class YamlVssParser(private val elementDelimiter: String = "") : VssPar
}

private fun fetchValue(
nodeName: String,
dataKey: VssDataKey,
yamlElementJoined: String,
delimiter: Char,
): String {
// Also parse the delimiter to not confuse type != datatype
return yamlElementJoined
.substringAfter("$delimiter$nodeName: ")
.substringAfter("$delimiter${dataKey.key}: ")
.substringBefore(delimiter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,45 @@

package org.eclipse.kuksa.vssprocessor.spec

import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_COMMENT
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_DATATYPE
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_DESCRIPTION
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_MAX
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_MIN
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_TYPE
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_UNIT
import org.eclipse.kuksa.vssprocessor.parser.KEY_DATA_UUID
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.COMMENT
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.DATATYPE
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.DESCRIPTION
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.MAX
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.MIN
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.TYPE
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.UNIT
import org.eclipse.kuksa.vssprocessor.parser.VssDataKey.UUID
import kotlin.reflect.KClass

internal class VssNodePropertiesBuilder(
uuid: String,
type: String,
) {
private val nodePropertyMap: MutableMap<String, VssNodeProperty> = mutableMapOf()
private val nodePropertyMap: MutableMap<VssDataKey, VssNodeProperty> = mutableMapOf()

init {
val uuidNodeProperty = VssNodeProperty(KEY_DATA_UUID, uuid, String::class)
nodePropertyMap[KEY_DATA_UUID] = uuidNodeProperty
val uuidNodeProperty = VssNodeProperty(UUID, uuid, String::class)
nodePropertyMap[UUID] = uuidNodeProperty

val typeNodeProperty = VssNodeProperty(KEY_DATA_TYPE, type, String::class)
nodePropertyMap[KEY_DATA_TYPE] = typeNodeProperty
val typeNodeProperty = VssNodeProperty(TYPE, type, String::class)
nodePropertyMap[TYPE] = typeNodeProperty
}

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

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

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
val nodeProperty = VssNodeProperty(COMMENT, comment, String::class)
nodePropertyMap[COMMENT] = nodeProperty

return this
}
Expand All @@ -66,26 +67,26 @@ internal class VssNodePropertiesBuilder(

val valueDataType = findKClass(dataType)

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

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
val signalProperty = VssSignalProperty(UNIT, unit, String::class)
nodePropertyMap[UNIT] = signalProperty

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
val signalProperty = VssSignalProperty(MIN, min, clazz)
nodePropertyMap[MIN] = signalProperty

return this
}
Expand All @@ -101,8 +102,8 @@ internal class VssNodePropertiesBuilder(
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
val maxSignalProperty = VssSignalProperty(MAX, max, clazz)
nodePropertyMap[MAX] = maxSignalProperty

return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,30 @@

package org.eclipse.kuksa.vssprocessor.spec

import org.eclipse.kuksa.vssprocessor.parser.VssDataKey
import kotlin.reflect.KClass

internal open class VssNodeProperty(
val nodePropertyName: String,
val nodePropertyValue: String,
val dataKey: VssDataKey,
val value: String,
val dataType: KClass<*>,
) {
open val isCommon: Boolean = true

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as VssNodeProperty

return nodePropertyName == other.nodePropertyName
return dataKey == other.dataKey
}

override fun hashCode(): Int {
return nodePropertyName.hashCode()
return dataKey.hashCode()
}
}

internal class VssSignalProperty(
nodePropertyName: String,
nodePropertyValue: String,
dataKey: VssDataKey,
value: String,
dataType: KClass<*>,
) : VssNodeProperty(nodePropertyName, nodePropertyValue, dataType) {
override val isCommon: Boolean = false
}
) : VssNodeProperty(dataKey, value, dataType)
Loading

0 comments on commit 60fc507

Please sign in to comment.