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

Avoid unnecessary copies in generated VisitorTransforms #75

Merged
merged 3 commits into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,39 @@ import com.amazon.ionelement.api.MetaContainer
abstract class DomainVisitorTransformBase {
open fun transformMetas(metas: MetaContainer) = metas

open fun transformAnyElement(node: AnyElement): AnyElement =
// TODO: remove .asAnyElement() below when https://github.com/amzn/ion-element-kotlin/issues/36 is fixed.
node.copy(metas = transformMetas(node.metas)).asAnyElement()
open fun transformAnyElement(node: AnyElement): AnyElement {
val newMetas = transformMetas(node.metas)
return if(node.metas !== newMetas) {
// TODO: remove .asAnyElement() below when https://github.com/amzn/ion-element-kotlin/issues/36 is fixed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note, is this TODO still relevant? The mentioned issue was closed via:

Actually, this is not possible since the narrower IonElement implementations already has a covariant return type which is narrowed.

node.copy(metas = newMetas).asAnyElement()
} else {
node
}
}

open fun transformSymbolPrimitive(sym: SymbolPrimitive) =
SymbolPrimitive(
transformSymbolPrimitiveText(sym),
transformSymbolPrimitiveMetas(sym))
open fun transformSymbolPrimitive(sym: SymbolPrimitive): SymbolPrimitive {
val newText = transformSymbolPrimitiveText(sym)
val newMetas = transformSymbolPrimitiveMetas(sym)
return if(sym.text != newText || sym.metas !== newMetas) {
SymbolPrimitive(newText, newMetas)
} else {
sym
}
}

open fun transformSymbolPrimitiveText(sym: SymbolPrimitive) = sym.text

open fun transformSymbolPrimitiveMetas(sym: SymbolPrimitive) = transformMetas(sym.metas)

open fun transformLongPrimitive(lng: LongPrimitive) =
LongPrimitive(
transformLongPrimitiveValue(lng),
transformLongPrimitiveMetas(lng))
open fun transformLongPrimitive(lng: LongPrimitive): LongPrimitive {
val newValue = transformLongPrimitiveValue(lng)
val newMetas = transformLongPrimitiveMetas(lng)
return if(lng.value != newValue || lng.metas !== newMetas) {
LongPrimitive(newValue, newMetas)
} else {
lng
}
}

open fun transformLongPrimitiveValue(sym: LongPrimitive): Long = sym.value

Expand Down
Loading