Skip to content

Commit

Permalink
Polishing config apt.
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Jan 12, 2024
1 parent 246a890 commit 928da9a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public List<PropertyModel> prefixParameters() {
return properties.stream().filter(p -> p.kind == PropertyKind.NAME_PARAMETER).toList();
}

public List<String> descriptionLines() {
return description.lines().map(String::trim).toList();
}

record Converter(String methodName) {
}

Expand Down Expand Up @@ -68,19 +72,29 @@ public String propertyLiteral() {
return "PROPERTY_" + name;
}

public String convertMethod() {
public @Nullable String convertMethod() {
if (converter != null) {
return ".map(_v -> " + converter.methodName + "(_v))";
}
return switch (type) {
case INTEGER_TYPE -> ".toInt()";
case STRING_TYPE -> "";
case STRING_TYPE -> null;
case URI_TYPE -> ".toURI()";
case BOOLEAN_TYPE -> ".toBoolean()";
default -> throw new IllegalStateException(type + " is not supported");
};
}

public String typeDescription() {
return switch (type) {
case INTEGER_TYPE -> "Integer";
case STRING_TYPE -> "String";
case URI_TYPE -> "URI";
case BOOLEAN_TYPE -> "Boolean";
default -> "String (converted)";
};
}

boolean isLiteralType() {
return switch (type) {
case INTEGER_TYPE, STRING_TYPE, BOOLEAN_TYPE -> true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,29 @@ else if (inDescription) {
if (parts.length >= 3) {
String paramName = parts[1];
String paramDescription = parts[2];
properties.put(paramName, paramDescription);
properties.put(paramName, processParamDescription(paramDescription));
}
}
}
return new ConfigJavadoc(desc.toString(), properties);
}
}

private static String processParamDescription(String description) {
String s = capitalizeFirstLetter(description);
if (!s.endsWith(".")) {
return s + ".";
}
return s;
}

private static String capitalizeFirstLetter(String str) {
if (str.isEmpty()) {
return str;
}
return str.trim().substring(0, 1).toUpperCase() + str.substring(1);
}

private static Class<?> primitiveToClass(TypeKind k) {
if (!k.isPrimitive())
throw new IllegalArgumentException("k is not primitive: " + k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import io.jstach.rainbowgum.LogProperties.Property;

/**
* Builder to create $$targetType$$.
* $$description$$
* Builder to create {@link $$targetType$$ }.
$$#descriptionLines$$
* $$.$$
$$/descriptionLines$$
* <table class="table">
* <caption>Properties retrieved from LogProperties</caption>
* <caption><strong>Properties retrieved from LogProperties</strong></caption>
* <tr>
* <th>Property Pattern</th>
* <th>Type</th>
Expand All @@ -20,7 +22,7 @@
$$#normal$$
* <tr>
* <td><code>{@value $$propertyLiteral$$ }</code></td>
* <td><code>$$type$$</code></td>
* <td><code>$$typeDescription$$</code></td>
* <td><code>$$required$$</code></td>
* <td>$$defaultValueDoc$$</td>
* <td>$$javadoc$$</td>
Expand Down Expand Up @@ -80,8 +82,10 @@ public final class $$builderName$$ {
$$#properties$$
$$#normal$$
$$propertyVar$$ = Property.builder()
$$convertMethod$$
$$#convertMethod$$$$.$$
$$/convertMethod$$
.build(LogProperties.interpolateKey($$propertyLiteral$$, prefixParameters));

$$/normal$$
$$#prefixParameter$$
this.$$name$$ = $$name$$;
Expand All @@ -94,6 +98,7 @@ public final class $$builderName$$ {

/**
* Sets $$#required$$<strong>required</strong> $$/required$$$$name$$.
* $$javadoc$$
* Default is $$defaultValueDoc$$.
* @param $$name$$ <code>{@value #$$propertyLiteral$$ } = $$type$$</code> $$javadoc$$
* @return this builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class RabbitMQOutput implements LogOutput {
}

/**
* Creates a RabbitMQOutput. This method is called by the generated builder.
* Creates a RabbitMQOutput.
* @param name used to resolve config and give the output a name.
* @param uri passed to the rabbitmq connection factory.
* @param exchange exchange to send messages to.
Expand All @@ -103,7 +103,7 @@ static RabbitMQOutput of( //
@LogConfigurable.PrefixParameter String name, //
@Nullable URI uri, //
@LogConfigurable.DefaultParameter("DEFAULT_EXCHANGE") String exchange, //
@Nullable String routingKey, //
@LogConfigurable.ConvertParameter("toRoutingKeyFunction") @Nullable Function<LogEvent, String> routingKey, //
@Nullable Boolean declareExchange, //
@Nullable String host, //
@Nullable String username, //
Expand Down Expand Up @@ -142,7 +142,7 @@ static RabbitMQOutput of( //
}
Function<LogEvent, String> routingKeyFunction;
if (routingKey != null) {
routingKeyFunction = e -> routingKey;
routingKeyFunction = routingKey;
}
else {
routingKeyFunction = e -> e.level().name();
Expand All @@ -151,6 +151,10 @@ static RabbitMQOutput of( //
exchangeType);
}

static Function<LogEvent, String> toRoutingKeyFunction(String routingKey) {
return e -> routingKey;
}

@Override
public void start(LogConfig config) {
lock.writeLock().lock();
Expand Down

0 comments on commit 928da9a

Please sign in to comment.