carvel-dev / carvel-dev/ytt

When dict is accessed using attribute notation, hint using bracket notation

Open
#121 2 comments 0 reactions 0 assignees View on GitHub
enhancement error msg improvement
Dominant language
Go
Stars
1.9k
Forks
167
PR merge metrics
No merged PRs in 30d

Description

Sometimes, a user may not realize they are dealing with a Starlark dictionary and attempt to access it using attribute notation (as if it were a struct). When this happens, let's give them a helping hint.

For example:

`stable_config.json`
```json
{
"vpc_id": "pete",
"pks_subnet_ids": [ 1, 2, 3 ]
}
```

`cluster-template-paving.yaml`
```yaml
1: #@ load("@ytt:data", "data")
2: #@ load("@ytt:json", "json")
3:
4: #@ stable_config = json.decode(data.values.stable_config)
5: first_subnet_id: #@ stable_config.pks_subnet_ids[0]
```

yields...
```console
$ ytt --data-value-file stable_config=stable_config.json -f cluster-template-paving.yaml -f values.yml

ytt: Error:
- dict has no .pks_subnet_ids field or method
in
cluster-template-paving.yaml:5 | first_subnet_id: #@ stable_config.pks_subnet_ids[0]
```

Let's drop them a hint:
```console
$ ytt --data-value-file stable_config=stable_config.json -f cluster-template-paving.yaml -f values.yml

ytt: Error:
- dict has no .pks_subnet_ids field or method (hint: use bracket notation to get dictionary values. Example: ["pks_subnet_ids"])
in
cluster-template-paving.yaml:5 | first_subnet_id: #@ stable_config.pks_subnet_ids[0]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.