Skip to content

Commit

Permalink
Add some config code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Jan 3, 2024
1 parent a6078af commit fcc5ba1
Show file tree
Hide file tree
Showing 25 changed files with 1,500 additions and 58 deletions.
30 changes: 30 additions & 0 deletions core/src/main/java/io/jstach/rainbowgum/ConfigObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.jstach.rainbowgum;

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

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

/**
* Used to generate Rainbow Gum config objects
*/
@Retention(CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD })
@Documented
public @interface ConfigObject {

/**
* Name of builder.
* @return name of builder.
*/
String name();

/**
* Property prefix.
* @return prefix
*/
String prefix();

}
2 changes: 1 addition & 1 deletion core/src/main/java/io/jstach/rainbowgum/LogConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void subscribe(Consumer<? super LogConfig> consumer) {

@Override
public boolean isEnabled(String loggerName) {
return changeSetting.require(_config().properties(), loggerName);
return changeSetting.value(_config().properties(), loggerName);

}

Expand Down
109 changes: 58 additions & 51 deletions core/src/main/java/io/jstach/rainbowgum/LogOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@ private static URI uri(String s) {
}
}

/**
* The content type of the binary data passed to the output from an encoder.
*/
public interface ContentType {

/**
* Content type of binary datay passed to output.
* @return content type.
*/
String contentType();

/**
* Builtin content types.
*/
public enum StandardContentType implements ContentType {

/**
* text/plain
*/
TEXT_PLAIN() {
@Override
public String contentType() {
return "text/plain";
}
},
/**
* application/json
*/
APPLICATION_JSON() {
@Override
public String contentType() {
return "application/json";
}
}

}

}

/**
* The uri of the output.
* @return uri.
Expand All @@ -89,16 +128,17 @@ default void start(LogConfig config) {
* @param s string.
*/
default void write(LogEvent event, String s) {
write(event, s.getBytes(StandardCharsets.UTF_8));
write(event, s.getBytes(StandardCharsets.UTF_8), ContentType.StandardContentType.TEXT_PLAIN);
}

