Skip to content

Commit 0d4e8d5

Browse files
author
yevhenii-nadtochii
committed
Rename MessageValidation to CompiledMessage
1 parent ae1ac20 commit 0d4e8d5

File tree

8 files changed

+41
-49
lines changed

8 files changed

+41
-49
lines changed

java/src/main/kotlin/io/spine/validation/java/JavaValidationRenderer.kt

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import io.spine.validate.NonValidated
4242
import io.spine.validate.Validated
4343
import io.spine.validate.ValidationError
4444
import io.spine.validate.ValidationException
45-
import io.spine.validation.MessageValidation
45+
import io.spine.validation.CompiledMessage
4646
import io.spine.validation.java.ValidationCode.Companion.OPTIONAL_ERROR
4747
import io.spine.validation.java.ValidationCode.Companion.VALIDATE
4848
import io.spine.validation.java.point.BuildMethodReturnTypeAnnotation
@@ -84,18 +84,18 @@ public class JavaValidationRenderer : JavaRenderer() {
8484
return
8585
}
8686

87-
// `MessageValidation` is created for every message, no matter it has constraints or not.
88-
val validations = select(MessageValidation::class.java).all()
87+
val allCompiledMessages = select(CompiledMessage::class.java).all()
8988
.associateWith { sources.javaFileOf(it.type) }
9089

9190
// Adds `validate()` and `implements ValidatableMessage`.
92-
validations.forEach { (constraints, file) ->
93-
file.addValidationCode(constraints)
91+
allCompiledMessages.forEach { (message, file) ->
92+
val validationCode = ValidationCode(renderer = this, message, file)
93+
validationCode.generate()
9494
}
9595

9696
// Annotates `build()` and `buildPartial()` methods.
9797
// Adds invocation of `validate()` in `build()`.
98-
validations.values.distinct()
98+
allCompiledMessages.values.distinct()
9999
.forEach {
100100
// Though, it seems logical to do this along with adding `validate()`
101101
// in the `forEach` above; we cannot do that. Insertion points used
@@ -118,12 +118,6 @@ public class JavaValidationRenderer : JavaRenderer() {
118118
it.plugValidationIntoBuild()
119119
}
120120
}
121-
122-
private fun SourceFile<Java>.addValidationCode(validation: MessageValidation) {
123-
val rendered = this@JavaValidationRenderer
124-
val validationCode = ValidationCode(rendered, validation, this)
125-
validationCode.generate()
126-
}
127121
}
128122

