Z3R0:
私は、変換規則の要件は、実行時にJavaでコードをデータベースにJSON形式で提示しています。
例えば、
{
"id": "g-KqVJwrEMUYNOEVEnNxxqc",
"rules": [
{
"id": "r-2VC4YQOkYu-lxkGgMABRc",
"field": "firstName",
"value": "Steve",
"operator": "="
},
{
"id": "r-B2Dd6eHO1rsZ-t1mfPk33",
"field": "lastName",
"value": "Vai",
"operator": "="
}
],
"combinator": "and",
"not": false
}
JSONのキーは、手の前に知られているであろう。また、フィールドとオペレータの値が固定され、知られているであろう。
しかし、私は、コードに上記のいずれかのような何かを変換する方法へと困惑しています
inputObject.firstName.equals("Steve") && inputObject.lastName.equals("Vai")
任意のリードは、思考は高く評価されます!
OscarRyz:
あなたは、実行時にフィールドを評価するためのイントロスペクションを使用することができます
それは次のようになります
Command command = parseJson(input); // transform input into a java object
InputObject o = getItFromSomewhere();
bool finalResult;
// process each rule
for ( Rule r: command.rules ) {
var fieldValue = o.getClass().getField(r.field).get(o);
var currentResult;
switch(r.operator) {
case "=": currentResult = fieldValue.equals(r.value);
break;
case ">": currentResult = ....
..etc
}
// combine it with previous results;
switch(command.combinator) {
case "and":
finalResult = finalResult && currentResult;
break;
case "or":
finalResult = finalResult || currentResult;
}
}
System.out.println(finalResult);
明らかにこれは正確なコードではなく、単に実行時に動的にフィールドの値を取得し、それを評価する方法を示すこと。