Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trunkVersion and branchId support #6276

Merged
merged 15 commits into from
Jul 31, 2024
Next Next commit
Add trunkVersion
  • Loading branch information
seadowg committed Jul 30, 2024
commit e21244e19a444d2daae9cff965a6433a16cce7b5
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
entity.version,
entity.properties,
entity.state,
index
index,
entity.trunkVersion
)
}
}
Expand All @@ -51,7 +52,8 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
entity.label ?: existing.label,
version = entity.version,
properties = mergeProperties(existing.toEntity(entity.list), entity),
state = state
state = state,
trunkVersion = entity.trunkVersion
).toJson()
)
} else {
Expand Down Expand Up @@ -86,7 +88,11 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
return getEntities(list).firstOrNull { it.id == id }
}

override fun getAllByProperty(list: String, property: String, value: String): List<Entity.Saved> {
override fun getAllByProperty(
list: String,
property: String,
value: String
): List<Entity.Saved> {
return getEntities(list).filter { entity ->
entity.properties.any { (first, second) -> first == property && second == value }
}
Expand Down Expand Up @@ -163,7 +169,8 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
val label: String?,
val version: Int,
val properties: Map<String, String>,
val offline: Boolean
val offline: Boolean,
val trunkVersion: Int?
)

private fun JsonEntity.toEntity(list: String): Entity.New {
Expand All @@ -179,7 +186,8 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
this.label,
this.version,
this.properties.entries.map { Pair(it.key, it.value) },
state
state,
this.trunkVersion
)
}

