EcsEncoderBuilder.java

package io.jstach.rainbowgum.json.encoder;

import io.jstach.rainbowgum.LogProperties;
import io.jstach.rainbowgum.LogProperty;

/**
 * Builder to create {@link io.jstach.rainbowgum.json.encoder.EcsEncoder }.
 * Creates ECS Encoder.
 * <table class="table">
 * <caption><strong>Properties retrieved from LogProperties</strong></caption>
 * <tr>
 * <th>Property Pattern</th>
 * <th>Type</th>
 * <th>Required</th>
 * <th>Default</th>
 * <th>Description</th>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_serviceName }</code></td>
 * <td><code>String</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>service.name</code> field, or null to omit.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_serviceVersion }</code></td>
 * <td><code>String</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>service.version</code> field, or null to omit.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_serviceEnvironment }</code></td>
 * <td><code>String</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>service.environment</code> field, or null to omit.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_serviceNodeName }</code></td>
 * <td><code>String</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>service.node.name</code> field, or null to omit.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_eventDataset }</code></td>
 * <td><code>String</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>event.dataset</code> field, or null to omit.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_structured }</code></td>
 * <td><code>Boolean</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>true</code> nests fields as JSON objects (Spring Boot's ECS shape) instead of flattened dotted field names, default is false.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_prettyPrint }</code></td>
 * <td><code>Boolean</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>true</code> will pretty print the JSON, default is false.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_maxBufferSize }</code></td>
 * <td><code>Integer</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td>Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.</td>
 * </tr>
 * </table>
 */
public final class EcsEncoderBuilder implements io.jstach.rainbowgum.LogBuilder<EcsEncoderBuilder,io.jstach.rainbowgum.json.encoder.EcsEncoder> {

	/**
	 * The properties to be retrieved from config will have
	 * this prefix.
	 */
	static final String PROPERTY_PREFIX = "logging.appender.{name}.encoder.ecs.";
	
	/**
	 * <code>{@value #PROPERTY_serviceName } = java.lang.String</code> <code>service.name</code> field, or null to omit.
	 */
	static final String PROPERTY_serviceName = PROPERTY_PREFIX + "serviceName";
	/**
	 * <code>{@value #PROPERTY_serviceVersion } = java.lang.String</code> <code>service.version</code> field, or null to omit.
	 */
	static final String PROPERTY_serviceVersion = PROPERTY_PREFIX + "serviceVersion";
	/**
	 * <code>{@value #PROPERTY_serviceEnvironment } = java.lang.String</code> <code>service.environment</code> field, or null to omit.
	 */
	static final String PROPERTY_serviceEnvironment = PROPERTY_PREFIX + "serviceEnvironment";
	/**
	 * <code>{@value #PROPERTY_serviceNodeName } = java.lang.String</code> <code>service.node.name</code> field, or null to omit.
	 */
	static final String PROPERTY_serviceNodeName = PROPERTY_PREFIX + "serviceNodeName";
	/**
	 * <code>{@value #PROPERTY_eventDataset } = java.lang.String</code> <code>event.dataset</code> field, or null to omit.
	 */
	static final String PROPERTY_eventDataset = PROPERTY_PREFIX + "eventDataset";
	/**
	 * <code>{@value #PROPERTY_structured } = java.lang.Boolean</code> <code>true</code> nests fields as JSON objects (Spring Boot's ECS shape) instead of flattened dotted field names, default is false.
	 */
	static final String PROPERTY_structured = PROPERTY_PREFIX + "structured";
	/**
	 * <code>{@value #PROPERTY_prettyPrint } = java.lang.Boolean</code> <code>true</code> will pretty print the JSON, default is false.
	 */
	static final String PROPERTY_prettyPrint = PROPERTY_PREFIX + "prettyPrint";
	/**
	 * <code>{@value #PROPERTY_maxBufferSize } = java.lang.Integer</code> Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 */
	static final String PROPERTY_maxBufferSize = PROPERTY_PREFIX + "maxBufferSize";
	
	private final String propertyPrefix;
	final String property_serviceName;
	final String property_serviceVersion;
	final String property_serviceEnvironment;
	final String property_serviceNodeName;
	final String property_eventDataset;
	final String property_structured;
	final String property_prettyPrint;
	final String property_maxBufferSize;
	
