All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.finn.unleash.repository.JsonToggleCollectionDeserializer Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
package no.finn.unleash.repository;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import no.finn.unleash.ActivationStrategy;
import no.finn.unleash.FeatureToggle;

import java.lang.reflect.Type;
import java.util.*;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

public class JsonToggleCollectionDeserializer implements JsonDeserializer {
    private static final Type PARAMS_TYPE = new TypeToken>() {}.getType();
    private  static final Type FEATURE_COLLECTION_TYPE = new TypeToken>() {}.getType();

    @Override
    public ToggleCollection deserialize(
            JsonElement rootElement,
            Type type,
            JsonDeserializationContext context) throws JsonParseException {

        int version = getVersion(rootElement);

        switch (version) {
            case 0:
                return deserializeVersion0(rootElement, context);
            case 1:
            default:
                return deserializeVersion1(rootElement, context);
        }
    }

    static ToggleCollection deserializeVersion0(JsonElement rootElement, JsonDeserializationContext context) {
        if(!rootElement.getAsJsonObject().has("features")) {
            return null;
        }

        Collection featureToggles = new ArrayList<>();

        JsonArray features = rootElement.getAsJsonObject().getAsJsonArray("features");

        features.forEach(elm -> {
            JsonObject featureObj = elm.getAsJsonObject();

            String name = featureObj.get("name").getAsString();
            boolean enabled = featureObj.get("enabled").getAsBoolean();
            String strategyName = featureObj.get("strategy").getAsString();
            Map strategyParams = context.deserialize(featureObj.get("parameters"), PARAMS_TYPE);

            ActivationStrategy strategy = new ActivationStrategy(strategyName, strategyParams);
            featureToggles.add(new FeatureToggle(name, enabled, singletonList(strategy), emptyList()));
        });

        return new ToggleCollection(featureToggles);
    }

    static ToggleCollection deserializeVersion1(JsonElement rootElement, JsonDeserializationContext context) {
        if(!rootElement.getAsJsonObject().has("features")) {
            return null;
        }

        JsonArray featureArray = rootElement.getAsJsonObject().getAsJsonArray("features");

        Collection featureTgggles =  context.deserialize(featureArray, FEATURE_COLLECTION_TYPE);
        return new ToggleCollection(featureTgggles);
    }

    private int getVersion(JsonElement rootElement) {
        if(!rootElement.getAsJsonObject().has("version")) {
            return 0;
        } else {
            return rootElement.getAsJsonObject().get("version").getAsInt();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy