@@ -78,38 +78,47 @@ internal class RequiredPolicy : Policy<FieldOptionDiscovered>() {
78
78
val field = event.subject
79
79
val file = event.file
80
80
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()
86
84
}
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()
87
95
}
88
96
}
89
97
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
- }
99
98
100
99
private fun checkFieldType (field : Field , file : File ) {
101
100
val type = field.type
102
101
if (type.isPrimitive && type.primitive !in SUPPORTED_PRIMITIVES ) {
103
- file. compilationError(field.span) {
102
+ compilationError(file, field.span) {
104
103
" The field `${field.qualifiedName} ` of the type `${field.type} ` does not " +
105
104
" support `($REQUIRED )` option."
106
105
}
107
106
}
108
107
}
109
108
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
+
110
120
// 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.
113
122
private fun determineErrorMessage (field : Field ): String {
114
123
val companion = field.optionList.find { it.name == IF_MISSING }
115
124
return if (companion == null ) {
@@ -119,14 +128,3 @@ private fun determineErrorMessage(field: Field): String {
119
128
.errorMsg
120
129
}
121
130
}
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