Skip to content

Commit 2f83f7d

Browse files
committed
refactor: replace Service with LiveLocation in LiveView
necessary for sourceplusplus/sourceplusplus#771
1 parent 8dc648a commit 2f83f7d

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

src/main/kotlin/spp/protocol/instrument/location/LiveLocation.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package spp.protocol.instrument.location
1818

19+
import io.vertx.core.json.JsonObject
1920
import spp.protocol.platform.general.Service
2021

2122
/**
@@ -24,4 +25,17 @@ import spp.protocol.platform.general.Service
2425
interface LiveLocation {
2526
val service: Service?
2627
val probeId: String?
28+
29+
fun isSameLocation(other: LiveLocation): Boolean
30+
fun toJson(): JsonObject
31+
32+
companion object {
33+
fun fromJson(json: JsonObject): LiveLocation {
34+
return if (json.containsKey("source")) {
35+
LiveSourceLocation(json)
36+
} else {
37+
Service(json)
38+
}
39+
}
40+
}
2741
}

src/main/kotlin/spp/protocol/instrument/location/LiveSourceLocation.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ data class LiveSourceLocation @JvmOverloads constructor(
3636
val scope: LocationScope = LocationScope.LINE
3737
) : LiveLocation, Comparable<LiveSourceLocation> {
3838

39+
init {
40+
require(source.isNotBlank()) { "source is required" }
41+
}
42+
3943
constructor(json: JsonObject) : this(
4044
source = json.getString("source"),
4145
line = json.getInteger("line") ?: -1,
@@ -51,7 +55,16 @@ data class LiveSourceLocation @JvmOverloads constructor(
5155
scope = json.getString("scope")?.let { LocationScope.valueOf(it) } ?: LocationScope.LINE
5256
)
5357

54-
fun toJson(): JsonObject {
58+
override fun isSameLocation(other: LiveLocation): Boolean {
59+
if (other !is LiveSourceLocation) return false
60+
if (source != other.source) return false
61+
if (line != other.line && line != -1 && other.line != -1) return false //-1 is wildcard
62+
if (service != null && (other.service == null || !service.isSameLocation(other.service!!))) return false
63+
if (probeId != null && probeId != other.probeId) return false
64+
return true
65+
}
66+
67+
override fun toJson(): JsonObject {
5568
val json = JsonObject()
5669
json.put("source", source)
5770
json.put("line", line)
@@ -70,7 +83,7 @@ data class LiveSourceLocation @JvmOverloads constructor(
7083
fun isSameLocation(other: LiveSourceLocation): Boolean {
7184
if (source != other.source) return false
7285
if (line != other.line && line != -1 && other.line != -1) return false //-1 is wildcard
73-
if (service != null && (other.service == null || !service.isSameService(other.service))) return false
86+
if (service != null && (other.service == null || !service.isSameLocation(other.service))) return false
7487
if (probeId != null && probeId != other.probeId) return false
7588
return true
7689
}

src/main/kotlin/spp/protocol/platform/general/Service.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
*/
1717
package spp.protocol.platform.general
1818

19+
import com.fasterxml.jackson.annotation.JsonIgnore
1920
import io.vertx.codegen.annotations.DataObject
21+
import io.vertx.codegen.annotations.GenIgnore
2022
import io.vertx.core.json.JsonObject
23+
import spp.protocol.instrument.location.LiveLocation
2124
import spp.protocol.platform.general.util.IDManager
2225

2326
/**
@@ -34,7 +37,16 @@ data class Service(
3437
val normal: Boolean = true,
3538
val environment: String? = null,
3639
val version: String? = null
37-
) {
40+
) : LiveLocation {
41+
42+
@GenIgnore
43+
@get:JsonIgnore
44+
@delegate:JsonIgnore
45+
override val service: Service by lazy { this }
46+
47+
@GenIgnore
48+
@get:JsonIgnore
49+
override val probeId: String? = null
3850

3951
val id by lazy {
4052
if (version != null) {
@@ -54,7 +66,7 @@ data class Service(
5466
json.getString("version")
5567
)
5668

57-
fun toJson(): JsonObject {
69+
override fun toJson(): JsonObject {
5870
val json = JsonObject()
5971
json.put("name", name)
6072
json.put("group", group)
@@ -77,7 +89,8 @@ data class Service(
7789
/**
7890
* Ensures all non-null fields are equal.
7991
*/
80-
fun isSameService(other: Service): Boolean {
92+
override fun isSameLocation(other: LiveLocation): Boolean {
93+
if (other !is Service) return false //todo: support other types
8194
if (name != other.name) return false
8295
if (group != null && group != other.group) return false
8396
if (shortName != null && shortName != other.shortName) return false

src/main/kotlin/spp/protocol/view/LiveView.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import io.vertx.codegen.annotations.DataObject
2020
import io.vertx.core.Vertx
2121
import io.vertx.core.json.JsonArray
2222
import io.vertx.core.json.JsonObject
23-
import spp.protocol.platform.general.Service
23+
import spp.protocol.instrument.location.LiveLocation
2424
import spp.protocol.service.SourceServices.Subscribe.toLiveViewSubscription
2525

2626
/**
@@ -33,25 +33,22 @@ import spp.protocol.service.SourceServices.Subscribe.toLiveViewSubscription
3333
data class LiveView(
3434
val entityIds: MutableSet<String>,
3535
val viewConfig: LiveViewConfig,
36-
val service: Service? = null,
37-
val serviceInstance: String? = null,
36+
val location: LiveLocation? = null,
3837
val subscriptionId: String? = null
3938
) {
4039
constructor(json: JsonObject) : this(
4140
subscriptionId = json.getString("subscriptionId"),
4241
entityIds = json.getJsonArray("entityIds").map { it.toString() }.toMutableSet(),
4342
viewConfig = LiveViewConfig(json.getJsonObject("viewConfig")),
44-
service = json.getJsonObject("service")?.let { Service(it) },
45-
serviceInstance = json.getString("serviceInstance")
43+
location = json.getJsonObject("location")?.let { LiveLocation.fromJson(it) },
4644
)
4745

4846
fun toJson(): JsonObject {
4947
val json = JsonObject()
50-
json.put("subscriptionId", subscriptionId)
5148
json.put("entityIds", JsonArray().apply { entityIds.forEach { add(it) } })
5249
json.put("viewConfig", viewConfig.toJson())
53-
service?.let { json.put("service", it.toJson()) }
54-
json.put("serviceInstance", serviceInstance)
50+
json.put("location", location?.toJson())
51+
json.put("subscriptionId", subscriptionId)
5552
return json
5653
}
5754

@@ -73,8 +70,7 @@ data class LiveView(
7370
if (subscriptionId != null) append("subscriptionId=$subscriptionId, ")
7471
append("entityIds=$entityIds, ")
7572
append("viewConfig=$viewConfig")
76-
if (service != null) append(", service=$service")
77-
if (serviceInstance != null) append(", serviceInstance=$serviceInstance")
73+
if (location != null) append(", location=$location")
7874
append(")")
7975
}
8076
}

0 commit comments

Comments
 (0)