/**
* Analogous to {@link OutputStream#write(byte[])}.
* @param event event associated with bytes.
* @param bytes data to be written.
* @param contentType content type of the bytes.
*/
default void write(LogEvent event, byte[] bytes) {
write(event, bytes, 0, bytes.length);
default void write(LogEvent event, byte[] bytes, ContentType contentType) {
write(event, bytes, 0, bytes.length, contentType);
}

/**
Expand All @@ -107,19 +147,21 @@ default void write(LogEvent event, byte[] bytes) {
* @param bytes data to be written.
* @param off offset.
* @param len length.
* @param contentType content type of the bytes.
*/
public void write(LogEvent event, byte[] bytes, int off, int len);
public void write(LogEvent event, byte[] bytes, int off, int len, ContentType contentType);

/**
* Write event with byte buffer.
* @param event event.
* @param buf byte buffer.
* @param contentType content type of the buf
*/
default void write(LogEvent event, ByteBuffer buf) {
default void write(LogEvent event, ByteBuffer buf, ContentType contentType) {
byte[] arr = new byte[buf.position()];
buf.rewind();
buf.get(arr);
write(event, arr);
write(event, arr, contentType);
}

public void flush();
Expand Down Expand Up @@ -154,12 +196,12 @@ public enum WriteMethod {
*/
STRING,
/**
* Prefer calling {@link LogOutput#write(LogEvent, byte[])} or
* {@link LogOutput#write(LogEvent, byte[], int, int)}.
* Prefer calling {@link LogOutput#write(LogEvent, byte[], ContentType)} or
* {@link LogOutput#write(LogEvent, byte[], int, int, ContentType)}.
*/
BYTES,
/**
* Prefer calling {@link LogOutput#write(LogEvent, ByteBuffer)}.
* Prefer calling {@link LogOutput#write(LogEvent, ByteBuffer, ContentType)}.
*/
BYTE_BUFFER

Expand Down Expand Up @@ -211,41 +253,6 @@ public static LogOutput ofStandardErr() {
return new StdErrOutput(fos.getChannel(), fos);
}

// public static LogOutput of(OutputStream out) {
// return new LogOutput() {
//
// @Override
// public void write(LogEvent event, byte[] bytes, int off, int len) {
// try {
// out.write(bytes, off, len);
// }
// catch (IOException e) {
// throw new UncheckedIOException(e);
// }
// }
//
// @Override
// public void flush() {
// try {
// out.flush();
// }
// catch (IOException e) {
// throw new UncheckedIOException(e);
// }
// }
//
// @Override
// public void close() {
// try {
// out.close();
// }
// catch (IOException e) {
// throw new UncheckedIOException(e);
// }
// }
// };
// }

/**
* Creates an output from a file channel.
* @param uri uri of output.
Expand Down Expand Up @@ -281,8 +288,8 @@ public URI uri() throws UnsupportedOperationException {
}

@Override
public synchronized void write(LogEvent event, byte[] bytes, int off, int len) {
output.write(event, bytes, off, len);
public synchronized void write(LogEvent event, byte[] bytes, int off, int len, ContentType contentType) {
output.write(event, bytes, off, len, contentType);
}

@Override
Expand Down Expand Up @@ -320,12 +327,12 @@ public URI uri() throws UnsupportedOperationException {
}

@Override
public void write(LogEvent event, byte[] bytes, int off, int len) {
write(event, ByteBuffer.wrap(bytes, off, len));
public void write(LogEvent event, byte[] bytes, int off, int len, ContentType contentType) {
write(event, ByteBuffer.wrap(bytes, off, len), contentType);
}

@Override
public void write(LogEvent event, ByteBuffer buffer) {
public void write(LogEvent event, ByteBuffer buffer, ContentType contentType) {
try {

// Clear any current interrupt (see LOGBACK-875)
Expand Down Expand Up @@ -396,7 +403,7 @@ public Std(URI uri, FileChannel channel, OutputStream outputStream) {
}

@Override
public void write(LogEvent event, ByteBuffer buffer) {
public void write(LogEvent event, ByteBuffer buffer, ContentType contentType) {
try {
channel.write(buffer);
}
Expand Down Expand Up @@ -455,7 +462,7 @@ public URI uri() {
}

@Override
public void write(LogEvent event, byte[] bytes, int off, int len) {
public void write(LogEvent event, byte[] bytes, int off, int len, ContentType contentType) {
try {
outputStream.write(bytes, off, len);
}
Expand Down
74 changes: 71 additions & 3 deletions core/src/main/java/io/jstach/rainbowgum/LogProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@ public <U> PropertyValue<U> map(PropertyFunction<? super T, ? extends U, ? super
return property.propertyGetter.valueOrNull(properties, property.key);
}

/**
* Gets a value if there if not uses the fallback.
* @param fallback maybe <code>null</code>.
* @return value.
*/
public @Nullable T valueOrNull(@Nullable T fallback) {
return property.propertyGetter.valueOrNull(properties, property.key, fallback);
}

/**
* Convenience that turns a value into an optional.
* @return optional.
Expand All @@ -395,7 +404,19 @@ public Optional<T> optional() {
* @throws NoSuchElementException if there is no value.
*/
public T value() throws NoSuchElementException {
return property.propertyGetter.require(properties, property.key);
return property.propertyGetter.value(properties, property.key);
}

/**
* Gets a value if there if not uses the fallback if not null otherwise throws an
* exception.
* @param fallback maybe <code>null</code>.
* @return value.
* @throws NoSuchElementException if no property and fallback is
* <code>null</code>.
*/
public T value(@Nullable T fallback) throws NoSuchElementException {
return property.propertyGetter.value(properties, property.key, fallback);
}
}

Expand Down Expand Up @@ -423,7 +444,7 @@ public PropertyValue<T> get(LogProperties properties) {
* @param mapper function to map.
* @return property.
*/
private <U> Property<U> map(PropertyFunction<? super T, ? extends U, ? super Exception> mapper) {
public <U> Property<U> map(PropertyFunction<? super T, ? extends U, ? super Exception> mapper) {
return new Property<>(propertyGetter.map(mapper), key);
}

Expand Down Expand Up @@ -452,6 +473,21 @@ sealed interface PropertyGetter<T> {
@Nullable
T valueOrNull(LogProperties props, String key);

/**
* Value or fallback if property is missing.
* @param props log properties.
* @param key key to use.
* @param fallback to use can be null.
* @return value or <code>null</code>.
*/
default @Nullable T valueOrNull(LogProperties props, String key, @Nullable T fallback) {
var t = valueOrNull(props, key);
if (t == null) {
return fallback;
}
return t;
}

/**
* Determines full name of key.
* @param key key.
Expand All @@ -468,14 +504,34 @@ default String fullyQualifiedKey(String key) {
* @return value.
* @throws NoSuchElementException if no value is found for key.
*/
default T require(LogProperties props, String key) throws NoSuchElementException {
default T value(LogProperties props, String key) throws NoSuchElementException {
var t = valueOrNull(props, key);
if (t == null) {
throw findRoot(this).throwMissing(props, key);
}
return t;
}

/**
* Value or fallback or exception if property is missing and fallback is null.
* @param props log properties.
* @param key key to use.
* @param fallback to use can be null.
* @return value or <code>null</code>.
* @throws NoSuchElementException if property is missing and fallback is
* <code>null</code>.
*/
default T value(LogProperties props, String key, @Nullable T fallback) throws NoSuchElementException {
var t = valueOrNull(props, key);
if (t == null) {
t = fallback;
}
if (t == null) {
throw findRoot(this).throwMissing(props, key);
}
return t;
}

/**
* Creates a Property from the given key and this getter.
* @param key key.
Expand Down Expand Up @@ -573,6 +629,18 @@ public String fullyQualifiedKey(String key) {
return concatKey(prefix, key);
}

public PropertyGetter<Integer> toInt() {
return this.map(Integer::parseInt);
}

public <T extends Enum<T>> PropertyGetter<T> toEnum(Class<T> enumClass) {
return this.map(s -> Enum.valueOf(enumClass, s));
}

public PropertyGetter<URI> toURI() {
return this.map(URI::new);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.jstach.rainbowgum.LogEvent;
import io.jstach.rainbowgum.LogOutput;
import io.jstach.rainbowgum.LogOutput.ContentType.StandardContentType;

class RawJsonWriter {

Expand Down Expand Up @@ -396,7 +397,7 @@ public final void toStream(final OutputStream stream) throws IOException {
}

public final void write(final LogOutput output, LogEvent event) {
output.write(event, buffer, 0, position);
output.write(event, buffer, 0, position, StandardContentType.APPLICATION_JSON);
position = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void write(LogEvent event, String s) {
}

@Override
public void write(LogEvent event, byte[] bytes, int off, int len) {
public void write(LogEvent event, byte[] bytes, int off, int len, ContentType contentType) {
write(event, new String(bytes, off, len, StandardCharsets.UTF_8));
}

Expand Down
Loading

0 comments on commit fcc5ba1

Please sign in to comment.