Groovy : 坚持使用可变参数设计 DSL
Groovy : Stuck in designing DSL with variable arguments
我需要使用 groovy 链式命令设计 DSL(领域特定语言)。
我有以下员工数据库
Name Age Date-Of-Joining Salary
Test 24 12-aug-2015 6000$
我需要创建一个类似于 :-
的 DSL
将姓名更新为 "test1" ,将年龄更新为“26” 等等。
问题是:- 可以更新的字段是用户定义的,即 he/she 可以选择,将更新哪些列及其动态。
所以 :- 将工资更新为“7000 美元”也应该可行。
是否可以创建这样的动态 dsl?如果是,请提供一些简要信息,以便继续进行此类设计
我已经开发了将更新值的后端系统。
我不知道如何从这个动态 dsl 中获取值
在 DSL 中,您应该有一些关于应该更新哪些员工记录的信息(例如,提供员工的主键 table)。
您希望在 DSL 中拥有的活力在 Groovy 中绝对是可能的。以这个小例子为起点(为了简单起见,这个例子没有使用数据库):
class Employee
{
String name
int age
int salary
public String toString()
{
"name=${name}, age=${age}, salary=${salary}$"
}
def update(String attributeName)
{
['to': { Object value ->
this[attributeName] = value
['and': { String nextAttrName ->
update(nextAttrName)
}]
}]
}
}
Employee emp = new Employee(name: 'Test', age: 24, salary: 6000)
println emp // name=Test, age=24, salary=6000$
emp.with
{
update 'name' to 'John'
println emp // name=John, age=24, salary=6000$
update 'salary' to 7000
println emp // name=John, age=24, salary=7000$
update 'name' to 'Michael' and 'age' to 48
println emp // name=Michael, age=48, salary=7000$
}
您的 DSL 将执行 table 代码,因此您不会 'extract' 值,但 DSL 将调用您的后端方法。
Groovy 中有关于创建 DSL 的非常好的介绍:Creating Groovy DSLs that Developers Can Actually Use。此演示文稿讲授了很多有关 DSL 内部原理的知识。
我需要使用 groovy 链式命令设计 DSL(领域特定语言)。
我有以下员工数据库
Name Age Date-Of-Joining Salary
Test 24 12-aug-2015 6000$
我需要创建一个类似于 :-
的 DSL将姓名更新为 "test1" ,将年龄更新为“26” 等等。
问题是:- 可以更新的字段是用户定义的,即 he/she 可以选择,将更新哪些列及其动态。
所以 :- 将工资更新为“7000 美元”也应该可行。
是否可以创建这样的动态 dsl?如果是,请提供一些简要信息,以便继续进行此类设计
我已经开发了将更新值的后端系统。
我不知道如何从这个动态 dsl 中获取值
在 DSL 中,您应该有一些关于应该更新哪些员工记录的信息(例如,提供员工的主键 table)。
您希望在 DSL 中拥有的活力在 Groovy 中绝对是可能的。以这个小例子为起点(为了简单起见,这个例子没有使用数据库):
class Employee
{
String name
int age
int salary
public String toString()
{
"name=${name}, age=${age}, salary=${salary}$"
}
def update(String attributeName)
{
['to': { Object value ->
this[attributeName] = value
['and': { String nextAttrName ->
update(nextAttrName)
}]
}]
}
}
Employee emp = new Employee(name: 'Test', age: 24, salary: 6000)
println emp // name=Test, age=24, salary=6000$
emp.with
{
update 'name' to 'John'
println emp // name=John, age=24, salary=6000$
update 'salary' to 7000
println emp // name=John, age=24, salary=7000$
update 'name' to 'Michael' and 'age' to 48
println emp // name=Michael, age=48, salary=7000$
}
您的 DSL 将执行 table 代码,因此您不会 'extract' 值,但 DSL 将调用您的后端方法。
Groovy 中有关于创建 DSL 的非常好的介绍:Creating Groovy DSLs that Developers Can Actually Use。此演示文稿讲授了很多有关 DSL 内部原理的知识。