Parsing Guide¶
Overview¶
This guide covers HCL parsing techniques with pyvider-hcl.
Basic Parsing¶
Parsing from Files¶
from pathlib import Path
from pyvider.hcl import parse_hcl_to_cty
content = Path("config.hcl").read_text()
result = parse_hcl_to_cty(content)
Enhanced Error Context¶
from pyvider.hcl import parse_with_context
content = file.read_text()
result = parse_with_context(content, source_file=file)
Type Inference vs Schemas¶
Automatic Inference¶
Best for: Prototyping, exploration
Schema Validation¶
Best for: Production, type safety
from pyvider.cty import CtyObject, CtyString
schema = CtyObject({"name": CtyString()})
result = parse_hcl_to_cty(hcl, schema=schema)
Handling Complex Structures¶
Nested Objects¶
hcl = """
server = {
config = {
host = "localhost"
port = 8080
}
}
"""
result = parse_hcl_to_cty(hcl)
Lists¶
Best Practices¶
- Always handle
HclParsingError - Use schemas for production code
- Use
parse_with_contextfor file parsing - Validate before processing