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

Fix model asset references to use version-agnostic QN #916

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ log4j = "2.24.1"
wiremock = "3.9.1"
jnanoid = "2.0.0"
numaflow = "0.8.0"
awssdk = "2.28.21"
awssdk = "2.28.22"
gcs = "26.48.0"
system-stubs = "2.1.7"
fastcsv = "3.3.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.atlan.model.assets.DataProduct
import com.atlan.model.assets.Glossary
import com.atlan.model.assets.GlossaryCategory
import com.atlan.model.assets.GlossaryTerm
import com.atlan.model.assets.IModel
import com.atlan.model.assets.Link
import com.atlan.model.assets.Readme
import com.atlan.pkg.Utils
Expand All @@ -22,7 +23,7 @@ import java.util.concurrent.atomic.AtomicLong
* Static object to transform asset references.
*/
object AssetRefXformer {
private const val TYPE_QN_DELIMITER = "@"
const val TYPE_QN_DELIMITER = "@"

/**
* Encodes (serializes) an asset reference into a string form.
Expand All @@ -46,6 +47,7 @@ object AssetRefXformer {
is GlossaryCategory -> GlossaryCategoryXformer.encode(asset)
is GlossaryTerm -> GlossaryTermXformer.encode(asset)
is DataDomain -> DataDomainXformer.encode(asset)
is IModel -> ModelAssetXformer.encode(asset)
else -> {
var qualifiedName = asset.qualifiedName
if (asset.qualifiedName.isNullOrEmpty() && asset.uniqueAttributes != null) {
Expand Down Expand Up @@ -76,6 +78,7 @@ object AssetRefXformer {
GlossaryCategory.ANCHOR.atlanFieldName -> GlossaryXformer.decode(assetRef, fieldName)
"assignedTerms", in GlossaryTermXformer.TERM_TO_TERM_FIELDS -> GlossaryTermXformer.decode(assetRef, fieldName)
DataDomain.PARENT_DOMAIN.atlanFieldName, DataProduct.DATA_DOMAIN.atlanFieldName -> DataDomainXformer.decode(assetRef, fieldName)
in ModelAssetXformer.MODEL_ASSET_REF_FIELDS -> ModelAssetXformer.decode(assetRef, fieldName)
else -> {
val tokens = assetRef.split(TYPE_QN_DELIMITER)
val typeName = tokens[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* SPDX-License-Identifier: Apache-2.0
Copyright 2023 Atlan Pte. Ltd. */
package com.atlan.pkg.serde.cell

import com.atlan.model.assets.Asset
import com.atlan.model.assets.IModel
import com.atlan.model.assets.ModelAttribute
import com.atlan.model.assets.ModelAttributeAssociation
import com.atlan.model.assets.ModelDataModel
import com.atlan.model.assets.ModelEntity
import com.atlan.model.assets.ModelEntityAssociation
import com.atlan.model.assets.ModelVersion
import com.atlan.pkg.serde.cell.AssetRefXformer.TYPE_QN_DELIMITER

/**
* Static object to transform term references.
*/
object ModelAssetXformer {
val MODEL_ASSET_REF_FIELDS =
setOf(
ModelDataModel.MODEL_VERSIONS.atlanFieldName,
ModelVersion.MODEL_DATA_MODEL.atlanFieldName,
ModelVersion.MODEL_VERSION_ENTITIES.atlanFieldName,
ModelEntity.MODEL_ENTITY_ATTRIBUTES.atlanFieldName,
ModelEntity.MODEL_ENTITY_MAPPED_FROM_ENTITIES.atlanFieldName,
ModelEntity.MODEL_ENTITY_MAPPED_TO_ENTITIES.atlanFieldName,
ModelEntity.MODEL_ENTITY_RELATED_FROM_ENTITIES.atlanFieldName,
ModelEntity.MODEL_ENTITY_RELATED_TO_ENTITIES.atlanFieldName,
ModelEntity.MODEL_VERSIONS,
ModelAttribute.MODEL_ATTRIBUTE_ENTITIES.atlanFieldName,
ModelAttribute.MODEL_ATTRIBUTE_MAPPED_FROM_ATTRIBUTES.atlanFieldName,
ModelAttribute.MODEL_ATTRIBUTE_MAPPED_TO_ATTRIBUTES.atlanFieldName,
ModelAttribute.MODEL_ATTRIBUTE_RELATED_FROM_ATTRIBUTES.atlanFieldName,
ModelAttribute.MODEL_ATTRIBUTE_RELATED_TO_ATTRIBUTES.atlanFieldName,
ModelEntityAssociation.MODEL_ENTITY_ASSOCIATION_FROM.atlanFieldName,
ModelEntityAssociation.MODEL_ENTITY_ASSOCIATION_TO.atlanFieldName,
ModelAttributeAssociation.MODEL_ATTRIBUTE_ASSOCIATION_FROM.atlanFieldName,
ModelAttributeAssociation.MODEL_ATTRIBUTE_ASSOCIATION_TO.atlanFieldName,
)

/**
* Encodes (serializes) a term reference into a string form.
*
* @param asset to be encoded
* @return the string-encoded form for that asset
*/
fun encode(asset: Asset): String {
// Handle some assets as direct embeds
return when (asset) {
is IModel -> {
val ma = asset as IModel
"${ma.typeName}$TYPE_QN_DELIMITER${ma.modelVersionAgnosticQualifiedName}"
}
else -> AssetRefXformer.encode(asset)
}
}

/**
* Decodes (deserializes) a string form into a term reference object.
*
* @param assetRef the string form to be decoded
* @param fieldName the name of the field containing the string-encoded value
* @return the term reference represented by the string
*/
fun decode(
assetRef: String,
fieldName: String,
): Asset {
return when (fieldName) {
in MODEL_ASSET_REF_FIELDS -> {
val tokens = assetRef.split(TYPE_QN_DELIMITER)
if (tokens.size > 1) {
ModelDataModel._internal() // start with a model so we have versionAgnosticQN
.typeName(tokens[0]) // override the type
.modelVersionAgnosticQualifiedName(assetRef.substringAfter(TYPE_QN_DELIMITER))
.build()
} else {
throw NoSuchElementException("Model asset $assetRef not found (via $fieldName).")
}
}
else -> AssetRefXformer.decode(assetRef, fieldName)
}
}
}
Loading