Skip to content

Commit 5bb07f1

Browse files
authored
Refactor Parser and YamlReader (#176)
* refactor(parser): decompose conditional for implicit document for initProductionTable method * refactor(yaml-reader): rename variable anchors to anchorMap Makes the name more descriptive and readable. By reading the name you can tell it's a hashmap which was not clear previously. * refactor(yaml-reader): extract method readScalarValue from readValueInternal Extract out the deserialization of scalar values to a new method. Make readValueInternal more modular * refactor(yaml-reader): rename method valueConvertedNumber to parseNumber The method valueConvertedNumber has been renamed to parseNumber for improved clarity and consistency with naming conventions. Enhances the readability of the codebase and better reflects the purpose of the method, which is to parse a string value into a Number object. * revert "refactor(yaml-reader): rename variable anchors to anchorMap" This reverts commit 78d10cd.
1 parent 763726d commit 5bb07f1

2 files changed

Lines changed: 66 additions & 40 deletions

File tree

src/com/esotericsoftware/yamlbeans/YamlReader.java

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,55 @@ protected Class<?> findTagClass (String tag, ClassLoader classLoader) throws Cla
227227
return Class.forName(tag, true, classLoader);
228228
}
229229

230+
/**
231+
* Reads a scalar value from the parser and converts it to the specified type.
232+
* @param type The type to convert the scalar to.
233+
* @param anchor The anchor of the scalar, or null.
234+
* @return The converted scalar value.
235+
* @throws YamlException
236+
* @throws ParserException
237+
*/
238+
private Object readScalarValue(Class<?> type, String anchor) throws YamlException, ParserException {
239+
Event event = parser.getNextEvent();
240+
if (event.type != SCALAR) {
241+
throw new YamlReaderException("Expected scalar for primitive type '" + type.getClass() + "' but found: " + event.type);
242+
}
243+
244+
String value = ((ScalarEvent) event).value;
245+
try {
246+
Object convertedValue;
247+
if (value == null) {
248+
convertedValue = null;
249+
} else if (type == String.class) {
250+
convertedValue = value;
251+
} else if (type == Integer.TYPE || type == Integer.class) {
252+
convertedValue = Integer.decode(value);
253+
} else if (type == Boolean.TYPE || type == Boolean.class) {
254+
convertedValue = Boolean.valueOf(value);
255+
} else if (type == Float.TYPE || type == Float.class) {
256+
convertedValue = Float.valueOf(value);
257+
} else if (type == Double.TYPE || type == Double.class) {
258+
convertedValue = Double.valueOf(value);
259+
} else if (type == Long.TYPE || type == Long.class) {
260+
convertedValue = Long.decode(value);
261+
} else if (type == Short.TYPE || type == Short.class) {
262+
convertedValue = Short.decode(value);
263+
} else if (type == Character.TYPE || type == Character.class) {
264+
convertedValue = value.charAt(0);
265+
} else if (type == Byte.TYPE || type == Byte.class) {
266+
convertedValue = Byte.decode(value);
267+
} else {
268+
throw new YamlException("Unknown field type.");
269+
}
270+
if (anchor != null) {
271+
addAnchor(anchor, convertedValue);
272+
}
273+
return convertedValue;
274+
} catch (Exception ex) {
275+
throw new YamlReaderException("Unable to convert value to required type \"" + type + "\": " + value, ex);
276+
}
277+
}
278+
230279
private Object readValueInternal (Class type, Class elementType, String anchor)
231280
throws YamlException, ParserException, TokenizerException {
232281
if (type == null || type == Object.class) {
@@ -239,7 +288,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
239288
if (config.readConfig.guessNumberTypes) {
240289
String value = ((ScalarEvent)event).value;
241290
if (value != null) {
242-
Number number = valueConvertedNumber(value);
291+
Number number = parseNumber(value);
243292
if (number != null) {
244293
if (anchor != null) addAnchor(anchor, number);
245294
parser.getNextEvent();
@@ -258,39 +307,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
258307
}
259308

260309
if (Beans.isScalar(type)) {
261-
Event event = parser.getNextEvent();
262-
if (event.type != SCALAR) throw new YamlReaderException(
263-
"Expected scalar for primitive type '" + type.getClass() + "' but found: " + event.type);
264-
String value = ((ScalarEvent)event).value;
265-
try {
266-
Object convertedValue;
267-
if (value == null) {
268-
convertedValue = null;
269-
} else if (type == String.class) {
270-
convertedValue = value;
271-
} else if (type == Integer.TYPE || type == Integer.class) {
272-
convertedValue = Integer.decode(value);
273-
} else if (type == Boolean.TYPE || type == Boolean.class) {
274-
convertedValue = Boolean.valueOf(value);
275-
} else if (type == Float.TYPE || type == Float.class) {
276-
convertedValue = Float.valueOf(value);
277-
} else if (type == Double.TYPE || type == Double.class) {
278-
convertedValue = Double.valueOf(value);
279-
} else if (type == Long.TYPE || type == Long.class) {
280-
convertedValue = Long.decode(value);
281-
} else if (type == Short.TYPE || type == Short.class) {
282-
convertedValue = Short.decode(value);
283-
} else if (type == Character.TYPE || type == Character.class) {
284-
convertedValue = value.charAt(0);
285-
} else if (type == Byte.TYPE || type == Byte.class) {
286-
convertedValue = Byte.decode(value);
287-
} else
288-
throw new YamlException("Unknown field type.");
289-
if (anchor != null) addAnchor(anchor, convertedValue);
290-
return convertedValue;
291-
} catch (Exception ex) {
292-
throw new YamlReaderException("Unable to convert value to required type \"" + type + "\": " + value, ex);
293-
}
310+
return readScalarValue(type, anchor);
294311
}
295312

296313
for (Entry<Class, ScalarSerializer> entry : config.scalarSerializers.entrySet()) {
@@ -486,7 +503,7 @@ public YamlReaderException (String message) {
486503
}
487504
}
488505

489-
private Number valueConvertedNumber (String value) {
506+
private Number parseNumber(String value) {
490507
Number number = null;
491508
try {
492509
number = Long.decode(value);

src/com/esotericsoftware/yamlbeans/parser/Parser.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,24 @@ public Event produce () {
127127
};
128128
table[P_IMPLICIT_DOCUMENT] = new Production() {
129129
public Event produce () {
130-
TokenType type = tokenizer.peekNextTokenType();
131-
if (!(type == DIRECTIVE || type == DOCUMENT_START || type == STREAM_END)) {
132-
parseStack.add(0, table[P_DOCUMENT_END]);
133-
parseStack.add(0, table[P_BLOCK_NODE]);
134-
parseStack.add(0, table[P_DOCUMENT_START_IMPLICIT]);
130+
if (isNextTokenImplicitDocument()) {
131+
parseImplicitDocument();
135132
}
136133
return null;
137134
}
135+
136+
private boolean isNextTokenImplicitDocument() {
137+
TokenType type = tokenizer.peekNextTokenType();
138+
return type != DIRECTIVE && type != DOCUMENT_START && type != STREAM_END;
139+
}
140+
141+
private void parseImplicitDocument() {
142+
parseStack.add(0, table[P_DOCUMENT_END]);
143+
parseStack.add(0, table[P_BLOCK_NODE]);
144+
parseStack.add(0, table[P_DOCUMENT_START_IMPLICIT]);
145+
}
138146
};
147+
139148
table[P_EXPLICIT_DOCUMENT] = new Production() {
140149
public Event produce () {
141150
if (tokenizer.peekNextTokenType() != STREAM_END) {

0 commit comments

Comments
 (0)