-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(urn-validation): Add UrnValidation PDL annotation
* Apply UrnValidation logic to StructuredPropertyDefinition valueType
- Loading branch information
1 parent
65376ee
commit 2c53b38
Showing
31 changed files
with
885 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
entity-registry/src/main/java/com/linkedin/metadata/models/UrnValidationFieldSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.linkedin.metadata.models; | ||
|
||
import com.linkedin.data.schema.DataSchema; | ||
import com.linkedin.data.schema.PathSpec; | ||
import com.linkedin.metadata.models.annotation.UrnValidationAnnotation; | ||
import javax.annotation.Nonnull; | ||
import lombok.Value; | ||
|
||
@Value | ||
public class UrnValidationFieldSpec { | ||
@Nonnull PathSpec path; | ||
@Nonnull UrnValidationAnnotation urnValidationAnnotation; | ||
@Nonnull DataSchema pegasusSchema; | ||
} |
57 changes: 57 additions & 0 deletions
57
...-registry/src/main/java/com/linkedin/metadata/models/UrnValidationFieldSpecExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.linkedin.metadata.models; | ||
|
||
import com.linkedin.data.schema.DataSchema; | ||
import com.linkedin.data.schema.DataSchemaTraverse; | ||
import com.linkedin.data.schema.PathSpec; | ||
import com.linkedin.data.schema.annotation.SchemaVisitor; | ||
import com.linkedin.data.schema.annotation.SchemaVisitorTraversalResult; | ||
import com.linkedin.data.schema.annotation.TraverserContext; | ||
import com.linkedin.metadata.models.annotation.UrnValidationAnnotation; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class UrnValidationFieldSpecExtractor implements SchemaVisitor { | ||
private final List<UrnValidationFieldSpec> urnValidationFieldSpecs = new ArrayList<>(); | ||
|
||
@Override | ||
public void callbackOnContext(TraverserContext context, DataSchemaTraverse.Order order) { | ||
if (context.getEnclosingField() == null) { | ||
return; | ||
} | ||
|
||
if (DataSchemaTraverse.Order.PRE_ORDER.equals(order)) { | ||
final DataSchema currentSchema = context.getCurrentSchema().getDereferencedDataSchema(); | ||
final PathSpec path = new PathSpec(context.getSchemaPathSpec()); | ||
|
||
// Check for @UrnValidation annotation in primary properties | ||
final Object urnValidationAnnotationObj = | ||
context.getEnclosingField().getProperties().get(UrnValidationAnnotation.ANNOTATION_NAME); | ||
|
||
// Check if it's either explicitly annotated with @UrnValidation | ||
if (urnValidationAnnotationObj != null) { | ||
addUrnValidationFieldSpec(currentSchema, path, urnValidationAnnotationObj); | ||
} | ||
} | ||
} | ||
|
||
private void addUrnValidationFieldSpec( | ||
DataSchema currentSchema, PathSpec path, Object annotationObj) { | ||
UrnValidationAnnotation annotation = | ||
UrnValidationAnnotation.fromPegasusAnnotationObject( | ||
annotationObj, FieldSpecUtils.getSchemaFieldName(path), path.toString()); | ||
|
||
urnValidationFieldSpecs.add(new UrnValidationFieldSpec(path, annotation, currentSchema)); | ||
} | ||
|
||
@Override | ||
public VisitorContext getInitialVisitorContext() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public SchemaVisitorTraversalResult getSchemaVisitorTraversalResult() { | ||
return new SchemaVisitorTraversalResult(); | ||
Check warning on line 55 in entity-registry/src/main/java/com/linkedin/metadata/models/UrnValidationFieldSpecExtractor.java
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...gistry/src/main/java/com/linkedin/metadata/models/annotation/UrnValidationAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.linkedin.metadata.models.annotation; | ||
|
||
import com.linkedin.metadata.models.ModelValidationException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import javax.annotation.Nonnull; | ||
import lombok.Value; | ||
|
||
@Value | ||
public class UrnValidationAnnotation { | ||
public static final String ANNOTATION_NAME = "UrnValidation"; | ||
boolean exist; | ||
boolean strict; | ||
List<String> entityTypes; | ||
|
||
@Nonnull | ||
public static UrnValidationAnnotation fromPegasusAnnotationObject( | ||
@Nonnull final Object annotationObj, | ||
@Nonnull final String schemaFieldName, | ||
@Nonnull final String context) { | ||
if (!Map.class.isAssignableFrom(annotationObj.getClass())) { | ||
throw new ModelValidationException( | ||
String.format( | ||
Check warning on line 24 in entity-registry/src/main/java/com/linkedin/metadata/models/annotation/UrnValidationAnnotation.java
|
||
"Failed to validate @%s annotation declared at %s: Invalid value type provided (Expected Map)", | ||
ANNOTATION_NAME, context)); | ||
} | ||
|
||
Map<String, ?> map = (Map<String, ?>) annotationObj; | ||
final Optional<Boolean> exist = AnnotationUtils.getField(map, "exist", Boolean.class); | ||
final Optional<Boolean> strict = AnnotationUtils.getField(map, "strict", Boolean.class); | ||
final List<String> entityTypes = AnnotationUtils.getFieldList(map, "entityTypes", String.class); | ||
|
||
return new UrnValidationAnnotation(exist.orElse(true), strict.orElse(true), entityTypes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.