Skip to content

Commit

Permalink
Refactor Parser and YamlReader (#176)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ramchaik authored Mar 16, 2024
1 parent 763726d commit 5bb07f1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 40 deletions.
87 changes: 52 additions & 35 deletions src/com/esotericsoftware/yamlbeans/YamlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,55 @@ protected Class<?> findTagClass (String tag, ClassLoader classLoader) throws Cla
return Class.forName(tag, true, classLoader);
}

/**
* Reads a scalar value from the parser and converts it to the specified type.
* @param type The type to convert the scalar to.
* @param anchor The anchor of the scalar, or null.
* @return The converted scalar value.
* @throws YamlException
* @throws ParserException
*/
private Object readScalarValue(Class<?> type, String anchor) throws YamlException, ParserException {
Event event = parser.getNextEvent();
if (event.type != SCALAR) {
throw new YamlReaderException("Expected scalar for primitive type '" + type.getClass() + "' but found: " + event.type);
}

String value = ((ScalarEvent) event).value;
try {
Object convertedValue;
if (value == null) {
convertedValue = null;
} else if (type == String.class) {
convertedValue = value;
} else if (type == Integer.TYPE || type == Integer.class) {
convertedValue = Integer.decode(value);
} else if (type == Boolean.TYPE || type == Boolean.class) {
convertedValue = Boolean.valueOf(value);
} else if (type == Float.TYPE || type == Float.class) {
convertedValue = Float.valueOf(value);
} else if (type == Double.TYPE || type == Double.class) {
convertedValue = Double.valueOf(value);
} else if (type == Long.TYPE || type == Long.class) {
convertedValue = Long.decode(value);
} else if (type == Short.TYPE || type == Short.class) {
convertedValue = Short.decode(value);
} else if (type == Character.TYPE || type == Character.class) {
convertedValue = value.charAt(0);
} else if (type == Byte.TYPE || type == Byte.class) {
convertedValue = Byte.decode(value);
} else {
throw new YamlException("Unknown field type.");
}
if (anchor != null) {
addAnchor(anchor, convertedValue);
}
return convertedValue;
} catch (Exception ex) {
throw new YamlReaderException("Unable to convert value to required type \"" + type + "\": " + value, ex);
}
}

private Object readValueInternal (Class type, Class elementType, String anchor)
throws YamlException, ParserException, TokenizerException {
if (type == null || type == Object.class) {
Expand All @@ -239,7 +288,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
if (config.readConfig.guessNumberTypes) {
String value = ((ScalarEvent)event).value;
if (value != null) {
Number number = valueConvertedNumber(value);
Number number = parseNumber(value);
if (number != null) {
if (anchor != null) addAnchor(anchor, number);
parser.getNextEvent();
Expand All @@ -258,39 +307,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
}

if (Beans.isScalar(type)) {
Event event = parser.getNextEvent();
if (event.type != SCALAR) throw new YamlReaderException(
"Expected scalar for primitive type '" + type.getClass() + "' but found: " + event.type);
String value = ((ScalarEvent)event).value;
try {
Object convertedValue;
if (value == null) {
convertedValue = null;
} else if (type == String.class) {
convertedValue = value;
} else if (type == Integer.TYPE || type == Integer.class) {
convertedValue = Integer.decode(value);
} else if (type == Boolean.TYPE || type == Boolean.class) {
convertedValue = Boolean.valueOf(value);
} else if (type == Float.TYPE || type == Float.class) {
convertedValue = Float.valueOf(value);
} else if (type == Double.TYPE || type == Double.class) {
convertedValue = Double.valueOf(value);
} else if (type == Long.TYPE || type == Long.class) {
convertedValue = Long.decode(value);
} else if (type == Short.TYPE || type == Short.class) {
convertedValue = Short.decode(value);
} else if (type == Character.TYPE || type == Character.class) {
convertedValue = value.charAt(0);
} else if (type == Byte.TYPE || type == Byte.class) {
convertedValue = Byte.decode(value);
} else
throw new YamlException("Unknown field type.");
if (anchor != null) addAnchor(anchor, convertedValue);
return convertedValue;
} catch (Exception ex) {
throw new YamlReaderException("Unable to convert value to required type \"" + type + "\": " + value, ex);
}
return readScalarValue(type, anchor);
}

for (Entry<Class, ScalarSerializer> entry : config.scalarSerializers.entrySet()) {
Expand Down Expand Up @@ -486,7 +503,7 @@ public YamlReaderException (String message) {
}
}

private Number valueConvertedNumber (String value) {
private Number parseNumber(String value) {
Number number = null;
try {
number = Long.decode(value);
Expand Down
19 changes: 14 additions & 5 deletions src/com/esotericsoftware/yamlbeans/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,24 @@ public Event produce () {
};
table[P_IMPLICIT_DOCUMENT] = new Production() {
public Event produce () {
TokenType type = tokenizer.peekNextTokenType();
if (!(type == DIRECTIVE || type == DOCUMENT_START || type == STREAM_END)) {
parseStack.add(0, table[P_DOCUMENT_END]);
parseStack.add(0, table[P_BLOCK_NODE]);
parseStack.add(0, table[P_DOCUMENT_START_IMPLICIT]);
if (isNextTokenImplicitDocument()) {
parseImplicitDocument();
}
return null;
}

private boolean isNextTokenImplicitDocument() {
TokenType type = tokenizer.peekNextTokenType();
return type != DIRECTIVE && type != DOCUMENT_START && type != STREAM_END;
}

private void parseImplicitDocument() {
parseStack.add(0, table[P_DOCUMENT_END]);
parseStack.add(0, table[P_BLOCK_NODE]);
parseStack.add(0, table[P_DOCUMENT_START_IMPLICIT]);
}
};

table[P_EXPLICIT_DOCUMENT] = new Production() {
public Event produce () {
if (tokenizer.peekNextTokenType() != STREAM_END) {
Expand Down

0 comments on commit 5bb07f1

Please sign in to comment.