1
1
/*
2
- * Copyright 2023 , TeamDev. All rights reserved.
2
+ * Copyright 2024 , TeamDev. All rights reserved.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
7
*
8
- * http ://www.apache.org/licenses/LICENSE-2.0
8
+ * https ://www.apache.org/licenses/LICENSE-2.0
9
9
*
10
10
* Redistribution and use in source and/or binary forms, with or without
11
11
* modification, must retain the above copyright notice and the following
@@ -30,12 +30,15 @@ import com.squareup.javapoet.CodeBlock
30
30
import io.spine.protodata.ast.Field
31
31
import io.spine.protodata.ast.PrimitiveType.TYPE_BYTES
32
32
import io.spine.protodata.ast.PrimitiveType.TYPE_STRING
33
- import io.spine.protodata.value.Value
33
+ import io.spine.protodata.ast.Type
34
+ import io.spine.protodata.ast.isList
35
+ import io.spine.protodata.ast.isMap
34
36
import io.spine.protodata.java.ClassName
35
37
import io.spine.protodata.java.Expression
36
38
import io.spine.protodata.java.Literal
37
39
import io.spine.protodata.java.call
38
- import io.spine.protodata.ast.isRepeated
40
+ import io.spine.protodata.value.Value
41
+ import io.spine.string.shortly
39
42
import io.spine.tools.java.codeBlock
40
43
import io.spine.validation.ComparisonOperator.EQUAL
41
44
import io.spine.validation.ComparisonOperator.GREATER_OR_EQUAL
@@ -48,6 +51,7 @@ import io.spine.validation.SimpleRule
48
51
import io.spine.validation.SimpleRule.OperatorKindCase.CUSTOM_OPERATOR
49
52
import io.spine.validation.SimpleRule.OperatorKindCase.OPERATOR
50
53
import io.spine.validation.UnsetValue
54
+ import io.spine.validation.extractType
51
55
import kotlin.jvm.optionals.getOrNull
52
56
53
57
/* *
@@ -103,7 +107,7 @@ internal open class SimpleRuleGenerator(ctx: GenerationContext) : CodeGenerator(
103
107
private fun defaultFieldValue (): Value ? = with (ctx) {
104
108
val field = simpleRuleField
105
109
return if (isElement) {
106
- UnsetValue .singular(field.type )
110
+ UnsetValue .singular(field.directOrElementType() )
107
111
} else {
108
112
UnsetValue .forField(field)
109
113
}.getOrNull()
@@ -120,17 +124,19 @@ internal open class SimpleRuleGenerator(ctx: GenerationContext) : CodeGenerator(
120
124
checkNotNull(otherValue) {
121
125
" Expected the rule to specify `simple.other_value`, but was: $rule "
122
126
}
123
- val type = field.type
127
+ val type = field.directOrElementType()
124
128
val signs = selectSigns()
125
129
val compare = signs[rule.operator ] ? : error(
126
- " Unsupported operation `${rule.operator } ` for type `$type `."
130
+ " Unsupported operation `${rule.operator } ` for the type `$type `."
127
131
)
128
- checkNotNull(ctx.fieldOrElement) { " There is no field value for rule: $rule " }
132
+ checkNotNull(ctx.fieldOrElement) {
133
+ " There is no field value for the rule: `${rule.shortly()} `."
134
+ }
129
135
return Literal (compare(ctx.fieldOrElement!! .toCode(), otherValue.toCode()))
130
136
}
131
137
132
138
private fun fieldIsJavaObject (): Boolean =
133
- ! field.isJavaPrimitive () || (field.isRepeated && ! ctx.isElement)
139
+ ! field.refersToJavaPrimitive () || (( field.isList || field.isMap) && ! ctx.isElement)
134
140
135
141
private fun selectSigns () = if (fieldIsJavaObject()) {
136
142
OBJECT_COMPARISON_OPS
@@ -154,7 +160,7 @@ internal open class SimpleRuleGenerator(ctx: GenerationContext) : CodeGenerator(
154
160
internal fun generatorForSimple (ctx : GenerationContext ): CodeGenerator {
155
161
val distribute = ctx.rule.simple.distribute
156
162
val field = ctx.simpleRuleField
157
- return if (distribute && field.isRepeated ) {
163
+ return if (distribute && ( field.isList || field.isMap) ) {
158
164
DistributingGenerator (ctx) {
159
165
generatorForSingular(it)
160
166
}
@@ -172,12 +178,21 @@ private fun generatorForSingular(ctx: GenerationContext): CodeGenerator {
172
178
}
173
179
}
174
180
175
- private fun Field.isJavaPrimitive (): Boolean {
176
- if (! type.hasPrimitive()) {
181
+ @Suppress(" ReturnCount" )
182
+ private fun Field.refersToJavaPrimitive (): Boolean {
183
+ if (type.isList) {
184
+ return type.list.isPrimitive
185
+ }
186
+ if (type.isMap) {
187
+ return type.map.valueType.isPrimitive
188
+ }
189
+ if (! type.isPrimitive) {
177
190
return false
178
191
}
179
192
return when (type.primitive) {
180
193
TYPE_STRING , TYPE_BYTES -> false
181
194
else -> true
182
195
}
183
196
}
197
+
198
+ private fun Field.directOrElementType (): Type = type.extractType()
0 commit comments