Skip to content

[MNG-8515] Use internal interpolator #2044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/
package org.apache.maven.api.cli;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
Expand Down Expand Up @@ -201,11 +201,11 @@ public interface Options {
/**
* Returns a new instance of {@link Options} with values interpolated using the given properties.
*
* @param properties a collection of property maps to use for interpolation
* @param callback the callback to use for interpolation
* @return a new {@link Options} instance with interpolated values
*/
@Nonnull
Options interpolate(@Nonnull Collection<Map<String, String>> properties);
Options interpolate(@Nonnull UnaryOperator<String> callback);

/**
* Emits warning messages if deprecated options are used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/
package org.apache.maven.api.cli.mvn;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
Expand Down Expand Up @@ -217,11 +216,11 @@ public interface MavenOptions extends Options {
Optional<List<String>> goals();

/**
* Returns a new instance of {@link MavenOptions} with values interpolated using the given properties.
* Returns a new instance of {@link MavenOptions} with values interpolated using the given callback.
*
* @param properties a collection of property maps to use for interpolation
* @param callback a callback to use for interpolation
* @return a new MavenOptions instance with interpolated values
*/
@Nonnull
MavenOptions interpolate(@Nonnull Collection<Map<String, String>> properties);
MavenOptions interpolate(@Nonnull UnaryOperator<String> callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/
package org.apache.maven.api.cli.mvnenc;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
Expand Down Expand Up @@ -61,9 +60,9 @@ public interface EncryptOptions extends Options {
/**
* Returns a new instance of EncryptOptions with values interpolated using the given properties.
*
* @param properties a collection of property maps to use for interpolation
* @param callback a callback to use for interpolation
* @return a new EncryptOptions instance with interpolated values
*/
@Nonnull
EncryptOptions interpolate(Collection<Map<String, String>> properties);
EncryptOptions interpolate(UnaryOperator<String> callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.apache.maven.api.cli.mvnsh;

import java.util.Collection;
import java.util.Map;
import java.util.function.UnaryOperator;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
Expand All @@ -36,9 +35,9 @@ public interface ShellOptions extends Options {
/**
* Returns a new instance of ShellOptions with values interpolated using the given properties.
*
* @param properties a collection of property maps to use for interpolation
* @param callback a callback to use for interpolation
* @return a new EncryptOptions instance with interpolated values
*/
@Nonnull
ShellOptions interpolate(Collection<Map<String, String>> properties);
ShellOptions interpolate(UnaryOperator<String> callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;

import org.apache.maven.api.Service;
import org.apache.maven.api.annotations.Experimental;
Expand All @@ -47,7 +48,7 @@ public interface Interpolator extends Service {
* @param properties The map containing key-value pairs to be interpolated.
* @param callback The function to resolve variable values not found in the map.
*/
default void interpolate(@Nonnull Map<String, String> properties, @Nullable Function<String, String> callback) {
default void interpolate(@Nonnull Map<String, String> properties, @Nullable UnaryOperator<String> callback) {
interpolate(properties, callback, null, true);
}

Expand All @@ -59,7 +60,7 @@ default void interpolate(@Nonnull Map<String, String> properties, @Nullable Func
* @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. If false, they are left unchanged.
*/
default void interpolate(
@Nonnull Map<String, String> map, @Nullable Function<String, String> callback, boolean defaultsToEmpty) {
@Nonnull Map<String, String> map, @Nullable UnaryOperator<String> callback, boolean defaultsToEmpty) {
interpolate(map, callback, null, defaultsToEmpty);
}

Expand All @@ -72,8 +73,8 @@ default void interpolate(
*/
void interpolate(
@Nonnull Map<String, String> map,
@Nullable Function<String, String> callback,
@Nullable BiFunction<String, String, String> postprocessor,
@Nullable UnaryOperator<String> callback,
@Nullable BinaryOperator<String> postprocessor,
boolean defaultsToEmpty);

/**
Expand All @@ -85,7 +86,7 @@ void interpolate(
* @return The interpolated string, or null if the input was null.
*/
@Nullable
default String interpolate(@Nullable String val, @Nullable Function<String, String> callback) {
default String interpolate(@Nullable String val, @Nullable UnaryOperator<String> callback) {
return interpolate(val, callback, false);
}

Expand All @@ -99,7 +100,7 @@ default String interpolate(@Nullable String val, @Nullable Function<String, Stri
*/
@Nullable
default String interpolate(
@Nullable String val, @Nullable Function<String, String> callback, boolean defaultsToEmpty) {
@Nullable String val, @Nullable UnaryOperator<String> callback, boolean defaultsToEmpty) {
return interpolate(val, callback, null, defaultsToEmpty);
}

Expand All @@ -114,8 +115,8 @@ default String interpolate(
@Nullable
String interpolate(
@Nullable String val,
@Nullable Function<String, String> callback,
@Nullable BiFunction<String, String, String> postprocessor,
@Nullable UnaryOperator<String> callback,
@Nullable BinaryOperator<String> postprocessor,
boolean defaultsToEmpty);

/**
Expand All @@ -127,9 +128,9 @@ String interpolate(
*
* @throws NullPointerException if the input collection is null or contains null elements.
*/
static Function<String, String> chain(Collection<? extends Function<String, String>> functions) {
static UnaryOperator<String> chain(Collection<? extends UnaryOperator<String>> functions) {
return s -> {
for (Function<String, String> function : functions) {
for (UnaryOperator<String> function : functions) {
String v = function.apply(s);
if (v != null) {
return v;
Expand All @@ -140,7 +141,7 @@ static Function<String, String> chain(Collection<? extends Function<String, Stri
}

@SafeVarargs
static Function<String, String> chain(Function<String, String>... functions) {
static UnaryOperator<String> chain(UnaryOperator<String>... functions) {
return chain(List.of(functions));
}

Expand All @@ -150,14 +151,14 @@ static Function<String, String> chain(Function<String, String>... functions) {
* improving performance for repeated calls with the same input.
*
* @param callback The original function to be memoized. It takes a String as input and returns a String.
* @return A new {@code Function<String, String>} that caches the results of the original function.
* @return A new {@code UnaryOperator<String>} that caches the results of the original function.
* If the original function returns null for a given input, null will be cached and returned for subsequent calls with the same input.
*
* @see Function
* @see Optional
* @see HashMap#computeIfAbsent(Object, Function)
*/
static Function<String, String> memoize(Function<String, String> callback) {
static UnaryOperator<String> memoize(UnaryOperator<String> callback) {
Map<String, Optional<String>> cache = new HashMap<>();
return s -> cache.computeIfAbsent(s, v -> Optional.ofNullable(callback.apply(v)))
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.UnaryOperator;

import org.apache.maven.api.ProtoSession;
import org.apache.maven.api.annotations.Experimental;
Expand Down Expand Up @@ -72,7 +72,7 @@ public interface SettingsBuilderRequest {
* @return the interpolation source for interpolation
*/
@Nonnull
Optional<Function<String, String>> getInterpolationSource();
Optional<UnaryOperator<String>> getInterpolationSource();

@Nonnull
static SettingsBuilderRequest build(
Expand Down Expand Up @@ -136,7 +136,7 @@ class SettingsBuilderRequestBuilder {
Source installationSettingsSource;
Source projectSettingsSource;
Source userSettingsSource;
Function<String, String> interpolationSource;
UnaryOperator<String> interpolationSource;

public SettingsBuilderRequestBuilder session(ProtoSession session) {
this.session = session;
Expand All @@ -158,7 +158,7 @@ public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSourc
return this;
}

public SettingsBuilderRequestBuilder interpolationSource(Function<String, String> interpolationSource) {
public SettingsBuilderRequestBuilder interpolationSource(UnaryOperator<String> interpolationSource) {
this.interpolationSource = interpolationSource;
return this;
}
Expand All @@ -177,15 +177,15 @@ private static class DefaultSettingsBuilderRequest extends BaseRequest<ProtoSess
private final Source installationSettingsSource;
private final Source projectSettingsSource;
private final Source userSettingsSource;
private final Function<String, String> interpolationSource;
private final UnaryOperator<String> interpolationSource;

@SuppressWarnings("checkstyle:ParameterNumber")
DefaultSettingsBuilderRequest(
@Nonnull ProtoSession session,
@Nullable Source installationSettingsSource,
@Nullable Source projectSettingsSource,
@Nullable Source userSettingsSource,
@Nullable Function<String, String> interpolationSource) {
@Nullable UnaryOperator<String> interpolationSource) {
super(session);
this.installationSettingsSource = installationSettingsSource;
this.projectSettingsSource = projectSettingsSource;
Expand Down Expand Up @@ -213,7 +213,7 @@ public Optional<Source> getUserSettingsSource() {

@Nonnull
@Override
public Optional<Function<String, String>> getInterpolationSource() {
public Optional<UnaryOperator<String>> getInterpolationSource() {
return Optional.ofNullable(interpolationSource);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.maven.cli;

import java.util.function.Function;
import java.util.function.UnaryOperator;

import com.google.inject.Binder;
import com.google.inject.Module;
Expand All @@ -36,10 +36,10 @@
public class ExtensionConfigurationModule implements Module {

private final CoreExtensionEntry extension;
private final Function<String, String> callback;
private final UnaryOperator<String> callback;
private final DefaultInterpolator interpolator = new DefaultInterpolator();

public ExtensionConfigurationModule(CoreExtensionEntry extension, Function<String, String> callback) {
public ExtensionConfigurationModule(CoreExtensionEntry extension, UnaryOperator<String> callback) {
this.extension = extension;
this.callback = callback;
}
Expand All @@ -51,8 +51,8 @@ public void configure(Binder binder) {
if (configuration == null) {
configuration = new XmlNodeImpl("configuration");
}
Function<String, String> cb = Interpolator.memoize(callback);
Function<String, String> it = s -> interpolator.interpolate(s, cb);
UnaryOperator<String> cb = Interpolator.memoize(callback);
UnaryOperator<String> it = s -> interpolator.interpolate(s, cb);
configuration = new ExtensionInterpolator(it).transform(configuration);

binder.bind(XmlNode.class)
Expand All @@ -65,7 +65,7 @@ public void configure(Binder binder) {
}

static class ExtensionInterpolator extends MavenTransformer {
ExtensionInterpolator(Function<String, String> transformer) {
ExtensionInterpolator(UnaryOperator<String> transformer) {
super(transformer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
Expand Down Expand Up @@ -643,7 +643,7 @@ void properties(CliRequest cliRequest) throws Exception {
populateProperties(cliRequest.commandLine, paths, cliRequest.systemProperties, cliRequest.userProperties);

// now that we have properties, interpolate all arguments
Function<String, String> callback = v -> {
UnaryOperator<String> callback = v -> {
String r = paths.getProperty(v);
if (r == null) {
r = cliRequest.systemProperties.getProperty(v);
Expand Down Expand Up @@ -724,7 +724,7 @@ protected void configure() {

container.setLoggerManager(plexusLoggerManager);

Function<String, String> extensionSource = expression -> {
UnaryOperator<String> extensionSource = expression -> {
String value = cliRequest.userProperties.getProperty(expression);
if (value == null) {
value = cliRequest.systemProperties.getProperty(expression);
Expand Down Expand Up @@ -1660,7 +1660,7 @@ void populateProperties(
// ----------------------------------------------------------------------
// Load config files
// ----------------------------------------------------------------------
Function<String, String> callback =
UnaryOperator<String> callback =
or(paths::getProperty, prefix("cli.", commandLine::getOptionValue), systemProperties::getProperty);

Path mavenConf;
Expand All @@ -1686,7 +1686,7 @@ void populateProperties(
.forEach(k -> System.setProperty(k, userProperties.getProperty(k)));
}

private static Function<String, String> prefix(String prefix, Function<String, String> cb) {
private static UnaryOperator<String> prefix(String prefix, UnaryOperator<String> cb) {
return s -> {
String v = null;
if (s.startsWith(prefix)) {
Expand All @@ -1696,9 +1696,9 @@ private static Function<String, String> prefix(String prefix, Function<String, S
};
}

private static Function<String, String> or(Function<String, String>... callbacks) {
private static UnaryOperator<String> or(UnaryOperator<String>... callbacks) {
return s -> {
for (Function<String, String> cb : callbacks) {
for (UnaryOperator<String> cb : callbacks) {
String r = cb.apply(s);
if (r != null) {
return r;
Expand Down
Loading
Loading