Skip to content

Commit

Permalink
#591 [at]YamlComment Annotation for Reflected YamlNode
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jan 19, 2024
1 parent ccbc846 commit 8b9c40f
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/eoyaml/JsonYamlDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class JsonYamlDump implements YamlDump {
}

@Override
public YamlNode dump() {
public YamlNode dump(final String comment) {
final YamlNode node;
final JsonValue safeValue;
if (this.value == null) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/amihaiemil/eoyaml/ReflectedYamlDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ final class ReflectedYamlDump implements YamlDump {
}

@Override
public YamlNode dump() {
public YamlNode dump(final String comment) {
final YamlNode node;
if(this.object == null || SCALAR_TYPES.contains(this.object.getClass())) {
node = new ReflectedYamlScalar(this.object);
node = new ReflectedYamlScalar(this.object, comment);
} else if(this.object instanceof Collection || this.object.getClass().isArray()){
node = new ReflectedYamlSequence(this.object);
node = new ReflectedYamlSequence(this.object, comment);
} else {
node = new ReflectedYamlMapping(this.object);
node = new ReflectedYamlMapping(this.object, comment);
}
return node;
}
Expand Down
53 changes: 47 additions & 6 deletions src/main/java/com/amihaiemil/eoyaml/ReflectedYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,33 @@ final class ReflectedYamlMapping extends BaseYamlMapping {
*/
private final Object bean;

/**
* Comment of this mapping.
*/
private final String comment;

/**
* Constructor.
* @param bean Serializable get/set Java Bean.
*/
ReflectedYamlMapping(final Object bean) {
this(bean, "");
}

/**
* Constructor.
* @param bean Serializable get/set Java Bean.
* @param comment Comment of this mapping.
*/
ReflectedYamlMapping(final Object bean, final String comment) {
if(bean instanceof Collection || bean.getClass().isArray()) {
throw new IllegalArgumentException(
"YamlMapping can only be reflected "
+ "from an Object or from a Map."
);
}
this.bean = bean;
this.comment = comment;
}

@Override
Expand Down Expand Up @@ -93,9 +108,17 @@ public YamlNode value(final YamlNode key) {
}
} else {
if (key instanceof Scalar) {
node = this.objectToYamlNode(
this.invokeMethod(((Scalar) key).value())
);
final YamlNode reflectedKey = this.keys().stream().filter(
k -> k.asScalar().value().equals(((Scalar) key).value())
).findFirst().orElse(null);
if(reflectedKey == null) {
node = null;
} else {
node = this.objectToYamlNode(
this.invokeMethod(reflectedKey.asScalar().value()),
reflectedKey.asScalar().comment().value()
);
}
} else {
throw new IllegalArgumentException(
"Reflected YamlMapping can only have string keys "
Expand All @@ -116,7 +139,7 @@ public YamlNode yamlNode() {

@Override
public String value() {
return "";
return ReflectedYamlMapping.this.comment;
}
};
}
Expand Down Expand Up @@ -155,7 +178,17 @@ private Object invokeMethod(final String keyName) {
* @return YamlNode.
*/
private YamlNode objectToYamlNode(final Object value) {
return Yaml.createYamlDump(value).dump();
return this.objectToYamlNode(value, "");
}

/**
* Turn a Java Object to an appropriate YAML Node, adding a comment.
* @param value Object value.
* @param comm String comment.
* @return YamlNode.
*/
private YamlNode objectToYamlNode(final Object value, final String comm) {
return Yaml.createYamlDump(value).dump(comm);
}

/**
Expand Down Expand Up @@ -207,7 +240,15 @@ public YamlNode yamlNode() {

@Override
public String value() {
return "";
final YamlComment yamlComment = MethodKey.this.method
.getAnnotation(YamlComment.class);
final String value;
if(yamlComment == null) {
value = "";
} else {
value = yamlComment.value();
}
return value;
}
};
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/ReflectedYamlScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,27 @@ final class ReflectedYamlScalar extends BaseScalar {
*/
private final Object scalar;

/**
* Comment referring to this scalar.
*/
private final String comment;

/**
* Constructor.
* @param scalar Scalar Object
*/
ReflectedYamlScalar(final Object scalar) {
this(scalar, "");
}

/**
* Constructor.
* @param scalar Scalar Object
* @param comment String comment referring to this Scalar.
*/
ReflectedYamlScalar(final Object scalar, final String comment) {
this.scalar = scalar;
this.comment = comment;
}

@Override
Expand All @@ -69,7 +84,7 @@ public YamlNode yamlNode() {

@Override
public String value() {
return "";
return ReflectedYamlScalar.this.comment;
}
};
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/ReflectedYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,25 @@ final class ReflectedYamlSequence extends BaseYamlSequence {
*/
private final Collection<Object> sequence;

/**
* Comment referring to this sequence.
*/
private final String comment;

/**
* Constructor.
* @param sequence Collection or array of Object.
*/
ReflectedYamlSequence(final Object sequence) {
this(sequence, "");
}

/**
* Constructor.
* @param sequence Collection or array of Object.
* @param comment String comment referring to this sequence.
*/
ReflectedYamlSequence(final Object sequence, final String comment) {
if(sequence instanceof Collection) {
this.sequence = (Collection<Object>) sequence;
} else if(sequence.getClass().isArray()) {
Expand All @@ -58,9 +72,10 @@ final class ReflectedYamlSequence extends BaseYamlSequence {
} else {
throw new IllegalArgumentException(
"YamlSequence can only be reflected "
+ "from a Collection or from an array."
+ "from a Collection or from an array."
);
}
this.comment = comment;
}

@Override
Expand All @@ -82,7 +97,7 @@ public YamlNode yamlNode() {

@Override
public String value() {
return "";
return ReflectedYamlSequence.this.comment;
}
};
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/YamlComment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.amihaiemil.eoyaml;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation destined for the accessor methods (public non-void methods)
* of Java Beans to be converted to YAML. The specified value will be the
* comment of the corresponding YAML Node.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 7.1.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface YamlComment {

/**
* String value of the comment.
* @return String, never null.
*/
String value();
}
54 changes: 49 additions & 5 deletions src/main/java/com/amihaiemil/eoyaml/YamlDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,39 @@
public interface YamlDump {

/**
*
* Dump an Object, represent it as YAML.
* Generally, if the Object is a Collection or on Array, the resulting
* YamlNode will be a YamlSequence.<br><br>
* If the Object is a Map or other kind of Object, the resulting
* YamlNode will be a YamlSequence.<br><br>
* If the Object is a String, LocalDate, LocaDateTime or a primitive,
* the resulting YamlNode will be a plain Scalar.
* @param comment String comment.
* @return YAML Representation.
*/
YamlNode dump();
YamlNode dump(final String comment);

/**
* Dump an Object, represent it as YAML.
* Generally, if the Object is a Collection or on Array, the resulting
* YamlNode will be a YamlSequence.<br><br>
* If the Object is a Map or other kind of Object, the resulting
* YamlNode will be a YamlSequence.<br><br>
* If the Object is a String, LocalDate, LocaDateTime or a primitive,
* the resulting YamlNode will be a plain Scalar.
* @return YAML Representation.
*/
default YamlNode dump() {
return this.dump("");
}

/**
* Convenience method, equivalent to calling the
* dump(...) method and casting the YamlNode to YamlMapping.
* @return YamlMapping.
*/
default YamlMapping dumpMapping() {
return (YamlMapping) this.dump();
return this.dumpMapping("");
}

/**
Expand All @@ -63,7 +77,7 @@ default YamlMapping dumpMapping() {
* @return YamlSequence.
*/
default YamlSequence dumpSequence() {
return (YamlSequence) this.dump();
return this.dumpSequence("");
}

/**
Expand All @@ -72,7 +86,37 @@ default YamlSequence dumpSequence() {
* @return Scalar.
*/
default Scalar dumpScalar() {
return (Scalar) this.dump();
return this.dumpScalar("");
}

/**
* Convenience method, equivalent to calling the
* dump(...) method and casting the YamlNode to YamlMapping.
* @param comment String comment referring to the mapping.
* @return YamlMapping.
*/
default YamlMapping dumpMapping(final String comment) {
return (YamlMapping) this.dump(comment);
}

/**
* Convenience method, equivalent to calling the
* dump(...) method and casting the YamlNode to YamlSequence.
* @param comment String comment referring to the sequence.
* @return YamlSequence.
*/
default YamlSequence dumpSequence(final String comment) {
return (YamlSequence) this.dump(comment);
}

/**
* Convenience method, equivalent to calling the
* dump(...) method and casting the YamlNode to Scalar.
* @param comment String comment referring to the scalar.
* @return Scalar.
*/
default Scalar dumpScalar(final String comment) {
return (Scalar) this.dump(comment);
}

}
Loading

0 comments on commit 8b9c40f

Please sign in to comment.