Expand All @@ -189,7 +197,8 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
this.label,
this.version,
this.properties.toMap(),
this.state == Entity.State.OFFLINE
this.state == Entity.State.OFFLINE,
this.trunkVersion
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ abstract class EntitiesRepositoryTest {
fun `#getEntities returns entities for list`() {
val repository = buildSubject()

val wine = Entity.New("wines", "1", "Léoville Barton 2008")
val whisky = Entity.New("whiskys", "2", "Lagavulin 16")
val wine = Entity.New(
"wines",
"1",
"Léoville Barton 2008",
version = 2,
trunkVersion = 1
)

val whisky = Entity.New(
"whiskys",
"2",
"Lagavulin 16",
version = 3,
trunkVersion = 1
)

repository.save(wine)
repository.save(whisky)

Expand All @@ -47,10 +61,15 @@ abstract class EntitiesRepositoryTest {
fun `#save updates existing entity with matching id`() {
val repository = buildSubject()

val wine = Entity.New("wines", "1", "Léoville Barton 2008", version = 1)
val wine = Entity.New(
"wines",
"1",
"Léoville Barton 2008",
trunkVersion = 1
)
repository.save(wine)

val updatedWine = Entity.New("wines", wine.id, "Léoville Barton 2009", version = 2)
val updatedWine = wine.copy(label = "Léoville Barton 2009", version = 2)
repository.save(updatedWine)

val wines = repository.getEntities("wines")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,23 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos
val name = TreeElement(EntityItemElement.ID)
val label = TreeElement(EntityItemElement.LABEL)
val version = TreeElement(EntityItemElement.VERSION)
val trunkVersion = TreeElement(EntityItemElement.TRUNK_VERSION)

if (!partial) {
name.value = StringData(entity.id)
label.value = StringData(entity.label)
version.value = StringData(entity.version.toString())

if (entity.trunkVersion != null) {
trunkVersion.value = StringData(entity.trunkVersion.toString())
}
}

val item = TreeElement("item", entity.index, partial)
item.addChild(name)
item.addChild(label)
item.addChild(version)
item.addChild(trunkVersion)

entity.properties.forEach { property ->
val propertyElement = TreeElement(property.first)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ internal object EntityItemElement {
const val ID = "name"
const val LABEL = "label"
const val VERSION = "__version"
const val TRUNK_VERSION = "__trunkVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ sealed interface Entity {
val version: Int
val properties: List<Pair<String, String>>
val state: State
val trunkVersion: Int?

data class New(
override val list: String,
override val id: String,
override val label: String?,
override val version: Int = 1,
override val properties: List<Pair<String, String>> = emptyList(),
override val state: State = State.OFFLINE
override val state: State = State.OFFLINE,
override val trunkVersion: Int? = null
) : Entity

data class Saved(
Expand All @@ -24,7 +26,8 @@ sealed interface Entity {
override val version: Int = 1,
override val properties: List<Pair<String, String>> = emptyList(),
override val state: State = State.OFFLINE,
val index: Int
val index: Int,
override val trunkVersion: Int? = null
) : Entity

enum class State {
Expand All @@ -45,7 +48,8 @@ sealed interface Entity {
entity.label,
entity.version,
entity.properties,
entity.state
entity.state,
entity.trunkVersion
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class InMemEntitiesRepository : EntitiesRepository {
entity.version,
entity.properties,
entity.state,
index
index,
entity.trunkVersion
)
}
}
Expand Down Expand Up @@ -69,7 +70,8 @@ class InMemEntitiesRepository : EntitiesRepository {
entity.label ?: existing.label,
version = entity.version,
properties = mergeProperties(existing, entity),
state = state
state = state,
trunkVersion = entity.trunkVersion
)
)
} else {
Expand All @@ -80,7 +82,8 @@ class InMemEntitiesRepository : EntitiesRepository {
entity.label,
entity.version,
entity.properties,
entity.state
entity.state,
entity.trunkVersion
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LocalEntitiesInstanceProviderTest {
assertThat(instance.numChildren, equalTo(1))

val item = instance.getChildAt(0)!!
assertThat(item.numChildren, equalTo(5))
assertThat(item.numChildren, equalTo(6))
assertThat(item.getFirstChild("age")?.value?.value, equalTo("35"))
assertThat(item.getFirstChild("born")?.value?.value, equalTo("England"))
}
Expand All @@ -49,10 +49,49 @@ class LocalEntitiesInstanceProviderTest {
assertThat(instance.numChildren, equalTo(1))

val item = instance.getChildAt(0)!!
assertThat(item.numChildren, equalTo(3))
assertThat(item.numChildren, equalTo(4))
assertThat(item.getFirstChild(EntityItemElement.VERSION)?.value?.value, equalTo("1"))
}

@Test
fun `includes trunk version in local entity elements`() {
val entity =
Entity.New(
"people",
"1",
"Shiv Roy",
trunkVersion = 1
)
entitiesRepository.save(entity)

val parser = LocalEntitiesInstanceProvider { entitiesRepository }
val instance = parser.get("people", "people.csv")
assertThat(instance.numChildren, equalTo(1))

val item = instance.getChildAt(0)!!
assertThat(item.numChildren, equalTo(4))
assertThat(item.getFirstChild(EntityItemElement.TRUNK_VERSION)?.value?.value, equalTo("1"))
}

@Test
fun `includes blank trunk version when it is null`() {
val entity =
Entity.New(
"people",
"1",
"Shiv Roy",
trunkVersion = null
)
entitiesRepository.save(entity)

val parser = LocalEntitiesInstanceProvider { entitiesRepository }
val instance = parser.get("people", "people.csv")
assertThat(instance.numChildren, equalTo(1))

val item = instance.getChildAt(0)!!
assertThat(item.getFirstChild(EntityItemElement.TRUNK_VERSION)?.value, equalTo(null))
}

@Test
fun `partial parse returns elements without values for first item and just item for others`() {
val entity = arrayOf(
Expand All @@ -79,7 +118,7 @@ class LocalEntitiesInstanceProviderTest {

val item1 = instance.getChildAt(0)!!
assertThat(item1.isPartial, equalTo(true))
assertThat(item1.numChildren, equalTo(4))
assertThat(item1.numChildren, equalTo(5))
0.until(item1.numChildren).forEach {
assertThat(item1.getChildAt(it).value?.value, equalTo(null))
}
Expand Down