Skip to content

Commit e6af3dc

Browse files
author
yevhenii-nadtochii
committed
Polish RequiredPolicy class
1 parent 0e15270 commit e6af3dc

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

model/src/main/kotlin/io/spine/validation/required/RequiredPolicy.kt

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,47 @@ internal class RequiredPolicy : Policy<FieldOptionDiscovered>() {
7878
val field = event.subject
7979
val file = event.file
8080
checkFieldType(field, file)
81-
val errorMessage = determineErrorMessage(field)
82-
return if (event.option.boolValue) {
83-
discovered(field, errorMessage).asA()
84-
} else {
85-
ignore()
81+
82+
if (!event.option.boolValue) {
83+
return ignore()
8684
}
85+
86+
val message = determineErrorMessage(field)
87+
return requiredFieldDiscovered {
88+
id = fieldId {
89+
type = field.declaringType
90+
name = field.name
91+
}
92+
errorMessage = message
93+
subject = field
94+
}.asA()
8795
}
8896
}
8997

90-
private fun discovered(field: Field, message: String): RequiredFieldDiscovered =
91-
requiredFieldDiscovered {
92-
id = fieldId {
93-
type = field.declaringType
94-
name = field.name
95-
}
96-
errorMessage = message
97-
subject = field
98-
}
9998

10099
private fun checkFieldType(field: Field, file: File) {
101100
val type = field.type
102101
if (type.isPrimitive && type.primitive !in SUPPORTED_PRIMITIVES) {
103-
file.compilationError(field.span) {
102+
compilationError(file, field.span) {
104103
"The field `${field.qualifiedName}` of the type `${field.type}` does not " +
105104
"support `($REQUIRED)` option."
106105
}
107106
}
108107
}
109108

109+
private val SUPPORTED_PRIMITIVES = listOf(
110+
TYPE_STRING, TYPE_BYTES
111+
)
112+
113+
private fun compilationError(file: File, span: Span, message: () -> String): Nothing =
114+
Compilation.error(
115+
file.toPath().toFile(),
116+
span.startLine, span.startColumn,
117+
message()
118+
)
119+
110120
// TODO:2025-01-31:yevhenii.nadtochii: Locally changed ProtoData.
111-
// `Field.optionList` is empty when it is part of `FieldOptionDiscovered` event.
112-
// TODO:2025-02-03:yevhenii.nadtochii: No need to check the number of declarations.
121+
// `Field.optionList` is empty when it is payload of `FieldOptionDiscovered` event.
113122
private fun determineErrorMessage(field: Field): String {
114123
val companion = field.optionList.find { it.name == IF_MISSING }
115124
return if (companion == null) {
@@ -119,14 +128,3 @@ private fun determineErrorMessage(field: Field): String {
119128
.errorMsg
120129
}
121130
}
122-
123-
private fun File.compilationError(span: Span, message: () -> String): Nothing =
124-
Compilation.error(
125-
toPath().toFile(),
126-
span.startLine, span.startColumn,
127-
message()
128-
)
129-
130-
private val SUPPORTED_PRIMITIVES = listOf(
131-
TYPE_STRING, TYPE_BYTES
132-
)

0 commit comments

Comments
 (0)