com.datahub.authorization.FieldResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datahub-auth-api Show documentation
Show all versions of datahub-auth-api Show documentation
DataHub Auth API for developers to write custom Authentication & Authorization plugins for DataHub
package com.datahub.authorization;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Value;
/**
* Helper class for lazy resolution of fields Input resolveField function that is given as input
* will only be called when getFieldValuesFuture is called
*/
@RequiredArgsConstructor
public class FieldResolver {
private final Supplier> resolveField;
@Getter(lazy = true)
private final CompletableFuture fieldValuesFuture = resolveField.get();
private static final FieldValue EMPTY = new FieldValue(Collections.emptySet());
/** Helper function that returns FieldResolver for precomputed values */
public static FieldResolver getResolverFromValues(Set values) {
return new FieldResolver(
() -> CompletableFuture.completedFuture(FieldValue.builder().values(values).build()));
}
/** Helper function that returns FieldResolver given a fetchFieldValue function */
public static FieldResolver getResolverFromFunction(
EntitySpec entitySpec, Function fetchFieldValue) {
return new FieldResolver(
() -> CompletableFuture.supplyAsync(() -> fetchFieldValue.apply(entitySpec)));
}
public static FieldValue emptyFieldValue() {
return EMPTY;
}
/**
* Container for storing the field value, in case we need to extend this to have more types of
* field values
*/
@Value
@Builder
public static class FieldValue {
Set values;
}
}