	private final java.lang.String name;
	private java.lang.@org.jspecify.annotations.Nullable String serviceName = null;
	private java.lang.@org.jspecify.annotations.Nullable String serviceVersion = null;
	private java.lang.@org.jspecify.annotations.Nullable String serviceEnvironment = null;
	private java.lang.@org.jspecify.annotations.Nullable String serviceNodeName = null;
	private java.lang.@org.jspecify.annotations.Nullable String eventDataset = null;
	private java.lang.@org.jspecify.annotations.Nullable Boolean structured = null;
	private java.lang.@org.jspecify.annotations.Nullable Boolean prettyPrint = null;
	private java.lang.@org.jspecify.annotations.Nullable Integer maxBufferSize = null;
	
	/**
	 * Create a builder for {@code io.jstach.rainbowgum.json.encoder.EcsEncoder }.
	 * @param name will fill <code>{name}</code> in <code>logging.appender.{name}.encoder.ecs.</code>. Property name prefix.
	 */
	public EcsEncoderBuilder(
			java.lang.String name
			) {
		try {
			java.util.Map<String,String> prefixParameters = java.util.Map.of(
					"name", name
				);
			this.propertyPrefix = LogProperties.interpolateKey(PROPERTY_PREFIX, prefixParameters);
			this.name = name;
			property_serviceName = LogProperties.interpolateKey(PROPERTY_serviceName, prefixParameters);
			property_serviceVersion = LogProperties.interpolateKey(PROPERTY_serviceVersion, prefixParameters);
			property_serviceEnvironment = LogProperties.interpolateKey(PROPERTY_serviceEnvironment, prefixParameters);
			property_serviceNodeName = LogProperties.interpolateKey(PROPERTY_serviceNodeName, prefixParameters);
			property_eventDataset = LogProperties.interpolateKey(PROPERTY_eventDataset, prefixParameters);
			property_structured = LogProperties.interpolateKey(PROPERTY_structured, prefixParameters);
			property_prettyPrint = LogProperties.interpolateKey(PROPERTY_prettyPrint, prefixParameters);
			property_maxBufferSize = LogProperties.interpolateKey(PROPERTY_maxBufferSize, prefixParameters);
		}
		catch (IllegalArgumentException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}


	
	/**
	 * Sets serviceName.
	 * <code>service.name</code> field, or null to omit.
	 * Default is <code>null</code>.
	 * @param serviceName <code>{@value #PROPERTY_serviceName } = java.lang.String</code> <code>service.name</code> field, or null to omit.
	 * @return this builder.
	 */
	public EcsEncoderBuilder serviceName(java.lang.@org.jspecify.annotations.Nullable String serviceName) {
		this.serviceName = serviceName;
		return this;
	}
	
	/**
	 * Sets serviceVersion.
	 * <code>service.version</code> field, or null to omit.
	 * Default is <code>null</code>.
	 * @param serviceVersion <code>{@value #PROPERTY_serviceVersion } = java.lang.String</code> <code>service.version</code> field, or null to omit.
	 * @return this builder.
	 */
	public EcsEncoderBuilder serviceVersion(java.lang.@org.jspecify.annotations.Nullable String serviceVersion) {
		this.serviceVersion = serviceVersion;
		return this;
	}
	
	/**
	 * Sets serviceEnvironment.
	 * <code>service.environment</code> field, or null to omit.
	 * Default is <code>null</code>.
	 * @param serviceEnvironment <code>{@value #PROPERTY_serviceEnvironment } = java.lang.String</code> <code>service.environment</code> field, or null to omit.
	 * @return this builder.
	 */
	public EcsEncoderBuilder serviceEnvironment(java.lang.@org.jspecify.annotations.Nullable String serviceEnvironment) {
		this.serviceEnvironment = serviceEnvironment;
		return this;
	}
	
	/**
	 * Sets serviceNodeName.
	 * <code>service.node.name</code> field, or null to omit.
	 * Default is <code>null</code>.
	 * @param serviceNodeName <code>{@value #PROPERTY_serviceNodeName } = java.lang.String</code> <code>service.node.name</code> field, or null to omit.
	 * @return this builder.
	 */
	public EcsEncoderBuilder serviceNodeName(java.lang.@org.jspecify.annotations.Nullable String serviceNodeName) {
		this.serviceNodeName = serviceNodeName;
		return this;
	}
	
	/**
	 * Sets eventDataset.
	 * <code>event.dataset</code> field, or null to omit.
	 * Default is <code>null</code>.
	 * @param eventDataset <code>{@value #PROPERTY_eventDataset } = java.lang.String</code> <code>event.dataset</code> field, or null to omit.
	 * @return this builder.
	 */
	public EcsEncoderBuilder eventDataset(java.lang.@org.jspecify.annotations.Nullable String eventDataset) {
		this.eventDataset = eventDataset;
		return this;
	}
	
	/**
	 * Sets structured.
	 * <code>true</code> nests fields as JSON objects (Spring Boot's ECS shape) instead of flattened dotted field names, default is false.
	 * Default is <code>null</code>.
	 * @param structured <code>{@value #PROPERTY_structured } = java.lang.Boolean</code> <code>true</code> nests fields as JSON objects (Spring Boot's ECS shape) instead of flattened dotted field names, default is false.
	 * @return this builder.
	 */
	public EcsEncoderBuilder structured(java.lang.@org.jspecify.annotations.Nullable Boolean structured) {
		this.structured = structured;
		return this;
	}
	
	/**
	 * Sets prettyPrint.
	 * <code>true</code> will pretty print the JSON, default is false.
	 * Default is <code>null</code>.
	 * @param prettyPrint <code>{@value #PROPERTY_prettyPrint } = java.lang.Boolean</code> <code>true</code> will pretty print the JSON, default is false.
	 * @return this builder.
	 */
	public EcsEncoderBuilder prettyPrint(java.lang.@org.jspecify.annotations.Nullable Boolean prettyPrint) {
		this.prettyPrint = prettyPrint;
		return this;
	}
	
	/**
	 * Sets maxBufferSize.
	 * Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 * Default is <code>null</code>.
	 * @param maxBufferSize <code>{@value #PROPERTY_maxBufferSize } = java.lang.Integer</code> Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 * @return this builder.
	 */
	public EcsEncoderBuilder maxBufferSize(java.lang.@org.jspecify.annotations.Nullable Integer maxBufferSize) {
		this.maxBufferSize = maxBufferSize;
		return this;
	}
	
	/**
	 * Creates {@code io.jstach.rainbowgum.json.encoder.EcsEncoder } from this builder.
	 * @return {@code io.jstach.rainbowgum.json.encoder.EcsEncoder }.
	 */
	public io.jstach.rainbowgum.json.encoder.EcsEncoder build() {
		try {
			return io.jstach.rainbowgum.json.encoder.EcsEncoder.of(
					this.name
					, this.serviceName
					, this.serviceVersion
					, this.serviceEnvironment
					, this.serviceNodeName
					, this.eventDataset
					, this.structured
					, this.prettyPrint
					, this.maxBufferSize
					);
		}
		catch (IllegalArgumentException | LogProperty.PropertyMissingException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}
	
	@Override
	public EcsEncoderBuilder fromProperties(LogProperties properties) {
		var __v = LogProperty.Validator.of(this.getClass());
		var _serviceName = properties.forKey(property_serviceName).ofString().or(this.serviceName).validateIfError(__v);
		var _serviceVersion = properties.forKey(property_serviceVersion).ofString().or(this.serviceVersion).validateIfError(__v);
		var _serviceEnvironment = properties.forKey(property_serviceEnvironment).ofString().or(this.serviceEnvironment).validateIfError(__v);
		var _serviceNodeName = properties.forKey(property_serviceNodeName).ofString().or(this.serviceNodeName).validateIfError(__v);
		var _eventDataset = properties.forKey(property_eventDataset).ofString().or(this.eventDataset).validateIfError(__v);
		var _structured = properties.forKey(property_structured).ofBoolean().or(this.structured).validateIfError(__v);
		var _prettyPrint = properties.forKey(property_prettyPrint).ofBoolean().or(this.prettyPrint).validateIfError(__v);
		var _maxBufferSize = properties.forKey(property_maxBufferSize).ofInt().or(this.maxBufferSize).validateIfError(__v);
		__v.validate();
		this.serviceName = _serviceName.valueOrNull();
		this.serviceVersion = _serviceVersion.valueOrNull();
		this.serviceEnvironment = _serviceEnvironment.valueOrNull();
		this.serviceNodeName = _serviceNodeName.valueOrNull();
		this.eventDataset = _eventDataset.valueOrNull();
		this.structured = _structured.valueOrNull();
		this.prettyPrint = _prettyPrint.valueOrNull();
		this.maxBufferSize = _maxBufferSize.valueOrNull();
		return this;
	}
	
	/**
	 * The interpolated property prefix: {@value #PROPERTY_PREFIX}.
	 * @return resolved prefix which should end with a "<code>.</code>".
	 */
	@Override
	public String propertyPrefix() {
		return this.propertyPrefix;
	}
	
}