Skip to content

Commit 5280f11

Browse files
author
Patrick Thomson
committed
Added a little bit more predictive casting and another test.
1 parent 4fae9ab commit 5280f11

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/YKParser.m

+28-13
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,7 @@ - (NSArray *)parseWithError:(NSError **)e
7171
done = (event.type == YAML_STREAM_END_EVENT);
7272
switch(event.type) {
7373
case YAML_SCALAR_EVENT:
74-
obj = [NSString stringWithUTF8String:(const char *)event.data.scalar.value];
75-
76-
if(event.data.scalar.style == YAML_PLAIN_SCALAR_STYLE) {
77-
NSScanner *scanner = [NSScanner scannerWithString:obj];
78-
79-
// Integers are automatically casted unless given a !!str tag. I think.
80-
if([scanner scanInt:NULL]) {
81-
obj = [NSNumber numberWithInt:[obj intValue]];
82-
} else if([obj isEqualToString:@"~"]) {
83-
obj = [NSNull null];
84-
}
85-
}
86-
74+
obj = [self _interpretObjectFromEvent:event];
8775
temp = [stack lastObject];
8876

8977
if([temp isKindOfClass:[NSArray class]]) {
@@ -145,6 +133,33 @@ - (NSArray *)parseWithError:(NSError **)e
145133
return stack;
146134
}
147135

136+
// TODO: oof, add tag support.
137+
138+
- (id)_interpretObjectFromEvent:(yaml_event_t)event
139+
{
140+
id obj = [NSString stringWithUTF8String:(const char *)event.data.scalar.value];
141+
142+
if(event.data.scalar.style == YAML_PLAIN_SCALAR_STYLE) {
143+
NSScanner *scanner = [NSScanner scannerWithString:obj];
144+
145+
// Integers are automatically casted unless given a !!str tag. I think.
146+
if([scanner scanInt:NULL]) {
147+
obj = [NSNumber numberWithInt:[obj intValue]];
148+
} else if([scanner scanDouble:NULL]) {
149+
obj = [NSNumber numberWithDouble:[obj doubleValue]];
150+
// FIXME: Boolean parsing here is not in accordance with the YAML standards.
151+
} else if([obj caseInsensitiveCompare:@"true"] == NSOrderedSame) {
152+
obj = [NSNumber numberWithBool:YES];
153+
} else if([obj caseInsensitiveCompare:@"false"] == NSOrderedSame) {
154+
obj = [NSNumber numberWithBool:NO];
155+
} else if([obj isEqualToString:@"~"]) {
156+
obj = [NSNull null];
157+
}
158+
// TODO: add date parsing.
159+
}
160+
return obj;
161+
}
162+
148163
- (NSError *)_constructErrorFromParser:(yaml_parser_t *)p
149164
{
150165
int code = 0;

test/TestParsing.m

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ - (void)testModerateLoadingFromFile
3939
STAssertNotNil(o, @"#parse method failed to return anything.");
4040
NSDictionary *first = [o objectAtIndex:0];
4141
STAssertEqualObjects([first objectForKey:@"receipt"], @"Oz-Ware Purchase Invoice", @"recieved incorrect data from loaded YAML");
42+
STAssertTrue(([[first objectForKey:@"specialDelivery"] length] > 25), @"did not parse a multiline string correctly");
4243
}
4344

4445
- (void)testAutomaticIntegerCasting

0 commit comments

Comments
 (0)