Skip to content

Commit 2b60995

Browse files
committed
Combine RecordMessageMapping.kt and SubjectMapping.kt into DataMessageMapping.kt
1 parent b9bb8cd commit 2b60995

File tree

5 files changed

+95
-68
lines changed

5 files changed

+95
-68
lines changed

spring/api/spring-boot-starter-datasource.api

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ public final class com/caplin/integration/datasourcex/spring/DataSourceConfigura
4848
public fun toString ()Ljava/lang/String;
4949
}
5050

51+
public abstract interface annotation class com/caplin/integration/datasourcex/spring/annotations/DataMessageMapping : java/lang/annotation/Annotation {
52+
public abstract fun type ()Lcom/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type;
53+
public abstract fun value ()[Ljava/lang/String;
54+
}
55+
56+
public final class com/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type : java/lang/Enum {
57+
public static final field JSON Lcom/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type;
58+
public static final field MAPPING Lcom/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type;
59+
public static final field RECORD_GENERIC Lcom/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type;
60+
public static final field RECORD_TYPE1 Lcom/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type;
61+
public static fun getEntries ()Lkotlin/enums/EnumEntries;
62+
public static fun valueOf (Ljava/lang/String;)Lcom/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type;
63+
public static fun values ()[Lcom/caplin/integration/datasourcex/spring/annotations/DataMessageMapping$Type;
64+
}
65+
5166
public abstract interface annotation class com/caplin/integration/datasourcex/spring/annotations/DataService : java/lang/annotation/Annotation {
5267
public abstract fun discardTimeout ()J
5368
public abstract fun remoteLabelPattern ()Ljava/lang/String;
@@ -67,12 +82,3 @@ public final class com/caplin/integration/datasourcex/spring/annotations/Ingress
6782
public static final field USER_ID Ljava/lang/String;
6883
}
6984

70-
public abstract interface annotation class com/caplin/integration/datasourcex/spring/annotations/RecordMessageMapping : java/lang/annotation/Annotation {
71-
public abstract fun recordType ()Lcom/caplin/integration/datasourcex/reactive/api/RecordType;
72-
public abstract fun value ()[Ljava/lang/String;
73-
}
74-
75-
public abstract interface annotation class com/caplin/integration/datasourcex/spring/annotations/SubjectMapping : java/lang/annotation/Annotation {
76-
public abstract fun value ()[Ljava/lang/String;
77-
}
78-
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.caplin.integration.datasourcex.spring.annotations
2+
3+
import com.caplin.datasource.DataSource
4+
import kotlin.annotation.AnnotationRetention.RUNTIME
5+
import kotlin.annotation.AnnotationTarget.FUNCTION
6+
import org.springframework.core.annotation.AliasFor
7+
import org.springframework.messaging.handler.annotation.MessageMapping
8+
9+
@Target(FUNCTION, AnnotationTarget.PROPERTY_GETTER)
10+
@Retention(RUNTIME)
11+
@MustBeDocumented
12+
@MessageMapping
13+
annotation class DataMessageMapping(
14+
@get:AliasFor(annotation = MessageMapping::class) vararg val value: String,
15+
val type: Type = Type.JSON,
16+
) {
17+
18+
enum class Type {
19+
/**
20+
* Methods annotated with this message should return one or more objects to be serialized to
21+
* JSON.
22+
*
23+
* The JSON handler installed in [DataSource] will be used. By default, this is the
24+
* [com.fasterxml.jackson.databind.ObjectMapper] provided by Spring Boot.
25+
*/
26+
JSON,
27+
28+
/**
29+
* Methods annotated with this message should return one or more of [Map].
30+
*
31+
* [toString] will be used to convert the keys and values of the map to String key value pairs.
32+
*/
33+
RECORD_GENERIC,
34+
35+
/**
36+
* Methods annotated with this message should return one or more of [Map].
37+
*
38+
* [toString] will be used to convert the keys and values of the map to String key value pairs.
39+
*/
40+
RECORD_TYPE1,
41+
42+
/**
43+
* Functions of this type should return a String or a stream of Strings.
44+
*
45+
* The requesting peer should then additionally request on the returned path. If a stream of
46+
* Strings is returned, then the remapping can be updated by emitting a second string, and the
47+
* requesting peer will request on the new path, discarding the previously mapped path.
48+
*
49+
* > Note that the returned path will not pass through `object-map` or authentication module
50+
* > mappings before being requested.
51+
*/
52+
MAPPING,
53+
}
54+
}

spring/src/main/kotlin/com/caplin/integration/datasourcex/spring/annotations/RecordMessageMapping.kt

Lines changed: 0 additions & 22 deletions
This file was deleted.

spring/src/main/kotlin/com/caplin/integration/datasourcex/spring/annotations/SubjectMapping.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

spring/src/main/kotlin/com/caplin/integration/datasourcex/spring/internal/DataSourceMessageHandler.kt

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.caplin.integration.datasourcex.spring.internal
22

3-
import com.caplin.integration.datasourcex.reactive.api.RecordType
3+
import com.caplin.integration.datasourcex.spring.annotations.DataMessageMapping
4+
import com.caplin.integration.datasourcex.spring.annotations.DataMessageMapping.Type.MAPPING
5+
import com.caplin.integration.datasourcex.spring.annotations.DataMessageMapping.Type.RECORD_GENERIC
6+
import com.caplin.integration.datasourcex.spring.annotations.DataMessageMapping.Type.RECORD_TYPE1
47
import com.caplin.integration.datasourcex.spring.annotations.IngressDestinationVariable
5-
import com.caplin.integration.datasourcex.spring.annotations.RecordMessageMapping
6-
import com.caplin.integration.datasourcex.spring.annotations.SubjectMapping
78
import java.lang.reflect.AnnotatedElement
89
import java.lang.reflect.Method
910
import java.net.URLDecoder
@@ -160,18 +161,26 @@ internal class DataSourceMessageHandler :
160161
}
161162

162163
val returnType = reactiveAdapterRegistry.resolveElementType(handler.returnType)!!
163-
val isSubjectMapping = handler.getMethodAnnotation(SubjectMapping::class.java) != null
164+
val isSubjectMapping =
165+
handler.getMethodAnnotation(DataMessageMapping::class.java)?.takeIf {
166+
it.type == MAPPING
167+
} != null
164168
if (isSubjectMapping) {
165169
check(String::class.isSuperclassOf(returnType.kotlin)) {
166-
"Methods annotated with @${SubjectMapping::class.simpleName} must return a String or stream of Strings"
170+
"Methods annotated with @${DataMessageMapping::class.simpleName} with a type of " +
171+
"$MAPPING must return a String or stream of Strings"
167172
}
168173
}
169174

170175
val recordMessageMappingType =
171-
handler.getMethodAnnotation(RecordMessageMapping::class.java)?.recordType
176+
handler
177+
.getMethodAnnotation(DataMessageMapping::class.java)
178+
?.takeIf { it.type == RECORD_TYPE1 || it.type == RECORD_GENERIC }
179+
?.type
172180
if (recordMessageMappingType != null) {
173181
check(Map::class.isSuperclassOf(returnType.kotlin)) {
174-
"Methods annotated with @${RecordMessageMapping::class.simpleName} must return a Map or stream of Maps"
182+
"Methods annotated with @${DataMessageMapping::class.simpleName} with a type of " +
183+
"$recordMessageMappingType must return a Map or stream of Maps"
175184
}
176185
}
177186

@@ -182,11 +191,13 @@ internal class DataSourceMessageHandler :
182191

183192
recordMessageMappingType != null ->
184193
when (recordMessageMappingType) {
185-
RecordType.GENERIC ->
194+
RECORD_GENERIC ->
186195
DataSourceRequestTypeMessageCondition.RequestType.Stream.ObjectType.GENERIC
187196

188-
RecordType.TYPE1 ->
197+
RECORD_TYPE1 ->
189198
DataSourceRequestTypeMessageCondition.RequestType.Stream.ObjectType.TYPE1
199+
200+
else -> error("Invalid record type: $recordMessageMappingType")
190201
}
191202

192203
else -> DataSourceRequestTypeMessageCondition.RequestType.Stream.ObjectType.JSON
@@ -195,16 +206,19 @@ internal class DataSourceMessageHandler :
195206

196207
val channelType by lazy {
197208
check(!isSubjectMapping) {
198-
"Methods annotated with @${SubjectMapping::class.simpleName} cannot accept a payload"
209+
"Methods annotated with @${DataMessageMapping::class.simpleName} with a type of " +
210+
"$MAPPING cannot accept a payload"
199211
}
200212
when {
201213
recordMessageMappingType != null ->
202214
when (recordMessageMappingType) {
203-
RecordType.GENERIC ->
215+
RECORD_GENERIC ->
204216
DataSourceRequestTypeMessageCondition.RequestType.Channel.ObjectType.GENERIC
205217

206-
RecordType.TYPE1 ->
218+
RECORD_TYPE1 ->
207219
DataSourceRequestTypeMessageCondition.RequestType.Channel.ObjectType.TYPE1
220+
221+
else -> error("Invalid record type: $recordMessageMappingType")
208222
}
209223

210224
else -> DataSourceRequestTypeMessageCondition.RequestType.Channel.ObjectType.JSON

0 commit comments

Comments
 (0)