Skip to content

Commit

Permalink
chore: fix pmd violations (#856)
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
toddbaert authored Jul 2, 2024
1 parent a560308 commit f10d872
Show file tree
Hide file tree
Showing 8 changed files with 814 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static EnvironmentKeyTransformer replaceDotWithUnderscoreTransformer() {

public static EnvironmentKeyTransformer hyphenCaseToScreamingSnake() {
return new EnvironmentKeyTransformer(REPLACE_HYPHEN_WITH_UNDERSCORE)
.andThen(EnvironmentKeyTransformer.toUpperCaseTransformer());
.andThen(toUpperCaseTransformer());
}

public static EnvironmentKeyTransformer doNothing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@
import lombok.Builder;
import lombok.Getter;

import static dev.openfeature.contrib.providers.flagd.Config.BASE_EVENT_STREAM_RETRY_BACKOFF_MS;
import static dev.openfeature.contrib.providers.flagd.Config.BASE_EVENT_STREAM_RETRY_BACKOFF_MS_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.CACHE_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.DEADLINE_MS_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_CACHE;
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_DEADLINE;
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_HOST;
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_MAX_CACHE_SIZE;
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_MAX_EVENT_STREAM_RETRIES;
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_TLS;
import static dev.openfeature.contrib.providers.flagd.Config.HOST_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.MAX_CACHE_SIZE_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.MAX_EVENT_STREAM_RETRIES_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.OFFLINE_SOURCE_PATH;
import static dev.openfeature.contrib.providers.flagd.Config.PORT_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.SERVER_CERT_PATH_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.SOCKET_PATH_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.SOURCE_SELECTOR_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.TLS_ENV_VAR_NAME;
import static dev.openfeature.contrib.providers.flagd.Config.fallBackToEnvOrDefault;
import static dev.openfeature.contrib.providers.flagd.Config.fromValueProvider;

Expand All @@ -44,7 +25,7 @@ public class FlagdOptions {
* flagd connection host.
*/
@Builder.Default
private String host = fallBackToEnvOrDefault(HOST_ENV_VAR_NAME, DEFAULT_HOST);
private String host = fallBackToEnvOrDefault(Config.HOST_ENV_VAR_NAME, Config.DEFAULT_HOST);

/**
* flagd connection port.
Expand All @@ -55,75 +36,76 @@ public class FlagdOptions {
* Use TLS connectivity.
*/
@Builder.Default
private boolean tls = Boolean.parseBoolean(fallBackToEnvOrDefault(TLS_ENV_VAR_NAME, DEFAULT_TLS));
private boolean tls = Boolean.parseBoolean(fallBackToEnvOrDefault(Config.TLS_ENV_VAR_NAME, Config.DEFAULT_TLS));

/**
* TLS certificate overriding if TLS connectivity is used.
*/
@Builder.Default
private String certPath = fallBackToEnvOrDefault(SERVER_CERT_PATH_ENV_VAR_NAME, null);
private String certPath = fallBackToEnvOrDefault(Config.SERVER_CERT_PATH_ENV_VAR_NAME, null);

/**
* Unix socket path to flagd.
*/
@Builder.Default
private String socketPath = fallBackToEnvOrDefault(SOCKET_PATH_ENV_VAR_NAME, null);
private String socketPath = fallBackToEnvOrDefault(Config.SOCKET_PATH_ENV_VAR_NAME, null);

/**
* Cache type to use. Supports - lru, disabled.
*/
@Builder.Default
private String cacheType = fallBackToEnvOrDefault(CACHE_ENV_VAR_NAME, DEFAULT_CACHE);
private String cacheType = fallBackToEnvOrDefault(Config.CACHE_ENV_VAR_NAME, Config.DEFAULT_CACHE);

/**
* Max cache size.
*/
@Builder.Default
private int maxCacheSize = fallBackToEnvOrDefault(MAX_CACHE_SIZE_ENV_VAR_NAME, DEFAULT_MAX_CACHE_SIZE);
private int maxCacheSize = fallBackToEnvOrDefault(Config.MAX_CACHE_SIZE_ENV_VAR_NAME,
Config.DEFAULT_MAX_CACHE_SIZE);

/**
* Max event stream connection retries.
*/
@Builder.Default
private int maxEventStreamRetries =
fallBackToEnvOrDefault(MAX_EVENT_STREAM_RETRIES_ENV_VAR_NAME, DEFAULT_MAX_EVENT_STREAM_RETRIES);
private int maxEventStreamRetries = fallBackToEnvOrDefault(Config.MAX_EVENT_STREAM_RETRIES_ENV_VAR_NAME,
Config.DEFAULT_MAX_EVENT_STREAM_RETRIES);

/**
* Backoff interval in milliseconds.
*/
@Builder.Default
private int retryBackoffMs =
fallBackToEnvOrDefault(BASE_EVENT_STREAM_RETRY_BACKOFF_MS_ENV_VAR_NAME, BASE_EVENT_STREAM_RETRY_BACKOFF_MS);

private int retryBackoffMs = fallBackToEnvOrDefault(Config.BASE_EVENT_STREAM_RETRY_BACKOFF_MS_ENV_VAR_NAME,
Config.BASE_EVENT_STREAM_RETRY_BACKOFF_MS);

/**
* Connection deadline in milliseconds.
* For RPC resolving, this is the deadline to connect to flagd for flag evaluation.
* For RPC resolving, this is the deadline to connect to flagd for flag
* evaluation.
* For in-process resolving, this is the deadline for sync stream termination.
*/
@Builder.Default
private int deadline = fallBackToEnvOrDefault(DEADLINE_MS_ENV_VAR_NAME, DEFAULT_DEADLINE);
private int deadline = fallBackToEnvOrDefault(Config.DEADLINE_MS_ENV_VAR_NAME, Config.DEFAULT_DEADLINE);

/**
* Selector to be used with flag sync gRPC contract.
**/
@Builder.Default
private String selector = fallBackToEnvOrDefault(SOURCE_SELECTOR_ENV_VAR_NAME, null);
private String selector = fallBackToEnvOrDefault(Config.SOURCE_SELECTOR_ENV_VAR_NAME, null);

/**
* File source of flags to be used by offline mode.
* Setting this enables the offline mode of the in-process provider.
*/
@Builder.Default
private String offlineFlagSourcePath = fallBackToEnvOrDefault(OFFLINE_SOURCE_PATH, null);
private String offlineFlagSourcePath = fallBackToEnvOrDefault(Config.OFFLINE_SOURCE_PATH, null);

/**
* Inject OpenTelemetry for the library runtime. Providing sdk will initiate distributed tracing for flagd grpc
* Inject OpenTelemetry for the library runtime. Providing sdk will initiate
* distributed tracing for flagd grpc
* connectivity.
*/
private OpenTelemetry openTelemetry;


/**
* Builder overwrite in order to customize the "build" method.
*
Expand All @@ -144,7 +126,8 @@ public FlagdOptions build() {
*/
public static class FlagdOptionsBuilder {
/**
* Enable OpenTelemetry instance extraction from GlobalOpenTelemetry. Note that, this is only useful if global
* Enable OpenTelemetry instance extraction from GlobalOpenTelemetry. Note that,
* this is only useful if global
* configurations are registered.
*/
public FlagdOptionsBuilder withGlobalTelemetry(final boolean b) {
Expand All @@ -160,7 +143,8 @@ void prebuild() {
}

if (port == 0) {
port = Integer.parseInt(fallBackToEnvOrDefault(PORT_ENV_VAR_NAME, determineDefaultPortForResolver()));
port = Integer
.parseInt(fallBackToEnvOrDefault(Config.PORT_ENV_VAR_NAME, determineDefaultPortForResolver()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package dev.openfeature.contrib.providers.flagd.resolver.grpc;

import static dev.openfeature.contrib.providers.flagd.Config.CACHED_REASON;
import static dev.openfeature.contrib.providers.flagd.Config.CONTEXT_FIELD;
import static dev.openfeature.contrib.providers.flagd.Config.FLAG_KEY_FIELD;
import static dev.openfeature.contrib.providers.flagd.Config.METADATA_FIELD;
import static dev.openfeature.contrib.providers.flagd.Config.REASON_FIELD;
import static dev.openfeature.contrib.providers.flagd.Config.STATIC_REASON;
import static dev.openfeature.contrib.providers.flagd.Config.VALUE_FIELD;
import static dev.openfeature.contrib.providers.flagd.Config.VARIANT_FIELD;
import dev.openfeature.contrib.providers.flagd.Config;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -157,15 +150,15 @@ private <ValT, ReqT extends Message, ResT extends Message> ProviderEvaluation<Va
if (this.cacheAvailable()) {
ProviderEvaluation<? extends Object> fromCache = this.cache.get(key);
if (fromCache != null) {
fromCache.setReason(CACHED_REASON);
fromCache.setReason(Config.CACHED_REASON);
return (ProviderEvaluation<ValT>) fromCache;
}
}

// build the gRPC request
Message req = request.newBuilderForType()
.setField(getFieldDescriptor(request, FLAG_KEY_FIELD), key)
.setField(getFieldDescriptor(request, CONTEXT_FIELD), convertContext(ctx))
.setField(getFieldDescriptor(request, Config.FLAG_KEY_FIELD), key)
.setField(getFieldDescriptor(request, Config.CONTEXT_FIELD), convertContext(ctx))
.build();

final Message response;
Expand All @@ -178,16 +171,16 @@ private <ValT, ReqT extends Message, ResT extends Message> ProviderEvaluation<Va
}

// parse the response
ValT value = converter == null ? getField(response, VALUE_FIELD)
: converter.convert(getField(response, VALUE_FIELD));
ValT value = converter == null ? getField(response, Config.VALUE_FIELD)
: converter.convert(getField(response, Config.VALUE_FIELD));

// Extract metadata from response
ImmutableMetadata immutableMetadata = metadataFromResponse(response);

ProviderEvaluation<ValT> result = ProviderEvaluation.<ValT>builder()
.value(value)
.variant(getField(response, VARIANT_FIELD))
.reason(getField(response, REASON_FIELD))
.variant(getField(response, Config.VARIANT_FIELD))
.reason(getField(response, Config.REASON_FIELD))
.flagMetadata(immutableMetadata)
.build();

Expand All @@ -202,7 +195,7 @@ private <ValT, ReqT extends Message, ResT extends Message> ProviderEvaluation<Va
private <T> Boolean isEvaluationCacheable(ProviderEvaluation<T> evaluation) {
String reason = evaluation.getReason();

return reason != null && reason.equals(STATIC_REASON) && this.cacheAvailable();
return reason != null && reason.equals(Config.STATIC_REASON) && this.cacheAvailable();
}

private Boolean cacheAvailable() {
Expand All @@ -221,7 +214,8 @@ private static Value convertObjectResponse(Struct protobuf) {
*/
private static Struct convertContext(EvaluationContext ctx) {
Map<String, Value> ctxMap = ctx.asMap();
// asMap() does not provide explicitly set targeting key (ex:- new ImmutableContext("TargetingKey") ).
// asMap() does not provide explicitly set targeting key (ex:- new
// ImmutableContext("TargetingKey") ).
// Hence, we add this explicitly here for targeting rule processing.
ctxMap.put("targetingKey", new Value(ctx.getTargetingKey()));

Expand Down Expand Up @@ -348,7 +342,7 @@ private static Descriptors.FieldDescriptor getFieldDescriptor(Message message, S
}

private static ImmutableMetadata metadataFromResponse(Message response) {
final Object metadata = response.getField(getFieldDescriptor(response, METADATA_FIELD));
final Object metadata = response.getField(getFieldDescriptor(response, Config.METADATA_FIELD));

if (!(metadata instanceof Struct)) {
return ImmutableMetadata.builder().build();
Expand All @@ -373,7 +367,7 @@ private static ImmutableMetadata metadataFromResponse(Message response) {

private OpenFeatureError mapError(Exception e) {
if (e instanceof StatusRuntimeException) {
Code code = ((StatusRuntimeException)e).getStatus().getCode();
Code code = ((StatusRuntimeException) e).getStatus().getCode();
switch (code) {
case DATA_LOSS:
return new ParseError(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/**
* {@link TracedResolving} a request to response resolver with tracing for telemetry.
*/
@SuppressWarnings("PMD.UnusedLocalVariable")
public class TracedResolving implements ResolveStrategy {

private final Tracer tracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
private static String distributeValue(
final String hashKey,
final List<FractionProperty> propertyList,
int totalWeight
) throws JsonLogicEvaluationException {
int totalWeight) throws JsonLogicEvaluationException {
byte[] bytes = hashKey.getBytes(StandardCharsets.UTF_8);
int mmrHash = MurmurHash3.hash32x86(bytes, 0, bytes.length, 0);
float bucket = (Math.abs(mmrHash) * 1.0f / Integer.MAX_VALUE) * 100;
float bucket = Math.abs(mmrHash) * 1.0f / Integer.MAX_VALUE * 100;

float bucketSum = 0;
for (FractionProperty p : propertyList) {
Expand All @@ -90,7 +89,7 @@ private static String distributeValue(
}

@Getter
@SuppressWarnings({"checkstyle:NoFinalizer"})
@SuppressWarnings({ "checkstyle:NoFinalizer" })
private static class FractionProperty {
private final String variant;
private final int weight;
Expand Down
Loading

0 comments on commit f10d872

Please sign in to comment.