Skip to content

Commit

Permalink
Tidy AbstractEvent internal properties (close #666)
Browse files Browse the repository at this point in the history
* Match AbstractEvent entities API with iOS tracker

* Restore original behaviour
  • Loading branch information
mscwilson authored Jan 29, 2024
1 parent 334ebbe commit fbe62a0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ class AbstractEventTest {
@Test
fun testAddsEntitiesUsingAllAPIs() {
val event = ScreenView("screen")
val entity1 = SelfDescribingJson("schema1", "data1")
val entity2 = SelfDescribingJson("schema2", "data2")
val entity3 = SelfDescribingJson("schema3", "data3")

event.entities.add(SelfDescribingJson("schema1", "data1"))
event.customContexts.add(SelfDescribingJson("schema2", "data2"))
event.entities(listOf(SelfDescribingJson("schema3", "data3")))
event.contexts(listOf(SelfDescribingJson("schema4", "data4")))

Assert.assertEquals(4, event.entities.count())
Assert.assertEquals(4, event.contexts.count())
Assert.assertEquals(4, event.customContexts.count())
event.entities.add(entity1)
Assert.assertEquals(1, event.entities.count())

event.entities(listOf(entity2))
Assert.assertEquals(2, event.entities.count())

event.contexts(listOf(entity3))
Assert.assertEquals(3, event.entities.count())
Assert.assertEquals(3, event.contexts.count())
Assert.assertTrue(event.entities.contains(entity1))
Assert.assertTrue(event.entities.contains(entity2))
Assert.assertTrue(event.entities.contains(entity3))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@ import java.util.*
* - "True" timestamp: user-defined custom event timestamp
*/
abstract class AbstractEvent : Event {
/** List of custom contexts associated with the event. */
@Deprecated("Old nomenclature.", ReplaceWith("entities"))
@JvmField
val customContexts: MutableList<SelfDescribingJson> = LinkedList()
private var _entities = mutableListOf<SelfDescribingJson>()
/**
* @return the custom context entities associated with the event.
*/
override var entities: MutableList<SelfDescribingJson>
get() {
if (isProcessing) {
entitiesForProcessing?.let {
return (_entities + it).toMutableList()
}
}
return _entities
}
set(value) {
_entities = value
}

@Deprecated("Old nomenclature", ReplaceWith("entities"))
override var contexts: List<SelfDescribingJson>
get() = entities
set(value) { entities = value.toMutableList()}

/**
* @return the optional "true" (custom) event timestamp
Expand All @@ -39,14 +56,14 @@ abstract class AbstractEvent : Event {
// Builder methods

/** Adds a list of context entities to the existing ones. */
fun entities(entities: List<SelfDescribingJson>?): AbstractEvent {
entities?.let { customContexts.addAll(entities) }
fun entities(entities: List<SelfDescribingJson>): AbstractEvent {
this.entities.addAll(entities)
return this
}

/** Adds a list of context entities to the existing ones. */
@Deprecated("Old nomenclature.", ReplaceWith("entities()"))
fun contexts(contexts: List<SelfDescribingJson>?): AbstractEvent {
fun contexts(contexts: List<SelfDescribingJson>): AbstractEvent {
return entities(contexts)
}

Expand All @@ -56,24 +73,6 @@ abstract class AbstractEvent : Event {
return this
}

// Public methods

/**
* @return the event custom context entities
*/
override val entities: MutableList<SelfDescribingJson>
get() {
if (isProcessing) {
entitiesForProcessing?.let {
return (customContexts + it).toMutableList()
}
}
return customContexts
}

@Deprecated("Old nomenclature", ReplaceWith("entities"))
override val contexts: List<SelfDescribingJson>
get() = entities

private var isProcessing = false
override fun beginProcessing(tracker: Tracker) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ConsentGranted(expiry: String, documentId: String, documentVersion: String
override fun beginProcessing(tracker: Tracker) {
for (document in documents) {
val context = SelfDescribingJson(document.schema, document.dataPayload)
customContexts.add(context) // TODO: Only the user should modify the public customContexts property
entities.add(context)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ConsentWithdrawn(all: Boolean, documentId: String, documentVersion: String
override fun beginProcessing(tracker: Tracker) {
for (document in documents) {
val context = SelfDescribingJson(document.schema, document.dataPayload)
customContexts.add(context) // TODO: Only the user should modify the public customContexts property
entities.add(context)
}
}
}

0 comments on commit fbe62a0

Please sign in to comment.