-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Some cleanup + renaming
- Loading branch information
Showing
9 changed files
with
137 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
vss-processor/src/main/kotlin/org/eclipse/kuksa/vssprocessor/VssDefinitionParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.eclipse.kuksa.vssprocessor | ||
|
||
import org.eclipse.kuksa.vssprocessor.VssDefinitionProcessor.VssSpecificationElement | ||
import java.io.File | ||
import kotlin.reflect.KMutableProperty | ||
import kotlin.reflect.KProperty | ||
import kotlin.reflect.full.memberProperties | ||
|
||
internal interface VssDefinitionParser { | ||
/** | ||
* @param definitionFile to parse [VssSpecificationElement] with | ||
*/ | ||
fun parseSpecifications(definitionFile: File): List<VssSpecificationElement> | ||
} | ||
|
||
/** | ||
* @param fields to set via reflection. Pair<PropertyName, anyValue>. | ||
* @param remapNames which can be used if the propertyName does not match with the input name | ||
*/ | ||
fun VssSpecificationElement.setFields( | ||
fields: List<Pair<String, Any?>>, | ||
remapNames: Map<String, String> = emptyMap(), | ||
) { | ||
val nameToProperty = this::class.memberProperties.associateBy(KProperty<*>::name) | ||
|
||
val remappedFields = fields.toMutableList() | ||
remapNames.forEach { (propertyName, newName) -> | ||
val find = fields.find { it.first == propertyName } ?: return@forEach | ||
remappedFields.remove(find) | ||
remappedFields.add(Pair(find.first, newName)) | ||
} | ||
|
||
remappedFields.forEach { (propertyName, propertyValue) -> | ||
nameToProperty[propertyName] | ||
.takeIf { it is KMutableProperty<*> } | ||
?.let { it as KMutableProperty<*> } | ||
?.setter?.call(this, propertyValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
vss-processor/src/main/kotlin/org/eclipse/kuksa/vssprocessor/YamlDefinitionParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.eclipse.kuksa.vssprocessor | ||
|
||
import java.io.BufferedReader | ||
import java.io.File | ||
import java.io.InputStreamReader | ||
import kotlin.reflect.full.memberProperties | ||
|
||
internal class YamlDefinitionParser : VssDefinitionParser { | ||
override fun parseSpecifications(definitionFile: File): List<VssDefinitionProcessor.VssSpecificationElement> { | ||
val specificationElements = mutableListOf<VssDefinitionProcessor.VssSpecificationElement>() | ||
val vssDefinitionStream = definitionFile.inputStream() | ||
val bufferedReader = BufferedReader(InputStreamReader(vssDefinitionStream)) | ||
|
||
val yamlAttributes = mutableListOf<String>() | ||
while (bufferedReader.ready()) { | ||
val line = bufferedReader.readLine().trim() | ||
if (line.isEmpty()) { | ||
val specificationElement = parseYamlElement(yamlAttributes) | ||
specificationElements.add(specificationElement) | ||
|
||
yamlAttributes.clear() | ||
|
||
continue | ||
} | ||
|
||
yamlAttributes.add(line) | ||
} | ||
|
||
bufferedReader.close() | ||
vssDefinitionStream.close() | ||
|
||
return specificationElements | ||
} | ||
|
||
// Example .yaml element: | ||
// | ||
// Vehicle.ADAS.ABS: | ||
// description: Antilock Braking System signals. | ||
// type: branch | ||
// uuid: 219270ef27c4531f874bbda63743b330 | ||
private fun parseYamlElement(yamlElement: List<String>): VssDefinitionProcessor.VssSpecificationElement { | ||
val elementVssPath = yamlElement.first().substringBefore(":") | ||
|
||
val yamlElementJoined = yamlElement | ||
.joinToString(separator = ";") | ||
.substringAfter(";") // Remove vssPath (already parsed) | ||
.prependIndent(";") // So the parsing is consistent for the first element | ||
val members = VssDefinitionProcessor.VssSpecificationElement::class.memberProperties | ||
val fieldsToSet = mutableListOf<Pair<String, Any?>>() | ||
|
||
// The VSSPath is an exception because it is parsed from the top level name. | ||
val vssPathFieldInfo = Pair("vssPath", elementVssPath) | ||
fieldsToSet.add(vssPathFieldInfo) | ||
|
||
// Parse (example: "description: Antilock Braking System signals.") into name + value for all .yaml lines | ||
for (member in members) { | ||
val memberName = member.name | ||
if (!yamlElementJoined.contains(memberName)) continue | ||
|
||
val memberValue = yamlElementJoined | ||
.substringAfter(";$memberName: ") // Also parse "," to not confuse type != datatype | ||
.substringBefore(";") | ||
|
||
val fieldInfo = Pair(memberName, memberValue) | ||
fieldsToSet.add(fieldInfo) | ||
} | ||
|
||
val vssSpecificationMember = VssDefinitionProcessor.VssSpecificationElement() | ||
vssSpecificationMember.setFields(fieldsToSet) | ||
|
||
return vssSpecificationMember | ||
} | ||
} |