wso2 - 在 Ballerina 中访问表数据结构时出现问题。错误无效操作...不支持非必填字段的字段访问
问题描述
我一直在学习芭蕾舞女演员,并且一直在学习这些例子。我在Ballerina By Example - Tables的文档中遇到以下示例的问题。
示例(和我的)中的代码如下:
import ballerina/io;
import ballerina/jsonutils;
import ballerina/xmlutils;
type Employee record {
int id;
string name;
float salary;
};
public function main() {
table<Employee> tbEmployee = table {
{key id, name, salary},
[
{1, "Mary", 300.5},
{2, "John", 200.5},
{3, "Jim", 330.5}
]
};
io:print("Table Information: ");
io:println(tbEmployee);
Employee e1 = {id: 1, name: "Jane", salary: 300.50};
Employee e2 = {id: 2, name: "Anne", salary: 100.50};
Employee e3 = {id: 3, name: "John", salary: 400.50};
Employee e4 = {id: 4, name: "Peter", salary: 150.0};
table<Employee> tb = table {
{key id, name, salary},
[
e1,
e2
]
};
Employee[] employees = [e3, e4];
foreach var emp in employees {
var ret = tb.add(emp);
if (ret is ()) {
io:println("Adding record to table successful");
} else {
io:println("Adding to table failed: ", ret.reason());
}
}
io:println("Table Information: ", tb);
io:println("Using foreach: ");
foreach var x in tb {
io:println("Name: ", x.name);
}
io:println("Using while loop: ");
while (tb.hasNext()) {
var ret = tb.getNext();
io:println("Name: ", ret.name);
}
json retValJson = jsonutils:fromTable(tb);
io:println("JSON: ", retValJson.toJsonString());
xml retValXml = xmlutils:fromTable(tb);
io:println("XML: ", retValXml);
int|error count = tb.remove(isHigherSalary);
io:println("Deleted Count: ", count);
io:println(tb);
}
function isHigherSalary(Employee emp) returns boolean {
return emp.salary > 300.0;
}
当我运行它时,我遇到以下行 (59) 的问题:
io:println("Name: ", ret.name);
error: .::tables_orig.bal:59:30: invalid operation: type 'record {| anydata...; |}' does not support field access for non-required field 'name'
我正在使用芭蕾舞女演员 1.0.5。我怎样才能解决这个问题?
解决方案
理想情况下,字段访问应该与 一起使用getNext()
,但是在如何确定getNext()
调用的类型方面存在问题。现已通过https://github.com/ballerina-platform/ballerina-lang/commit/8196d93023ba12958dfdd5f28a70c8da2147bf48修复此问题,该修复程序将在即将发布的 Ballerina 1.1.0 中提供。
推荐阅读
- python - 使用 python 为我的网站创建临时网页
- docker - 在 gitlab CI/CD 管道期间使用 docker 和 jmeter 构建和测试组件
- laravel - 如何在表单中预先检查具有 belongsToMany 关系的复选框?
- java - 在 .jsp 文件中键入时 Eclipse 冻结
- xamarin - Genymotion:错误“无法启动虚拟设备”
- video-streaming - 视频托管的服务器推荐
- python - for 循环中的计数器总是打印 0
- oracle - 以嵌套表为参数调用过程
- javascript - 有人可以给我一个箭头函数中的关键字“this”的例子,它指的是“一级”函数吗?
- c# - Azure 媒体服务:LiveEventInput 目的的 LiveEventEndpoints