yaml - 如何在 Ada 中读取 yaml 文件
问题描述
我试图了解如何使用 Ada 和 AdaYaml 库(https://github.com/yaml/AdaYaml/)。我有几个 yaml 文件,我需要将它们转换为 Ada 记录以进行进一步处理。
在玩了几个小时的库后,我不知道如何使用 Accessor 访问 yaml 数据。
我最小的工作代码是
with Ada.Text_IO;
with Ada.Command_Line;
with Yaml.Source.File;
with Yaml.Source.Text_IO;
with Yaml.Dom.Loading;
with Yaml.Dom.Node;
with Yaml; use Yaml;
with Text; use Text;
procedure Load_Dom is
Input : Source.Pointer;
begin
if Ada.Command_Line.Argument_Count = 0 then
Input := Yaml.Source.Text_IO.As_Source (Ada.Text_IO.Standard_Input);
else
Input := Yaml.Source.File.As_Source (Ada.Command_Line.Argument (1));
end if;
declare
Document : Yaml.Dom.Document_Reference :=
Yaml.Dom.Loading.From_Source (Input);
begin
Ada.Text_IO.Put_Line ("Root node is a " & Yaml.Dom.Node.Instance'(Document.Root.Value).Kind'Img);
Ada.Text_IO.Put_Line ("Root node is a " & Yaml.Dom.Node.Instance'(Document.Root.Value).Tag);
Ada.Text_IO.Put_Line ("Root node is a " & Yaml.Dom.Node.Instance'(Document.Root.Value).Mapping_Style'Img);
end;
end Load_Dom;
我认为显式转换Yaml.Dom.Node.Instance'(Document.Root.Value)
是不正确的,我必须遗漏一些东西。
任何想法或代码示例如何以正确的方式读取 yaml 文件?
解决方案
我是 AdaYaml 的作者。
返回的Accessor
whichDocument.Root.Value
定义如下:
type Accessor (Data : not null access Node.Instance) is limited private
with Implicit_Dereference => Data;
与任何具有判别式的类型一样,您可以Node.Instance
通过其名称访问Data
. 所以这个显式表达式检索根节点的种类:
Document.Root.Value.Data.all.Kind
现在 Ada 允许我们使指针取消引用隐式,删除.all
:
Document.Root.Value.Data.Kind
Implicit_Dereference
属性允许Accessor
我们删除.Data
:
Document.Root.Value.Kind
所以你想做的是
declare
Document : Yaml.Dom.Document_Reference :=
Yaml.Dom.Loading.From_Source (Input);
Root : Yaml.Dom.Accessor := Document.Root.Value;
begin
Ada.Text_IO.Put_Line ("Root node is a " & Root.Kind'Img);
Ada.Text_IO.Put_Line ("Root node is a " & Root.Tag);
Ada.Text_IO.Put_Line ("Root node is a " & Root.Mapping_Style'Img);
end;
推荐阅读
- amazon-web-services - 如何生成与任何控制台操作等效的 AWS CLI
- tensorflow - Tensorflow:如何通过修剪阻止小值泄漏?
- tensorflow - 如何使用训练有素的估计器在张量流中进行预测
- reactjs - Emotion 11 使用打字稿访问主题对象并做出反应
- twilio - Twilio:如何启用 Alpha SMS 发件人 ID?
- python - 在不使用循环的情况下迭代元组数组
- android - 树莓派如何从蓝牙设备接收和读取消息(令牌)?
- javascript - 使用 Illustrator 脚本将 CSV 数据输入到图层中
- javascript - 我的观点没有出现在我的 Java3D 世界中?
- php - 如何在 PHP 表单中追加/插入新的隐藏输入