-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_data.mojo
More file actions
39 lines (31 loc) · 993 Bytes
/
nested_data.mojo
File metadata and controls
39 lines (31 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Working with nested data from README.md
Demonstrates navigating nested mappings and sequences.
"""
from yaml import parse
fn main() raises:
var yaml_str = """
config:
database:
host: localhost
port: 5432
servers:
- name: web1
ip: "192.168.1.10"
- name: web2
ip: "192.168.1.11"
"""
var data = parse(yaml_str)
print("=== Navigate Nested Mappings ===")
var config = data.get("config")
var db = config.get("database")
print("Database host:", db.get("host").as_string()) # localhost
print("Database port:", db.get("port").as_int()) # 5432
print()
print("=== Navigate Sequences ===")
var servers = config.get("servers")
print("Number of servers:", len(servers.sequence_value))
for i in range(len(servers.sequence_value)):
var server = servers.get_at(i)
print("Server", i, ":")
print(" Name:", server.get("name").as_string())
print(" IP:", server.get("ip").as_string())