Skip to content

Commit e99f4f7

Browse files
authored
Merge pull request #15 from infrablocks/module-variables
fix library generating types for variables of modules
2 parents 2eb1d09 + 73a528f commit e99f4f7

File tree

9 files changed

+81
-1
lines changed

9 files changed

+81
-1
lines changed

.github/workflows/pr.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Pull Request
2+
on:
3+
pull_request:
4+
5+
jobs:
6+
check:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- name: Install tools
11+
uses: ./.github/actions/asdf_install
12+
- name: Test
13+
run: ./go library:check
14+
test:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Install tools
19+
uses: ./.github/actions/asdf_install
20+
- name: Test
21+
run: ./go library:test:all
22+
build:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v3
26+
- name: Install tools
27+
uses: ./.github/actions/asdf_install
28+
- name: Test
29+
run: ./go library:build
30+

.idea/copilot.data.migration.ask2agent.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/with_module/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module "child" {
2+
source = "./modules/child"
3+
4+
sub_variable = "foo"
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "sub_variable" {
2+
value = var.sub_variable
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
variable "sub_variable" {
2+
}

examples/with_module/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
variable "root_string" {
2+
type = string
3+
}
4+
5+
variable "root_number" {
6+
type = number
7+
}

src/infrablocks/terraform_pkl_type_gen/pkl_generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class TFMeta(TypedDict):
77
label: str
88
filename: str
9+
path: str
910

1011

1112
class VariableMeta(TypedDict, total=False):
@@ -57,6 +58,7 @@ def generate_pkl_type(chdir: str, type_name: str) -> str:
5758
variable
5859
for variable in parsed["variable"]
5960
if variable["__tfmeta"]["filename"] == "variables.tf"
61+
and variable["__tfmeta"]["path"].startswith("variable.")
6062
]
6163
output: List[str] = []
6264

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from infrablocks.terraform_pkl_type_gen.pkl_generator import generate_pkl_type
2+
3+
with_module_example = generate_pkl_type(
4+
"examples/with_module", "WithModuleExample"
5+
).split("\n")
6+
7+
8+
class TestGeneratePklTypeWithModule:
9+
def test_includes_root_variables(self):
10+
labels = [
11+
line.split(":")[0].strip()
12+
for line in with_module_example
13+
if ":" in line and not line.startswith("//")
14+
]
15+
assert "root_string" in labels
16+
assert "root_number" in labels
17+
18+
def test_excludes_submodule_variables(self):
19+
labels = [
20+
line.split(":")[0].strip()
21+
for line in with_module_example
22+
if ":" in line and not line.startswith("//")
23+
]
24+
assert "sub_variable" not in labels

typings/tfparse/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ from tfparse._tfparse import ffi
1212
class TFMeta(tp.TypedDict):
1313
label: str
1414
filename: str
15+
path: str
1516

1617
class VariableMeta(tp.TypedDict, total=False):
1718
__tfmeta: tp.Required[TFMeta]
@@ -29,4 +30,4 @@ def load_from_path(
2930
allow_downloads: bool = False,
3031
workspace_name: str = "default",
3132
vars_paths: tp.List[str] | None = None,
32-
) -> Parsed: ...
33+
) -> Parsed: ...

0 commit comments

Comments
 (0)