129123
private fun SourceFile<Java>.annotateBuildMethod() {
@@ -154,14 +148,14 @@ private fun SourceFile<Java>.plugValidationIntoBuild() {
154148
}
155149

156150
/**
157-
* Java code to insert into [Message.Builder.build] method.
151+
* Java code to insert into the end of [Message.Builder.build] method.
158152
*
159-
* The generated code invokes `validate()` assuming there is a variable called `result`.
160-
* The variable type is the type of the validated message, holding the value of
161-
* the message to validate.
153+
* The generated code invokes `validate()` method. This code assumes there
154+
* is a variable called `result`. The variable type is the type of the validated message,
155+
* holding the instance of the message to validate.
162156
*
163-
* If the validation constraints do not pass, the generated code would throw
164-
* a [ValidationException][io.spine.validate.ValidationException].
157+
* If one or more validation constraints do not pass, the generated code throws
158+
* the [ValidationException][io.spine.validate.ValidationException].
165159
*/
166160
private fun validateBeforeBuild(): ImmutableList<String> = codeBlock {
167161
val result = ReadVar<Message>("result")

java/src/main/kotlin/io/spine/validation/java/ValidationCode.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ import io.spine.tools.code.Java
4141
import io.spine.validate.ConstraintViolation
4242
import io.spine.validate.ValidatableMessage
4343
import io.spine.validate.ValidationError
44-
import io.spine.validation.MessageValidation
44+
import io.spine.validation.CompiledMessage
4545
import java.lang.System.lineSeparator
4646
import java.lang.reflect.Type
4747
import java.util.*
4848

4949
/**
5050
* Generates validation code for a given message type specified via
51-
* [MessageValidation] instance.
51+
* [CompiledMessage] instance.
5252
*
5353
* Serves as a method object for the [JavaValidationRenderer] passed to the constructor.
5454
*/
5555
internal class ValidationCode(
5656
private val renderer: JavaValidationRenderer,
57-
private val validation: MessageValidation,
57+
private val message: CompiledMessage,
5858
private val sourceFile: SourceFile<Java>
5959
) {
60-
private val messageType: TypeName = validation.name
60+
private val messageType: TypeName = message.name
6161

6262
/**
6363
* Generates the code in the linked source file.
@@ -76,7 +76,7 @@ internal class ValidationCode(
7676

7777
private fun handleConstraints() {
7878
classScope().apply {
79-
val constraints = ValidationConstraintsCode.generate(renderer, validation)
79+
val constraints = ValidationConstraintsCode.generate(renderer, message)
8080
add(validateMethod(constraints.codeBlock()))
8181
add(constraints.supportingMembersCode())
8282
}

java/src/main/kotlin/io/spine/validation/java/ValidationConstraintsCode.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import com.squareup.javapoet.CodeBlock
3333
import io.spine.protodata.ast.File
3434
import io.spine.protodata.ast.TypeName
3535
import io.spine.protodata.java.This
36-
import io.spine.validation.MessageValidation
36+
import io.spine.validation.CompiledMessage
3737
import io.spine.validation.Rule
3838
import io.spine.validation.java.ValidationCode.Companion.VIOLATIONS
3939
import java.lang.System.lineSeparator
@@ -49,20 +49,20 @@ internal class ValidationConstraintsCode private constructor(
4949
private val renderer: JavaValidationRenderer,
5050

5151
/**
52-
* The validation rule for which to generate the code.
52+
* The compiled message to generate the code for.
5353
*/
54-
private val validation: MessageValidation
54+
private val message: CompiledMessage
5555
) {
5656

5757
/**
5858
* The name of the message type for which to generate the code.
5959
*/
60-
private val messageType: TypeName = validation.name
60+
private val messageType: TypeName = message.name
6161

6262
/**
6363
* The file which declares the message type.
6464
*/
65-
private val declaringFile: File = validation.type.file
65+
private val declaringFile: File = message.type.file
6666

6767
/**
6868
* The expression for referencing the message in the code.
@@ -96,7 +96,7 @@ internal class ValidationConstraintsCode private constructor(
9696
}
9797

9898
private fun generate() {
99-
for (rule in validation.ruleList) {
99+
for (rule in message.ruleList) {
100100
addRule(rule)
101101
}
102102
}
@@ -126,7 +126,7 @@ internal class ValidationConstraintsCode private constructor(
126126
/**
127127
* Creates a new instance with the generated validation constraints code.
128128
*/
129-
fun generate(r: JavaValidationRenderer, v: MessageValidation): ValidationConstraintsCode {
129+
fun generate(r: JavaValidationRenderer, v: CompiledMessage): ValidationConstraintsCode {
130130
val result = ValidationConstraintsCode(r, v)
131131
result.generate()
132132
return result

java/src/main/kotlin/io/spine/validation/java/point/PrintValidationInsertionPoints.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import io.spine.tools.code.Java
3232
/**
3333
* An [InsertionPointPrinter] which adds the [ValidateBeforeReturn] point
3434
* to all the message types which have an associated
35-
* [MessageValidation][io.spine.validation.MessageValidation] view.
35+
* [CompiledMessage][io.spine.validation.CompiledMessage] view.
3636
*/
3737
@Suppress("unused") // Accessed via reflection by ProtoData API.
3838
public class PrintValidationInsertionPoints : InsertionPointPrinter<Java>(

model/src/main/kotlin/io/spine/validation/MessageValidationRepository.kt renamed to model/src/main/kotlin/io/spine/validation/CompiledMessageRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ import io.spine.protodata.plugin.ViewRepository
3232
import io.spine.server.route.EventRouting
3333

3434
/**
35-
* A repository for the [MessageValidationView].
35+
* A repository for the [CompiledMessageView].
3636
*
3737
* Routes the [TypeDiscovered] events to the view by the type name.
3838
*/
39-
internal class MessageValidationRepository :
40-
ViewRepository<TypeName, MessageValidationView, MessageValidation>() {
39+
internal class CompiledMessageRepository :
40+
ViewRepository<TypeName, CompiledMessageView, CompiledMessage>() {
4141

4242
override fun setupEventRouting(routing: EventRouting<TypeName>) {
4343
super.setupEventRouting(routing)

model/src/main/kotlin/io/spine/validation/MessageValidationView.kt renamed to model/src/main/kotlin/io/spine/validation/CompiledMessageView.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,16 @@ import io.spine.validation.event.MessageWideRuleAdded
3737
import io.spine.validation.event.SimpleRuleAdded
3838

3939
/**
40-
* A view which accumulates validation data for a message type.
40+
* A view of messages compiled by the validation library.
4141
*
42-
* To add more rules to the message validation, emit [SimpleRuleAdded] or
43-
* [CompositeRuleAdded] events.
42+
* This view provides information about the compiled messages and their
43+
* validation constraints, if any.
4444
*
45-
* Please note, an instance of [MessageValidation] is created as long
46-
* as a new message is discovered. Thus, [MessageValidation] is always present
47-
* for all discovered types, even if the discovered type does not have any
48-
* validation constraints.
45+
* To add one or more validation constraints to the message, emit [SimpleRuleAdded]
46+
* or [CompositeRuleAdded] events.
4947
*/
50-
internal class MessageValidationView :
51-
View<TypeName, MessageValidation, MessageValidation.Builder>() {
48+
internal class CompiledMessageView :
49+
View<TypeName, CompiledMessage, CompiledMessage.Builder>() {
5250

5351
@Subscribe
5452
fun on(@External event: TypeDiscovered) = alter {

model/src/main/kotlin/io/spine/validation/ValidationPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ValidationPlugin : Plugin(
4141
renderers = listOf(),
4242
views = setOf(),
4343
viewRepositories = setOf<ViewRepository<*, *, *>>(
44-
MessageValidationRepository(),
44+
CompiledMessageRepository(),
4545
RequiredFieldRepository(),
4646
ValidatedFieldRepository(),
4747
SetOnceFieldRepository()

proto/context/src/main/proto/spine/validation/message_validation.proto renamed to proto/context/src/main/proto/spine/validation/compiled_message.proto

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ import "spine/options.proto";
3232

3333
option (type_url_prefix) = "type.spine.io";
3434
option java_package = "io.spine.validation";
35-
option java_outer_classname = "MessageValidationProto";
35+
option java_outer_classname = "CompiledMessageProto";
3636
option java_multiple_files = true;
3737

3838
import "spine/protodata/ast.proto";
3939
import "spine/validation/rule.proto";
4040

41-
// All the validation rules of a single message type.
42-
message MessageValidation {
41+
// A message compiled by the validation library with its validation constraints.
42+
message CompiledMessage {
4343
option (entity).kind = PROJECTION;
4444

45-
// The name of the message type for which validation rules are specified.
45+
// The name of the message type.
4646
protodata.TypeName name = 1;
4747

4848
// Partial information about the type.
@@ -55,7 +55,7 @@ message MessageValidation {
5555

5656
// Validation rules.
5757
//
58-
// All these rules must be met in order for a message to be regarded as valid.
58+
// All these rules must be met in order for a message to be valid.
5959
//
6060
repeated Rule rule = 3;
6161
}

0 commit comments

Comments
 (0)