这甚至可能吗?
你好朋友。我正在使用 AWS AppSync + DynamoDB 构建应用程序,并且我开始拥有大量解析器映射模板,所有这些模板都是使用 Apache Velocity 模板语言 (VTL) 编写的。
我开始担心的是,这些 vtl 文件对应用程序非常重要(因为它们定义了如何检索数据)并且其中一个错误可能会造成严重破坏。所以就像系统的任何关键部分一样......我想为他们编写一些自动化的单元测试。但我还没有发现其他人这样做。
提前致谢!
unit-testing vtl amazon-dynamodb aws-appsync velocity-template-language
我有一个像下面这样的java代码
public static void main(String args[]) throws Exception {
VelocityEngine engine = new VelocityEngine();
engine.init();
Template template = engine.getTemplate("userinfo.vm");
VelocityContext vc = new VelocityContext();
Map<String, Object> jsonResponse = new HashMap<String, Object>();
jsonResponse.put("44", "United Kingdom");
jsonResponse.put("33", "France");
jsonResponse.put("49", null);
vc.put("userList", jsonResponse);
StringWriter writer = new StringWriter();
template.merge(vc, writer);
System.out.println(writer);
}
Run Code Online (Sandbox Code Playgroud)
在 .vm 文件中
#foreach ($mapEntry in $userList.entrySet())
$!{mapEntry.get("44")}
#end
Run Code Online (Sandbox Code Playgroud)
在这里,我试图使用上面的代码获得特定的价值,但它没有给出预期的输出
我的预期输出是
United Kingdom
Run Code Online (Sandbox Code Playgroud) 我正在 AppSync GraphQL 查询的帮助下处理 DynamoDB。
我有一个 DynamoDB 表,其中用户名是分区键(哈希键),时间戳值是排序键(范围键)。
我针对一个项目保存两件事,即一件事是阅读,第二件事是活动(如锻炼、运动等)。为了添加这两件事,我们有不同的 UI 屏幕。这两件事可能具有相同的时间戳,因此它将保存在一项中。
所以现在我需要一个可用于上述操作的 upsert(插入或更新)查询,因为当您尝试插入新的读数时,它将检查该项目是否存在。如果存在,那么它将更新,或者不更新,那么它将插入该项目,并且当用户想要添加新活动时,必须发生同样的事情。
我对文档感到困惑,并且没有找到用于执行 upsert 操作的确切 AppSync 请求映射解析器。
下面是 PutItem 请求映射解析器:-
{
"version": "2017-02-28",
"operation": "PutItem",
"key": {
"username": $util.dynamodb.toDynamoDBJson($ctx.identity.username),
"timestamp": $util.dynamodb.toDynamoDBJson($ctx.args.input.timestamp),
},
"attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),
"condition": {
"expression": "attribute_not_exists(#timestamp)",
"expressionNames": {
"#timestamp": "timestamp",
},
},
}
Run Code Online (Sandbox Code Playgroud)
下面是 UpdateItem 请求映射解析器:-
{
"version": "2017-02-28",
"operation": "UpdateItem",
"key": {
"username": $util.dynamodb.toDynamoDBJson($ctx.identity.username),
"timestamp": $util.dynamodb.toDynamoDBJson($ctx.args.input.timestamp),
},
## Set up some space to keep track of things we're updating **
#set( $expNames …Run Code Online (Sandbox Code Playgroud) amazon-web-services amazon-dynamodb graphql aws-appsync velocity-template-language
我有一个java String,它基本上是一个速度模板。
String vt = "#foreach ($number in [1..34]) $number += $number #end"
String result = *String_vt_calculated_by_Velocity_Engine*;
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
如何在 Java 中计算上述字符串并得到结果?