The most common case when resolving dynamic metadata is to have a dynamic
set of keys that identify each of the possible types to be resolved. At design time, the end user will configure the connector, select an operation, and choose one of those keys from the set that gets populated dynamically, and the selected key will become the MetadataKeyId for the metadata resolution.
Implementing the dynamic key’s resolution starts with a TypeKeysResolver:
public class RecordKeysResolver implements TypeKeysResolver {
@Override
public String getCategoryName() {
return "Records";
}
@Override
public Set<MetadataKey> getKeys(MetadataContext context) throws MetadataResolvingException, ConnectionException {
// First we need to obtain the key IDs that will be used.
// This is where you'd normally use `context.getConfig()`
// or `context.getConnection()` to obtain the entity id's
// from a remote service, for this demo the keys will be fixed
List<String> keyIds = Arrays.asList("Author_id", "BookList_id", "Book_id");
HashSet<MetadataKey> keys = new HashSet<>();
for (String id: keyIds){
// For each possible if, we create a new MetadataKey with that ID
MetadataKeyBuilder builder = MetadataKeyBuilder.newKey(id);
// Then, we add a DisplayName to that MetadataKey,
// this will be the name shown in the UI
builder.withDisplayName(StringUtils.removeEnd(id, "_id"));
//finally, add the key to the Set of known MetadataKeys
keys.add(builder.build());
}
return keys;
}
}
Above, all the elements that are required to create a dynamic set of MetadataKeys. The method to implement is getKeys, which outputs all the keys available to the user.
The id element, like Author_id is used internally and must be unique for each structure that you want to represent across all the resolvers. The displayName is the name to show to the user for that key, so it is expected to be more user friendly and readable.
After the resolver is defined, you can use it as an operation or a source by adding the @MetadataScope annotation with its keysResolver value at the class definition level and annotating the parameter that is the key with @MetadataKeyId. All metadata resolvers must belong to the same category in an operation or a source.
@MetadataScope(keysResolver = RecordKeysResolver.class)
public class OperationWithMetadataKey {
public void create(@MetadataKeyId String type,
@Config MyConfig config){
//...
}
}
@MetadataScope(keysResolver = RecordKeysResolver.class)
public class DocsMetadataSource extends Source<Map<String, Object>, Void> {
@MetadataKeyId
@Parameter
public String type;
//...
}
|
|
The previous example is the only way to support metadata keys for sources. Do not set the resolver at the @MetadataKeyId annotation when defining sources.
|
Setting the @MetadataScope annotation at the operation class definition level indicates that all of its operations provide metadata and that they must all have a parameter annotated with @MetadataKeyId because they inherit the resolver from the scope.
You can also set a key resolver for a single operation:
public class MyOperations {
public void operationWithMetadataKey(@MetadataKeyId(RecordKeysResolver.class) String type,
@Config MyConfig config){
//...
}
public void plainOperation(@Config MyConfig config){
//...
}
}
In addition, if there is a @MetadataScope annotation at the operation class definition level, you can set a value for the @MetadataKeyId annotation in a parameter to override the resolver defined at the scope level for a single operation:
@MetadataScope(keysResolver = ClassLevelResolver.class)
public class MyOperations {
public void operation(@MetadataKeyId String type, @Config MyConfig config){
// This operation will use the ClassLevelResolver
}
public void operationWithAnotherResolver(@MetadataKeyId(OverridingResolver.class) String type,
@Config MyConfig config){
// This operation will use the OverridingResolver
}
}
|
|
The two previous examples (setting the resolver at the parameter level and overriding the inherited resolver from the scope) work only when defining operations. These features are not available for sources.
|