Skip to content

Parsing Guide

Overview

This guide covers HCL parsing techniques with pyvider-hcl.

Basic Parsing

from pyvider.hcl import parse_hcl_to_cty

hcl = 'name = "example"'
result = parse_hcl_to_cty(hcl)

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

result = parse_hcl_to_cty(hcl)  # Types inferred automatically

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

hcl = 'tags = ["prod", "api"]'
result = parse_hcl_to_cty(hcl)

Best Practices

  1. Always handle HclParsingError
  2. Use schemas for production code
  3. Use parse_with_context for file parsing
  4. Validate before processing

See Also