use of org.ff4j.property.Property in project ff4j by ff4j.
the class YamlParser method parseProperties.
@SuppressWarnings("unchecked")
private Map<String, Property<?>> parseProperties(List<Map<String, Object>> properties) {
Map<String, Property<?>> result = new HashMap<>();
if (null != properties) {
properties.forEach(property -> {
// Initiate with name and value
String name = (String) property.get(PROPERTY_PARAMNAME);
if (null == name) {
throw new IllegalArgumentException("Invalid YAML File: 'name' is expected for properties");
}
Object objValue = property.get(PROPERTY_PARAMVALUE);
if (null == objValue) {
throw new IllegalArgumentException("Invalid YAML File: 'value' is expected for properties");
}
// Convert as a String
String strValue = String.valueOf(objValue);
if (objValue instanceof Date) {
strValue = SIMPLE_DATE_FORMAT.format((Date) objValue);
}
Property<?> ap = new PropertyString(name, strValue);
String optionalType = (String) property.get(PROPERTY_PARAMTYPE);
// If specific type defined ?
if (null != optionalType) {
// Substitution if relevant (e.g. 'int' -> 'org.ff4j.property.PropertyInt')
optionalType = MappingUtil.mapPropertyType(optionalType);
try {
// Constructor (String, String) is mandatory in Property interface
Constructor<?> constr = Class.forName(optionalType).getConstructor(String.class, String.class);
ap = (Property<?>) constr.newInstance(name, strValue);
} catch (Exception e) {
throw new IllegalArgumentException("Cannot instantiate '" + optionalType + "' check default constructor", e);
}
}
// Description
String description = (String) property.get(PROPERTY_PARAMDESCRIPTION);
if (null != description) {
ap.setDescription(description);
}
// Fixed Values
List<Object> fixedValues = (List<Object>) property.get(PROPERTY_PARAMFIXED_VALUES);
if (null != fixedValues && fixedValues.size() > 0) {
fixedValues.stream().map(Object::toString).forEach(ap::add2FixedValueFromString);
}
// Check fixed value
if (ap.getFixedValues() != null && !ap.getFixedValues().isEmpty() && !ap.getFixedValues().contains(ap.getValue())) {
throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues());
}
result.put(ap.getName(), ap);
});
}
return result;
}
use of org.ff4j.property.Property in project ff4j by ff4j.
the class MongoFeatureMapper method mapCustomProperties.
/**
* Custom Properties.
*
* @param dbObject
* db object
* @return
* list of property
*/
private Map<String, Property<?>> mapCustomProperties(Document dbObject) {
Map<String, Property<?>> mapOfCustomProperties = new HashMap<>();
if (dbObject.containsKey(FEATURE_CUSTOMPROPERTIES)) {
String properties = (String) dbObject.get(FEATURE_CUSTOMPROPERTIES);
BasicDBObject values = BasicDBObject.parse(properties);
for (Map.Entry<String, Object> entry : values.entrySet()) {
mapOfCustomProperties.put(entry.getKey(), PMAPPER.fromStore((DBObject) entry.getValue()));
}
}
return mapOfCustomProperties;
}
use of org.ff4j.property.Property in project ff4j by ff4j.
the class HBaseFeatureMapper method toStore.
/**
* {@inheritDoc}
*/
@Override
public Put toStore(Feature fp) {
Put put = new Put(Bytes.toBytes(fp.getUid()));
// uid
put.addColumn(B_FEATURES_CF_CORE, B_FEAT_UID, Bytes.toBytes(fp.getUid()));
// description
byte[] desc = (fp.getDescription() == null) ? null : Bytes.toBytes(fp.getDescription());
put.addColumn(B_FEATURES_CF_CORE, B_FEAT_DESCRIPTION, desc);
// enable
put.addColumn(B_FEATURES_CF_CORE, B_FEAT_ENABLE, Bytes.toBytes(fp.isEnable()));
// group
byte[] group = (fp.getGroup() == null) ? null : Bytes.toBytes(fp.getGroup());
put.addColumn(B_FEATURES_CF_CORE, B_FEAT_GROUPNAME, group);
// permission
put.addColumn(B_FEATURES_CF_CORE, B_FEAT_ROLES, Bytes.toBytes(JsonUtils.permissionsAsJson(fp.getPermissions())));
// Flipping strategy
put.addColumn(B_FEATURES_CF_CORE, B_FEAT_STRATEGY, Bytes.toBytes(JsonUtils.flippingStrategyAsJson(fp.getFlippingStrategy())));
// Custom Properties
if (fp.getCustomProperties() != null && !fp.getCustomProperties().isEmpty()) {
for (Map.Entry<String, Property<?>> customP : fp.getCustomProperties().entrySet()) {
if (customP.getValue() != null) {
put.addColumn(B_FEATURES_CF_PROPERTIES, Bytes.toBytes(customP.getKey()), Bytes.toBytes(customP.getValue().toJson()));
}
}
}
return put;
}
use of org.ff4j.property.Property in project ff4j by ff4j.
the class FeatureDynamoDBMapper method fromStore.
@Override
public Feature fromStore(DynamoDbFeature item) {
Feature feature = new Feature(item.getFeatureUid());
feature.setEnable(item.isEnable());
feature.setDescription(item.getDescription());
feature.setGroup(item.getGroupName());
String jsonFlippingStrategy = item.getFlippingStrategy();
if (Util.hasLength(jsonFlippingStrategy)) {
feature.setFlippingStrategy(FeatureJsonParser.parseFlipStrategyAsJson(feature.getUid(), jsonFlippingStrategy));
}
Set<String> perms = item.getPermissions();
feature.setPermissions(perms == null ? new HashSet<>() : perms);
Map<String, String> props = item.getProperties();
if (props != null) {
Map<String, Property<?>> customProperties = new HashMap<>();
for (Map.Entry<String, String> propString : props.entrySet()) {
customProperties.put(propString.getKey(), PropertyJsonParser.parseProperty(propString.getValue()));
}
feature.setCustomProperties(customProperties);
}
return feature;
}
use of org.ff4j.property.Property in project ff4j by ff4j.
the class JdbcFeatureStoreErrorTest method testcreateCustomKO.
@Test(expected = FeatureAccessException.class)
public void testcreateCustomKO() throws SQLException {
DataSource mockDS = Mockito.mock(DataSource.class);
doThrow(new SQLException()).when(mockDS).getConnection();
JdbcFeatureStore jrepo = new JdbcFeatureStore(mockDS);
jrepo.setDataSource(mockDS);
List<Property<?>> lp = new ArrayList<Property<?>>();
lp.add(new PropertyString("p1", "v1"));
lp.add(new PropertyString("p2", "v2"));
jrepo.createCustomProperties("F1", lp);
}
Aggregations