use of com.yahoo.elide.core.type.Field in project elide by yahoo.
the class EntityBinding method getFieldName.
/**
* Returns name of field whether public member or method.
*
* @param fieldOrMethod field or method
* @return field or method name
*/
public static String getFieldName(AccessibleObject fieldOrMethod) {
if (fieldOrMethod instanceof Field) {
return ((Field) fieldOrMethod).getName();
}
Method method = (Method) fieldOrMethod;
String name = method.getName();
boolean hasValidParameterCount = method.getParameterCount() == 0 || isRequestScopeableMethod(method);
if (name.startsWith("get") && hasValidParameterCount) {
return StringUtils.uncapitalize(name.substring("get".length()));
}
if (name.startsWith("is") && hasValidParameterCount) {
return StringUtils.uncapitalize(name.substring("is".length()));
}
return null;
}
use of com.yahoo.elide.core.type.Field in project elide by yahoo.
the class EntityDictionary method setValue.
/**
* Invoke the set[fieldName] method on the target object OR set the field with the corresponding name.
* @param target The object which owns the field to set
* @param fieldName the field name to set or invoke equivalent set method
* @param value the value to set
*/
public void setValue(Object target, String fieldName, Object value) {
Type<?> targetClass = getType(target);
String targetType = getJsonAliasFor(targetClass);
String fieldAlias = fieldName;
try {
Type<?> fieldClass = getType(targetClass, fieldName);
String realName = getNameFromAlias(target, fieldName);
fieldAlias = (realName != null) ? realName : fieldName;
String setMethod = "set" + StringUtils.capitalize(fieldAlias);
Method method = EntityDictionary.findMethod(targetClass, setMethod, fieldClass);
method.invoke(target, coerce(target, value, fieldAlias, fieldClass));
} catch (IllegalAccessException e) {
throw new InvalidAttributeException(fieldAlias, targetType, e);
} catch (InvocationTargetException e) {
throw handleInvocationTargetException(e);
} catch (IllegalArgumentException | NoSuchMethodException noMethod) {
AccessibleObject accessor = getAccessibleObject(target, fieldAlias);
if (accessor != null && accessor instanceof Field) {
Field field = (Field) accessor;
try {
field.set(target, coerce(target, value, fieldAlias, field.getType()));
} catch (IllegalAccessException noField) {
throw new InvalidAttributeException(fieldAlias, targetType, noField);
}
} else {
throw new InvalidAttributeException(fieldAlias, targetType);
}
}
}
use of com.yahoo.elide.core.type.Field in project elide by yahoo.
the class EntityDictionary method getId.
/**
* Gets id.
*
* @param value the value
* @return the id
*/
public String getId(Object value) {
if (value == null) {
return null;
}
try {
AccessibleObject idField = null;
Type<?> valueClass = getType(value);
for (; idField == null && valueClass != null; valueClass = valueClass.getSuperclass()) {
try {
idField = getEntityBinding(valueClass).getIdField();
} catch (NullPointerException e) {
log.warn("Class: {} ID Field: {}", valueClass.getSimpleName(), idField);
}
}
Type<?> idClass;
Object idValue;
if (idField instanceof Field) {
idValue = ((Field) idField).get(value);
idClass = ((Field) idField).getType();
} else if (idField instanceof Method) {
idValue = ((Method) idField).invoke(value, (Object[]) null);
idClass = ((Method) idField).getReturnType();
} else {
return null;
}
Serde serde = serdeLookup.apply(((ClassType) idClass).getCls());
if (serde != null) {
return String.valueOf(serde.serialize(idValue));
}
return String.valueOf(idValue);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
return null;
}
}
use of com.yahoo.elide.core.type.Field in project elide by yahoo.
the class TableTypeTest method testHiddenDimension.
@Test
void testHiddenDimension() throws Exception {
Table testTable = Table.builder().table("table1").name("Table").dimension(Dimension.builder().name("dim1").type(Type.BOOLEAN).hidden(true).build()).build();
TableType testType = new TableType(testTable);
Field field = testType.getDeclaredField("dim1");
assertNotNull(field);
ColumnMeta columnMeta = field.getAnnotation(ColumnMeta.class);
assertNotNull(columnMeta);
assertTrue(columnMeta.isHidden());
}
use of com.yahoo.elide.core.type.Field in project elide by yahoo.
the class TableTypeTest method testMeasureAnnotations.
@Test
void testMeasureAnnotations() throws Exception {
Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2"));
Table testTable = Table.builder().table("table1").name("Table").measure(Measure.builder().type(Type.MONEY).category("category1").definition("SUM{{ price}}").hidden(false).friendlyName("Price").name("price").readAccess("Admin").description("A measure").tags(tags).build()).build();
TableType testType = new TableType(testTable);
Field field = testType.getDeclaredField("price");
assertNotNull(field);
ReadPermission readPermission = field.getAnnotation(ReadPermission.class);
assertEquals("Admin", readPermission.expression());
ColumnMeta columnMeta = field.getAnnotation(ColumnMeta.class);
assertEquals("A measure", columnMeta.description());
assertEquals("category1", columnMeta.category());
assertEquals("Price", columnMeta.friendlyName());
assertEquals(CardinalitySize.UNKNOWN, columnMeta.size());
assertEquals(tags, new HashSet<>(Arrays.asList(columnMeta.tags())));
MetricFormula metricFormula = field.getAnnotation(MetricFormula.class);
assertEquals("SUM{{price}}", metricFormula.value());
assertEquals(DefaultMetricProjectionMaker.class, metricFormula.maker());
}
Aggregations