Skip to content

Commit a9583f2

Browse files
committed
Support YAML booleans. Mustang now guesses yaml file path if left off.
1 parent 9e84cd0 commit a9583f2

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

examples/simple.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: Josh
2+
value: 1,000,000
3+
taxed_value: 800,000
4+
in_ca: Yes

mustang/YAMLContext.ooc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,26 @@ YAMLContext: class extends Context {
3434

3535
nodeToValue: static func(node: DocumentNode) -> Value {
3636
match node class {
37-
case ScalarNode => StringValue new((node as ScalarNode) value)
37+
case ScalarNode => scalarToValue(node as ScalarNode)
3838
case SequenceNode => sequenceToListValue(node as SequenceNode)
3939
case MappingNode => mappingToHashValue(node as MappingNode)
4040
case => Exception new("Unknown node type!") throw(); null
4141
}
4242
}
4343

44+
scalarToValue: static func(scalar: ScalarNode) -> Value {
45+
normalizedValue := scalar value trim() toLower()
46+
47+
//TODO: boolean detection should be moved into ooc-yaml (datatype detection)
48+
match normalizedValue {
49+
case "true" => BoolValue new(true)
50+
case "yes" => BoolValue new(true)
51+
case "false" => BoolValue new(false)
52+
case "no" => BoolValue new(false)
53+
case => StringValue new(scalar value)
54+
}
55+
}
56+
4457
sequenceToListValue: static func(seq: SequenceNode) -> ListValue {
4558
list := ListValue new()
4659

mustang/mustang.ooc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@ import structs/ArrayList
33
import mustang/[Template, YAMLContext]
44

55
main: func(args: ArrayList<String>) -> Int {
6-
if(args size() < 3) {
7-
"Usage: mustang <TEMPLATE> <YAML>" println()
6+
if(args size() < 2) {
7+
"Usage: mustang <TEMPLATE> [YAML]" println()
88
return 1
99
}
1010

11-
context := YAMLContext loadFromFile(args[2])
12-
template := Template loadFromFile(args[1])
11+
templateFile := args[1]
12+
yamlFile: String
13+
if(args size() != 3) {
14+
yamlFile = "%s.yaml" format(templateFile substring(0, templateFile lastIndexOf('.')))
15+
}
16+
else {
17+
yamlFile = args[2]
18+
}
19+
20+
context := YAMLContext loadFromFile(yamlFile)
21+
template := Template loadFromFile(templateFile)
1322
template render(context) println()
1423

1524
return 0

0 commit comments

Comments
 (0)