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

CompoundLens for python #324

Open
SDelgado-21 opened this issue Apr 12, 2024 · 0 comments
Open

CompoundLens for python #324

SDelgado-21 opened this issue Apr 12, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request py

Comments

@SDelgado-21
Copy link
Collaborator

Original implementation:

open class CompoundLens : Lens {
private val lenses = mutableListOf<Lens>()
fun add(lens: Lens): CompoundLens {
lenses.add(lens)
return this
}
fun replaceAll(toReplace: String, replacement: String): CompoundLens = mutateAllFacets {
it.replace(toReplace, replacement)
}
fun replaceAllRegex(regex: String, replacement: String): CompoundLens = mutateAllFacets {
it.replace(regex.toRegex(), replacement)
}
fun mutateAllFacets(perString: StringOptionalFunction): CompoundLens = add { snapshot ->
Snapshot.ofEntries(
snapshot.allEntries().mapNotNull { e ->
if (e.value.isBinary) e
else perString.apply(e.value.valueString())?.let { entry(e.key, SnapshotValue.of(it)) }
})
}
fun setFacetFrom(target: String, source: String, function: StringOptionalFunction) =
add { snapshot ->
val sourceValue = snapshot.subjectOrFacetMaybe(source)
if (sourceValue == null) snapshot
else setFacetOf(snapshot, target, function.apply(sourceValue.valueString()))
}
fun mutateFacet(target: String, function: StringOptionalFunction) =
setFacetFrom(target, target, function)
private fun setFacetOf(snapshot: Snapshot, target: String, newValue: String?): Snapshot =
if (newValue == null) snapshot else snapshot.plusOrReplace(target, SnapshotValue.of(newValue))
override fun transform(snapshot: Snapshot): Snapshot {
var current = snapshot
lenses.forEach { current = it.transform(current) }
return current
}

@SDelgado-21 SDelgado-21 added enhancement New feature or request py labels Apr 12, 2024
@SDelgado-21 SDelgado-21 self-assigned this Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request py
Projects
None yet
Development

No branches or pull requests

1 participant