| 1 |
Parsing strings (Str): |
| 2 |
docs: scalar/string |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
StrictYAML parses to a YAML object, not |
| 6 |
the value directly to give you more flexibility |
| 7 |
and control over what you can do with the YAML. |
| 8 |
|
| 9 |
This is what that can object can do - in most |
| 10 |
cases if parsed as a string, it will behave in |
| 11 |
the same way. |
| 12 |
given: |
| 13 |
setup: | |
| 14 |
from strictyaml import Str, Map, load |
| 15 |
from ensure import Ensure |
| 16 |
|
| 17 |
schema = Map({"a": Str(), "b": Str(), "c": Str(), "d": Str()}) |
| 18 |
|
| 19 |
parsed = load(yaml_snippet, schema) |
| 20 |
yaml_snippet: | |
| 21 |
a: 1 |
| 22 |
b: yes |
| 23 |
c: â string |
| 24 |
d: | |
| 25 |
multiline string |
| 26 |
variations: |
| 27 |
Parses correctly: |
| 28 |
steps: |
| 29 |
- Run: |
| 30 |
code: | |
| 31 |
Ensure(parsed).equals( |
| 32 |
{"a": "1", "b": "yes", "c": u"â string", "d": "multiline string\n"} |
| 33 |
) |
| 34 |
|
| 35 |
Dict lookup cast to string: |
| 36 |
steps: |
| 37 |
- Run: |
| 38 |
code: Ensure(str(parsed["a"])).equals("1") |
| 39 |
|
| 40 |
Dict lookup cast to int: |
| 41 |
steps: |
| 42 |
- Run: |
| 43 |
code: | |
| 44 |
Ensure(int(parsed["a"])).equals(1) |
| 45 |
|
| 46 |
Dict lookup cast to bool impossible: |
| 47 |
steps: |
| 48 |
- Run: |
| 49 |
code: bool(parsed["a"]) |
| 50 |
raises: |
| 51 |
message: |- |
| 52 |
Cannot cast 'YAML(1)' to bool. |
| 53 |
Use bool(yamlobj.data) or bool(yamlobj.text) instead. |
| 54 |
|