maestro.networkproxy.yaml.YamlMappingRuleParser.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maestro-network-proxy Show documentation
Show all versions of maestro-network-proxy Show documentation
Maestro is a server-driven platform-agnostic library that allows to drive tests for both iOS and Android using the same implementation through an intuitive API.
package maestro.networkproxy.yaml
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.KotlinModule
import java.nio.file.Path
import kotlin.io.path.isDirectory
import kotlin.io.path.pathString
object YamlMappingRuleParser {
private val YAML = YAMLFactory()
private val MAPPER = ObjectMapper(YAML).apply {
registerModule(KotlinModule.Builder().build())
}
fun readRules(dirOrFile: Path): List {
return if (dirOrFile.isDirectory()) {
dirOrFile.toAbsolutePath()
.toFile()
.walkTopDown()
.asSequence()
.filter { it.isFile && it.extension in setOf("yaml", "yml") }
.flatMap { readRulesFromFile(it.toPath()) }
.toList()
} else {
readRulesFromFile(dirOrFile.toAbsolutePath())
}
}
fun saveRules(rules: List, file: Path) {
MAPPER.writeValue(file.toFile(), rules)
}
private fun readRulesFromFile(file: Path): List {
val rules = MAPPER.readValue(
file.toFile(),
object : TypeReference>() {}
)
return rules.map {
it.copy(
ruleFilePath = file.toAbsolutePath().pathString
)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy