Skip to content

Commit

Permalink
convenience type casting for booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed May 5, 2024
1 parent bca5b5e commit 0d00395
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/YamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,44 @@ default LocalDateTime dateTime(final YamlNode key) {
return null;
}

/**
* Convenience method to directly read a boolean value
* from this map. Returns true if the value is not null and is equal,
* ignoring case, to the string "true".
* It is equivalent to:
* <pre>
* YamlMapping map = ...;
* boolean value = Boolean.valueOf(map.string(...));
* </pre>
* @param key The key of the value.
* @return Boolean.
*/
default boolean bool(final String key) {
return this.bool(
Yaml.createYamlScalarBuilder().addLine(key).buildPlainScalar()
);
}

/**
* Convenience method to directly read a boolean value
* from this map. Returns true if the value is not null and is equal,
* ignoring case, to the string "true".
* It is equivalent to:
* <pre>
* YamlMapping map = ...;
* boolean value = Boolean.valueOf(map.string(...));
* </pre>
* @param key The key of the value.
* @return Boolean.
*/
default boolean bool(final YamlNode key) {
final YamlNode value = this.value(key);
if(value instanceof Scalar) {
return Boolean.valueOf(((Scalar) value).value());
}
return false;
}

@Override
default List<YamlNode> children() {
final List<YamlNode> children = new ArrayList<>();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ default LocalDateTime dateTime(final int index) {
return datetime;
}

/**
* Convenience method to directly read a boolean value
* from this sequence. Returns true if the value is not null and is equal,
* ignoring case, to the string "true".It is equivalent to:
* <pre>
* YamlSequence sequence = ...;
* boolean value = Boolean.valueOf(sequence.string(...));
* </pre>
* @param index The index of the value.
* @return Boolean.
*/
default boolean bool(final int index) {
return Boolean.valueOf(this.string(index));
}

/**
* Turn this YamlSequence to a JsonArray.
* @return JsonArray.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,28 @@ public void returnsDateTime() {
Matchers.nullValue()
);
}


@Test
public void returnsBoolean() {
final YamlMapping mapping = this.mapping();
MatcherAssert.assertThat(
mapping.bool("trueValue"),
Matchers.is(true)
);
MatcherAssert.assertThat(
mapping.bool("falseValue"),
Matchers.is(false)
);
MatcherAssert.assertThat(
mapping.bool("integer"),
Matchers.is(false)
);
MatcherAssert.assertThat(
mapping.bool("missing"),
Matchers.is(false)
);
}

/**
* Get a YamlMapping for test.
* @return YamlMapping.
Expand All @@ -134,6 +155,8 @@ private YamlMapping mapping() {
.add("long", "32165498")
.add("localDate", "2007-12-03")
.add("localDateTime", "2007-12-03T10:15:30")
.add("trueValue", "true")
.add("falseValue", "false")
.build();
return mapping;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ public void returnsDateTime() {
Matchers.is(LocalDateTime.parse("2007-12-03T10:15:30"))
);
}

@Test
public void returnsBoolean() {
final YamlSequence sequence = this.sequence();
MatcherAssert.assertThat(
sequence.bool(5),
Matchers.is(false)
);
MatcherAssert.assertThat(
sequence.bool(6),
Matchers.is(true)
);
MatcherAssert.assertThat(
sequence.bool(7),
Matchers.is(false)
);
}

/**
* Get a YamlSequence for test.
Expand All @@ -104,6 +121,8 @@ private YamlSequence sequence() {
.add("32165498")
.add("2007-12-03")
.add("2007-12-03T10:15:30")
.add("true")
.add("false")
.build();
return sequence;
}
Expand Down

0 comments on commit 0d00395

Please sign in to comment.