Skip to content

Commit

Permalink
Update blueprint properties and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNatan committed Mar 17, 2023
1 parent b2d5fb2 commit 98e2623
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.typesafe.config.ConfigSyntax
import com.typesafe.config.ConfigValue
import com.typesafe.config.ConfigValueType
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
Expand Down Expand Up @@ -191,10 +190,7 @@ internal class BlueprintParser(private val supportedProperties: List<Property> =
return
}

private fun checkPropertyAndNodeKindEquality(
kind: KClass<out PropertyKind>,
nodeType: ConfigValueType
) {
private fun checkPropertyAndNodeKindEquality(kind: KClass<out PropertyKind>, nodeType: ConfigValueType) {
val targetNodeType = nodeValueTypeFromKind(kind)
if (nodeType != targetNodeType) {
error("Wrong value type. Expected: $targetNodeType, given: $nodeType")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ internal object NotBlankPropertyConstraint : PropertyConstraint {
check(value.isNotBlank()) { "${property.qualifiedName} cannot be blank" }
}
}

internal object EnvironmentVariableConstraint : PropertyConstraint {
override fun check(property: Property, actualKind: KClass<out PropertyKind>?, value: Any?) {
// TODO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.katan.service.blueprint.parser

import org.katan.service.blueprint.parser.PropertyKind.Literal
import org.katan.service.blueprint.parser.PropertyKind.Mixed
import org.katan.service.blueprint.parser.PropertyKind.Multiple
import org.katan.service.blueprint.parser.PropertyKind.Struct

internal object Properties {
val Name = Property(
qualifiedName = "name",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint, NotBlankPropertyConstraint)
)
val Version = Property(
qualifiedName = "version",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint, NotBlankPropertyConstraint)
)
val Build = Property(
qualifiedName = "build",
kind = Struct
)
val Entrypoint = Property(
qualifiedName = "build.entrypoint",
kind = Literal
)
val Image = Property(
qualifiedName = "build.image",
kind = Mixed(
Literal,
Multiple(Struct)
),
constraints = listOf(RequiredPropertyConstraint)
)
val ImageReference = Property(
qualifiedName = "build.image.ref",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint)
)
val ImageTag = Property(
qualifiedName = "build.image.tag",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint)
)
val Instance = Property(
qualifiedName = "build.instance",
kind = Struct
)
val InstanceName = Property(
qualifiedName = "build.instance.name",
kind = Literal
)
val Options = Property(
qualifiedName = "options",
kind = Struct
)
val OptionsId = Property(
qualifiedName = "options.id",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint)
)
val OptionsType = Property(
qualifiedName = "options.type",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint)
)
val OptionsEnv = Property(
qualifiedName = "options.env",
kind = Literal,
constraints = listOf(EnvironmentVariableConstraint)
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.katan.service.blueprint.parser

import org.katan.service.blueprint.parser.PropertyKind.Literal
import org.katan.service.blueprint.parser.PropertyKind.Mixed
import org.katan.service.blueprint.parser.PropertyKind.Multiple
import kotlin.reflect.KClass
Expand All @@ -13,7 +12,7 @@ internal data class Property(
val constraints: List<PropertyConstraint> = listOf()
) {

val name: String get() = qualifiedName.substringAfterLast(PROPERTY_NAME_SEPARATOR)
val nameStructure: List<String> get() = qualifiedName.split(PROPERTY_NAME_SEPARATOR)

fun supports(kind: KClass<out PropertyKind>): Boolean {
return supports(this.kind, kind)
Expand Down Expand Up @@ -52,44 +51,11 @@ internal val AllSupportedProperties: List<Property> = listOf(
Properties.Entrypoint,
Properties.Image,
Properties.ImageReference,
Properties.ImageTag
Properties.ImageTag,
Properties.Instance,
Properties.InstanceName,
Properties.Options,
Properties.OptionsId,
Properties.OptionsType,
Properties.OptionsEnv
)

internal object Properties {
val Name = Property(
qualifiedName = "name",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint, NotBlankPropertyConstraint)
)
val Version = Property(
qualifiedName = "version",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint, NotBlankPropertyConstraint)
)
val Build = Property(
qualifiedName = "build",
kind = PropertyKind.Struct
)
val Entrypoint = Property(
qualifiedName = "build.entrypoint",
kind = Literal
)
val Image = Property(
qualifiedName = "build.image",
kind = Mixed(
Literal,
Multiple(PropertyKind.Struct)
),
constraints = listOf(RequiredPropertyConstraint)
)
val ImageReference = Property(
qualifiedName = "build.image.ref",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint)
)
val ImageTag = Property(
qualifiedName = "build.image.tag",
kind = Literal,
constraints = listOf(RequiredPropertyConstraint)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class ParserTest {
)

assertFailsWith<NoMatchesForMixedProperty> {
withParserTest(listOf(property)) {
read(input)
}
withParserTest(input, property)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.katan.service.blueprint.parser

private val parser = BlueprintParser()
import kotlinx.serialization.json.JsonObject

internal inline fun <R> withParserTest(crossinline block: BlueprintParser.() -> R): R {
return block(parser)
return block(BlueprintParser())
}

internal inline fun <R> withParserTest(
Expand All @@ -12,3 +12,6 @@ internal inline fun <R> withParserTest(
): R {
return block(BlueprintParser(supportedProperties.toList()))
}

internal fun withParserTest(input: String, vararg supportedProperties: Property): JsonObject =
BlueprintParser(supportedProperties.toList()).read(input)
Loading

0 comments on commit 98e2623

Please sign in to comment.