Skip to content

Commit

Permalink
add getAspectTypesFromAssetType method (#438)
Browse files Browse the repository at this point in the history
Co-authored-by: Yang Yang <[email protected]>
  • Loading branch information
yangyangv2 and Yang Yang authored Sep 29, 2024
1 parent f71a224 commit 53ed1f5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,38 @@ public static <SNAPSHOT extends RecordTemplate> List<RecordTemplate> getAspectsF
}

/**
* Extracts the list of aspects in an asset.
* Get aspect types from asset type.
* @param assetClass asset class
* @return A list of aspect classes used in the asset
*/
public static <ASSET extends RecordTemplate> List<Class<? extends RecordTemplate>> getAspectTypesFromAssetType(
@Nonnull Class<ASSET> assetClass) {
try {
final List<Class<? extends RecordTemplate>> aspectTypes = new ArrayList<>();
final Field[] assetFields = assetClass.getDeclaredFields();
for (final Field assetField : assetFields) {
if (assetField.getName().startsWith(FIELD_FIELD_PREFIX)) {
final String assetFieldName = assetField.getName().substring(FIELD_FIELD_PREFIX.length());
if (assetFieldName.equalsIgnoreCase(URN_FIELD)) {
continue;
}
String methodName = "get" + assetFieldName;
Class<?> returnType = assetClass.getMethod(methodName).getReturnType();
if (RecordTemplate.class.isAssignableFrom(returnType)) {
aspectTypes.add(returnType.asSubclass(RecordTemplate.class));
}
}
}
return aspectTypes;
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}



/**
* Extracts the list of (non-null) aspects in an asset.
*
* @param asset the asset to extract aspects from
* @param <ASSET> must be a valid asset model defined in com.linkedin.metadata.asset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.linkedin.data.template.UnionTemplate;
import com.linkedin.metadata.validator.NullFieldException;
import com.linkedin.testing.AspectAttributes;
import com.linkedin.testing.AspectFooEvolved;
import com.linkedin.testing.AspectUnionWithSoftDeletedAspect;
import com.linkedin.testing.BarAsset;
import com.linkedin.testing.BarUrnArray;
Expand Down Expand Up @@ -328,6 +329,26 @@ public void testGetAspectsFromAsset() {
assertEquals(aspects.size(), 2);
assertEquals(aspects.get(0), expectedAspectFoo);
assertEquals(aspects.get(1), expectedAspectBar);


BarAsset barAsset = new BarAsset();
aspects = ModelUtils.getAspectsFromAsset(barAsset);
assertTrue(aspects.isEmpty());
}

@Test
public void testGetAspectTypesFromAssetType() {
List<Class<? extends RecordTemplate>> assetTypes = ModelUtils.getAspectTypesFromAssetType(BarAsset.class);
assertEquals(assetTypes.size(), 1);
assertEquals(assetTypes.get(0), AspectBar.class);

assetTypes = ModelUtils.getAspectTypesFromAssetType(EntityAsset.class);
assertEquals(assetTypes.size(), 5);
assertEquals(assetTypes.get(0), AspectFoo.class);
assertEquals(assetTypes.get(1), AspectBar.class);
assertEquals(assetTypes.get(2), AspectFooEvolved.class);
assertEquals(assetTypes.get(3), AspectFooBar.class);
assertEquals(assetTypes.get(4), AspectAttributes.class);
}

@Test
Expand Down

0 comments on commit 53ed1f5

Please sign in to comment.