Skip to content

Commit

Permalink
Pull out JSON into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Jan 9, 2024
1 parent 4bb443d commit ea34cad
Show file tree
Hide file tree
Showing 20 changed files with 493 additions and 144 deletions.
99 changes: 90 additions & 9 deletions core/src/main/java/io/jstach/rainbowgum/LogProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.System.Logger.Level;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
Expand All @@ -11,6 +12,7 @@
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -99,6 +101,11 @@ public interface LogProperties {
*/
static final String OUTPUT_PREFIX = ROOT_PREFIX + "output.{name}.";

/**
* Logging output prefix for configuration.
*/
static final String ENCODER_PREFIX = ROOT_PREFIX + "encoder.{name}.";

/**
* Analogous to {@link System#getProperty(String)}.
* @param key property key.
Expand Down Expand Up @@ -184,14 +191,88 @@ public static LogProperties of(List<? extends LogProperties> logProperties) {
* @see LogOutput
*/
public static LogProperties of(URI uri) {
var m = parseUriQuery(uri.getRawQuery(), true);
Map<String, String> m = new LinkedHashMap<>();
parseUriQuery(uri.getRawQuery(), (k, v) -> {
if (v != null) {
m.put(k, v);
}
});
return new MapProperties(uri.toString(), m);
}

private static Map<String, String> parseUriQuery(String query, boolean decode) {
/**
* Parse a {@linkplain URI#getRawQuery() URI query} in <a href=
* "https://www.w3.org/TR/2014/REC-html5-20141028/forms.html#url-encoded-form-data">
* application/x-www-form-urlencoded </a> format useful for parsing properties values
* that have key values embedded in them. <strong>This parser unlike form encoding
* uses <code>%20</code> for space as the data is coming from a URI.</strong>
* @param query raw query component of URI.
* @param consumer accept will be called for each entry and if key (left hand) does
* not have a "<code>=</code>" following it the second parameter on the consumer
* accept will be passed <code>null</code>.
*/
public static void parseUriQuery(String query,
@SuppressWarnings("exports") BiConsumer<String, @Nullable String> consumer) {
parseUriQuery(query, true, consumer);
}

/**
* Parses a URI query formatted string to a Map.
* @param query raw query component of URI.
* @return decoded key values with last key winning over previous equal keys.
* @see #parseUriQuery(String, BiConsumer)
*/
public static Map<String, String> parseMap(String query) {
Map<String, String> m = new LinkedHashMap<>();
parseUriQuery(query, (k, v) -> {
if (v != null) {
m.put(k, v);
}
});
return m;
}

/**
* Parses a URI query for a multi value map.
* @param query raw query component of URI.
* @return decoded key values with multiple keys grouped together in order found.
*/
public static Map<String, List<String>> parseMultiMap(String query) {
Map<String, List<String>> m = new LinkedHashMap<>();
BiConsumer<String, String> f = (k, v) -> {
m.computeIfAbsent(k, _k -> new ArrayList<String>()).add(v);
};
parseUriQuery(query, true, f);
return m;
}

Map<String, String> kvs = new LinkedHashMap<>();
String[] pairs = query.split("&");
/**
* Parses a list of strings from a string that is <a href=
* "https://www.w3.org/TR/2014/REC-html5-20141028/forms.html#url-encoded-form-data">
* percent encoded for escaping (application/x-www-form-urlencoded)</a> where the
* separator can be either "<code>&amp;</code>" or "<code>,</code>".
* <p>
* An example would be using ampersand: <pre><code>
* "a&amp;b%20B&amp;c" -> ["a","b B","c"]
* </code> </pre> and comma: <pre><code>
* "a,b,c" -> ["a","b","c"]
* </code> </pre>
* @param query the comma or ampersand delimited string that is in URI query format.
* @return list of strings
*/
public static List<String> parseList(String query) {
List<String> list = new ArrayList<>();
parseUriQuery(query, true, "[&,]", (k, v) -> list.add(k));
return list;
}

private static void parseUriQuery(String query, boolean decode, BiConsumer<String, @Nullable String> consumer) {
parseUriQuery(query, decode, "&", consumer);
}

private static void parseUriQuery(String query, boolean decode, String sep,
BiConsumer<String, @Nullable String> consumer) {
String[] pairs = query.split(sep);
for (String pair : pairs) {
int idx = pair.indexOf("=");
String key;
Expand All @@ -201,24 +282,24 @@ private static Map<String, String> parseUriQuery(String query, boolean decode) {
}
else if (idx < 0) {
key = pair;
value = "";
value = null;
}
else {
key = pair.substring(0, idx);
value = pair.substring(idx + 1);
}
if (decode) {
key = PercentCodec.decode(key, StandardCharsets.UTF_8);
value = PercentCodec.decode(key, StandardCharsets.UTF_8);
if (value != null) {
value = PercentCodec.decode(value, StandardCharsets.UTF_8);
}

}
if (key.isBlank()) {
continue;
}
kvs.put(key, value);

consumer.accept(key, value);
}
return kvs;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.jstach.rainbowgum;
package io.jstach.rainbowgum.annotation;

import static java.lang.annotation.RetentionPolicy.CLASS;

Expand All @@ -8,12 +8,13 @@
import java.lang.annotation.Target;

/**
* Used to generate Rainbow Gum config objects
* Used to generate Rainbow Gum config builder objects that once built will call the
* method annotated with the properties from the generated builder on build.
*/
@Retention(CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD })
@Documented
public @interface ConfigObject {
public @interface LogConfigurable {

/**
* Name of builder.
Expand Down Expand Up @@ -61,4 +62,21 @@

}

/**
* Use to set static defaults to parameters.
*/
@Retention(CLASS)
@Target({ ElementType.PARAMETER })
@Documented
public @interface ConvertParameter {

/**
* Static method on target type to call to convert the parameter. The method must
* be return a type and have a single argument.
* @return static method name.
*/
String value();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* RainbowGum annotations used for static code generation.
* @see io.jstach.rainbowgum.annotation.LogConfigurable
*/
@org.eclipse.jdt.annotation.NonNullByDefault
package io.jstach.rainbowgum.annotation;
97 changes: 0 additions & 97 deletions core/src/main/java/io/jstach/rainbowgum/json/JsonBuffer.java

This file was deleted.

2 changes: 1 addition & 1 deletion core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

module io.jstach.rainbowgum {
exports io.jstach.rainbowgum;
exports io.jstach.rainbowgum.annotation;
exports io.jstach.rainbowgum.format;
exports io.jstach.rainbowgum.json;
exports io.jstach.rainbowgum.output;
exports io.jstach.rainbowgum.spi;

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<artifactId>rainbowgum-jansi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rainbowgum-json</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rainbowgum-disruptor</artifactId>
Expand Down Expand Up @@ -788,5 +793,6 @@
<module>etc</module>
<module>rainbowgum-rabbitmq</module>
<module>rainbowgum-config-apt</module>
<module>rainbowgum-json</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.util.List;

import org.eclipse.jdt.annotation.Nullable;

import io.jstach.jstache.JStache;
import io.jstach.jstache.JStacheConfig;
import io.jstach.jstache.JStacheType;
import io.jstach.rainbowgum.apt.BuilderModel.PropertyModel;

@JStacheConfig(type = JStacheType.STACHE)
@JStache(path = "io/jstach/rainbowgum/apt/ConfigBuilder.java")
Expand Down Expand Up @@ -37,13 +40,17 @@ public List<PropertyModel> prefixParameters() {
return properties.stream().filter(p -> p.kind == PropertyKind.NAME_PARAMETER).toList();
}

record Converter(String methodName) {
}

record PropertyModel(PropertyKind kind, //
String name, //
String type, //
String typeWithAnnotation, //
String defaultValue, //
boolean required, //
String javadoc) {
String javadoc, //
@Nullable Converter converter) {

private static final String INTEGER_TYPE = "java.lang.Integer";

Expand All @@ -62,6 +69,9 @@ public String propertyLiteral() {
}

public String convertMethod() {
if (converter != null) {
return ".map(_v -> " + converter.methodName + "(_v))";
}
return switch (type) {
case INTEGER_TYPE -> ".toInt()";
case STRING_TYPE -> "";
Expand Down
Loading

0 comments on commit ea34cad

Please sign in to comment.