Skip to content

Commit 4fa916a

Browse files
authored
4.x: Value mapping API and implementation in modules. (helidon-io#7517)
* Value mapping API and implementation in modules * Refactored all usages
1 parent 5d40958 commit 4fa916a

136 files changed

Lines changed: 2178 additions & 885 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/FileService.java.multipart.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ final class FileService implements HttpService {
9292
}
9393

9494
private void download(ServerRequest req, ServerResponse res) {
95-
Path filePath = storage.resolve(req.path().pathParameters().value("fname"));
95+
Path filePath = storage.resolve(req.path().pathParameters().get("fname"));
9696
if (!filePath.getParent().equals(storage)) {
9797
res.status(BAD_REQUEST_400).send("Invalid file name");
9898
return;

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/FtService.java.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public class FtService implements HttpService {
5757
}
5858

5959
private void timeoutHandler(ServerRequest request, ServerResponse response) {
60-
long sleep = Long.parseLong(request.path().pathParameters().value("millis"));
60+
long sleep = request.path().pathParameters().first("millis").getLong();
6161
6262
response.send(timeout.invoke(() -> sleep(sleep)));
6363
}
6464

6565
private void retryHandler(ServerRequest request, ServerResponse response) {
66-
int count = Integer.parseInt(request.path().pathParameters().value("count"));
66+
int count = request.path().pathParameters().first("count").getInt();
6767
6868
AtomicInteger call = new AtomicInteger(1);
6969
AtomicInteger failures = new AtomicInteger();
@@ -79,7 +79,7 @@ public class FtService implements HttpService {
7979
}
8080

8181
private void fallbackHandler(ServerRequest request, ServerResponse response) {
82-
boolean success = "true".equalsIgnoreCase(request.path().pathParameters().value("success"));
82+
boolean success = request.path().pathParameters().first("success").getBoolean();
8383
if (success) {
8484
response.send(fallback.invoke(this::reactiveData));
8585
} else {
@@ -88,7 +88,7 @@ public class FtService implements HttpService {
8888
}
8989

9090
private void circuitBreakerHandler(ServerRequest request, ServerResponse response) {
91-
boolean success = "true".equalsIgnoreCase(request.path().pathParameters().value("success"));
91+
boolean success = request.path().pathParameters().first("success").getBoolean();
9292
if (success) {
9393
response.send(circuitBreaker.invoke(this::reactiveData));
9494
} else {
@@ -97,7 +97,7 @@ public class FtService implements HttpService {
9797
}
9898

9999
private void bulkheadHandler(ServerRequest request, ServerResponse response) {
100-
long sleep = Long.parseLong(request.path().pathParameters().value("millis"));
100+
long sleep = request.path().pathParameters().first("millis").getLong();
101101
CompletableFuture<String> future = async.invoke(() -> bulkhead.invoke(() -> sleep(sleep)));
102102
sleep(100);
103103
if (bulkhead.stats().callsRejected() > 0) {

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/GreetService.java.json.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class GreetService implements HttpService {
6565
*/
6666
private void getMessageHandler(ServerRequest request,
6767
ServerResponse response) {
68-
String name = request.path().pathParameters().value("name");
68+
String name = request.path().pathParameters().get("name");
6969
sendResponse(response, name);
7070
}
7171

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/GreetService.java.jsonp.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class GreetService implements HttpService {
8686
*/
8787
private void getMessageHandler(ServerRequest request,
8888
ServerResponse response) {
89-
String name = request.path().pathParameters().value("name");
89+
String name = request.path().pathParameters().get("name");
9090
sendResponse(response, name);
9191
}
9292

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/GreetService.java.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class GreetService implements HttpService {
8080
*/
8181
private void getMessageHandler(ServerRequest request,
8282
ServerResponse response) {
83-
String name = request.path().pathParameters().value("name");
83+
String name = request.path().pathParameters().get("name");
8484
sendResponse(response, name);
8585
}
8686

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/MetricsService.java.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class MetricsService implements HttpService {
7272
*/
7373
private void getMessageHandler(ServerRequest request,
7474
ServerResponse response) {
75-
String name = request.path().pathParameters().value("name");
75+
String name = request.path().pathParameters().get("name");
7676
sendResponse(response, name);
7777
}
7878

archetypes/helidon/src/main/archetype/se/database/files/src/main/java/__pkg__/PokemonMapper.java.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PokemonMapper implements DbMapper<Pokemon> {
2323
}
2424

2525
DbColumn type = row.column("type");
26-
return new Pokemon(name.as(String.class), type.as(String.class));
26+
return new Pokemon(name.get(String.class), type.get(String.class));
2727
}
2828

2929
@Override

archetypes/helidon/src/main/archetype/se/database/files/src/main/java/__pkg__/PokemonService.java.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class PokemonService implements HttpService {
9191
private void insertPokemonSimple(ServerRequest req, ServerResponse res) {
9292
Parameters params = req.path().pathParameters();
9393
// Test Pokémon POJO mapper
94-
Pokemon pokemon = new Pokemon(params.value("name"), params.value("type"));
94+
Pokemon pokemon = new Pokemon(params.get("name"), params.get("type"));
9595
9696
long count = dbClient.execute().createNamedInsert("insert2")
9797
.namedParam(pokemon)
@@ -106,7 +106,7 @@ public class PokemonService implements HttpService {
106106
* @param res server response
107107
*/
108108
private void getPokemon(ServerRequest req, ServerResponse res) {
109-
String pokemonName = req.path().pathParameters().value("name");
109+
String pokemonName = req.path().pathParameters().get("name");
110110
res.send(dbClient.execute()
111111
.namedGet("select-one", pokemonName)
112112
.orElseThrow(() -> new NotFoundException("Pokemon " + pokemonName + " not found"))
@@ -141,8 +141,8 @@ public class PokemonService implements HttpService {
141141
*/
142142
private void updatePokemonType(ServerRequest req, ServerResponse res) {
143143
Parameters params = req.path().pathParameters();
144-
String name = params.value("name");
145-
String type = params.value("type");
144+
String name = params.get("name");
145+
String type = params.get("type");
146146
long count = dbClient.execute()
147147
.createNamedUpdate("update")
148148
.addParam("name", name)
@@ -177,7 +177,7 @@ public class PokemonService implements HttpService {
177177
* @param res the server response
178178
*/
179179
private void deletePokemon(ServerRequest req, ServerResponse res) {
180-
String name = req.path().pathParameters().value("name");
180+
String name = req.path().pathParameters().get("name");
181181
long count = dbClient.execute().namedDelete("delete", name);
182182
res.send("Deleted: " + count + " values");
183183
}

common/config/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<groupId>io.helidon.common</groupId>
3434
<artifactId>helidon-common</artifactId>
3535
</dependency>
36+
<dependency>
37+
<groupId>io.helidon.common</groupId>
38+
<artifactId>helidon-common-mapper</artifactId>
39+
</dependency>
3640
<dependency>
3741
<groupId>org.junit.jupiter</groupId>
3842
<artifactId>junit-jupiter-api</artifactId>

common/config/src/main/java/io/helidon/common/config/ConfigValue.java

Lines changed: 10 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
package io.helidon.common.config;
1818

1919
import java.util.Optional;
20-
import java.util.function.Consumer;
2120
import java.util.function.Function;
22-
import java.util.function.Predicate;
2321
import java.util.function.Supplier;
24-
import java.util.stream.Stream;
22+
23+
import io.helidon.common.GenericType;
24+
import io.helidon.common.mapper.OptionalValue;
2525

2626
/**
2727
* A typed value of a {@link Config} node.
@@ -31,7 +31,7 @@
3131
*
3232
* @param <T> type of the value
3333
*/
34-
public interface ConfigValue<T> {
34+
public interface ConfigValue<T> extends OptionalValue<T> {
3535

3636
/**
3737
* Returns the fully-qualified key of the originating {@code Config} node.
@@ -76,213 +76,25 @@ public interface ConfigValue<T> {
7676
* @see #key()
7777
* @see io.helidon.common.config.Config.Key#name()
7878
*/
79+
@Override
7980
default String name() {
8081
return key().name();
8182
}
8283

83-
/**
84-
* Returns a typed value as {@link java.util.Optional}.
85-
* Returns a {@link java.util.Optional#empty() empty} for nodes without a value.
86-
* As this class implements all methods of {@link java.util.Optional}, this is only a utility method if an actual
87-
* {@link java.util.Optional}
88-
* instance is needed.
89-
*
90-
* @return value as type instance as {@link java.util.Optional}, {@link java.util.Optional#empty() empty} in case the node
91-
* does not have
92-
* a direct value
93-
* @throws io.helidon.common.config.ConfigException in case the value cannot be converted to the expected type
94-
* @see #get()
95-
*/
96-
Optional<T> asOptional() throws ConfigException;
97-
98-
/**
99-
* Typed value of the represented {@link Config} node.
100-
* Throws {@link io.helidon.common.config.ConfigException} if the node is Type#MISSING type.
101-
*
102-
* @return direct value of this node converted to the expected type
103-
* @throws io.helidon.common.config.ConfigException in case the value cannot be converted to the expected type
104-
*/
105-
T get() throws ConfigException;
106-
10784
/**
10885
* Convert this {@code ConfigValue} to a different type using a mapper function.
10986
*
11087
* @param mapper mapper to map the type of this {@code ConfigValue} to a type of the returned {@code ConfigValue}
11188
* @param <N> type of the returned {@code ConfigValue}
11289
* @return a new value with the new type
11390
*/
114-
<N> ConfigValue<N> as(Function<T, N> mapper);
115-
116-
/**
117-
* Return {@code true} if there is a value present, otherwise {@code false}.
118-
* <p>
119-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
120-
*
121-
* @return {@code true} if there is a value present, otherwise {@code false}
122-
* @see java.util.Optional#isPresent()
123-
*/
124-
default boolean isPresent() {
125-
return asOptional().isPresent();
126-
}
127-
128-
/**
129-
* If a value is present, performs the given action with the value,
130-
* otherwise performs the given empty-based action.
131-
*
132-
* @param action the action to be performed, if a value is present
133-
* @param emptyAction the empty-based action to be performed, if no value is
134-
* present
135-
* @throws NullPointerException if a value is present and the given action
136-
* is {@code null}, or no value is present and the given empty-based
137-
* action is {@code null}.
138-
*/
139-
default void ifPresentOrElse(Consumer<T> action, Runnable emptyAction) {
140-
Optional<T> optional = asOptional();
141-
if (optional.isPresent()) {
142-
action.accept(optional.get());
143-
} else {
144-
emptyAction.run();
145-
}
146-
}
91+
<N> ConfigValue<N> as(Function<? super T, ? extends N> mapper);
14792

148-
/**
149-
* If a value is present, invoke the specified consumer with the value,
150-
* otherwise do nothing.
151-
* <p>
152-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
153-
*
154-
* @param consumer block to be executed if a value is present
155-
* @throws NullPointerException if value is present and {@code consumer} is
156-
* null
157-
* @see java.util.Optional#ifPresent(java.util.function.Consumer)
158-
*/
159-
default void ifPresent(Consumer<? super T> consumer) {
160-
asOptional().ifPresent(consumer);
161-
}
93+
@Override
94+
<N> ConfigValue<N> as(Class<N> type);
16295

163-
/**
164-
* If a value is present, and the value matches the given predicate,
165-
* return an {@code Optional} describing the value, otherwise return an
166-
* empty {@code Optional}.
167-
* <p>
168-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
169-
*
170-
* @param predicate a predicate to apply to the value, if present
171-
* @return an {@code Optional} describing the value of this {@code Optional}
172-
* if a value is present and the value matches the given predicate,
173-
* otherwise an empty {@code Optional}
174-
* @throws NullPointerException if the predicate is null
175-
* @see java.util.Optional#filter(java.util.function.Predicate)
176-
*/
177-
default Optional<T> filter(Predicate<? super T> predicate) {
178-
return asOptional().filter(predicate);
179-
}
180-
181-
/**
182-
* If a value is present, apply the provided mapping function to it,
183-
* and if the result is non-null, return an {@code Optional} describing the
184-
* result. Otherwise return an empty {@code Optional}.
185-
*
186-
* @param <U> The type of the result of the mapping function
187-
* @param mapper a mapping function to apply to the value, if present
188-
* @return an {@code Optional} describing the result of applying a mapping
189-
* function to the value of this {@code Optional}, if a value is present,
190-
* otherwise an empty {@code Optional}
191-
* @throws NullPointerException if the mapping function is null
192-
*
193-
* <p>
194-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
195-
* @see java.util.Optional#map(java.util.function.Function)
196-
*/
197-
default <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
198-
return asOptional().map(mapper);
199-
}
200-
201-
/**
202-
* If a value is present, apply the provided {@code Optional}-bearing
203-
* mapping function to it, return that result, otherwise return an empty
204-
* {@code Optional}. This method is similar to {@link #map(java.util.function.Function)},
205-
* but the provided mapper is one whose result is already an {@code Optional},
206-
* and if invoked, {@code flatMap} does not wrap it with an additional
207-
* {@code Optional}.
208-
*
209-
* <p>
210-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
211-
*
212-
* @param <U> The type parameter to the {@code Optional} returned by
213-
* @param mapper a mapping function to apply to the value, if present
214-
* the mapping function
215-
* @return the result of applying an {@code Optional}-bearing mapping
216-
* function to the value of this {@code Optional}, if a value is present,
217-
* otherwise an empty {@code Optional}
218-
* @throws NullPointerException if the mapping function is null or returns
219-
* a null result
220-
* @see java.util.Optional#flatMap(java.util.function.Function)
221-
*/
222-
default <U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
223-
return asOptional().flatMap(mapper);
224-
}
225-
226-
/**
227-
* Return the value if present, otherwise return {@code other}.
228-
* <p>
229-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
230-
*
231-
* @param other the value to be returned if there is no value present, may
232-
* be null
233-
* @return the value, if present, otherwise {@code other}
234-
* @see java.util.Optional#orElse(Object)
235-
*/
236-
default T orElse(T other) {
237-
return asOptional().orElse(other);
238-
}
239-
240-
/**
241-
* Return the value if present, otherwise invoke {@code other} and return
242-
* the result of that invocation.
243-
* <p>
244-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
245-
*
246-
* @param other a {@code Supplier} whose result is returned if no value
247-
* is present
248-
* @return the value if present otherwise the result of {@code other.get()}
249-
* @throws NullPointerException if value is not present and {@code other} is
250-
* null
251-
* @see java.util.Optional#orElseGet(java.util.function.Supplier)
252-
*/
253-
default T orElseGet(Supplier<? extends T> other) {
254-
return asOptional().orElseGet(other);
255-
}
256-
257-
/**
258-
* Return the contained value, if present, otherwise throw an exception
259-
* to be created by the provided supplier.
260-
*
261-
* <p>
262-
* Copied from {@link java.util.Optional}. You can get real optional from {@link #asOptional()}.
263-
*
264-
* @param <X> Type of the exception to be thrown
265-
* @param exceptionSupplier The supplier which will return the exception to
266-
* be thrown
267-
* @return the present value
268-
* @throws X if there is no value present
269-
* @throws NullPointerException if no value is present and
270-
* {@code exceptionSupplier} is null
271-
* @see java.util.Optional#orElseThrow(java.util.function.Supplier)
272-
*/
273-
default <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
274-
return asOptional().orElseThrow(exceptionSupplier);
275-
}
276-
277-
/**
278-
* If a value is present, returns a sequential {@link java.util.stream.Stream} containing
279-
* only that value, otherwise returns an empty {@code Stream}.
280-
*
281-
* @return the optional value as a {@code Stream}
282-
*/
283-
default Stream<T> stream() {
284-
return asOptional().stream();
285-
}
96+
@Override
97+
<N> ConfigValue<N> as(GenericType<N> type);
28698

28799
/**
288100
* Returns a supplier of a typed value. The semantics depend on implementation, this may always return the

0 commit comments

Comments
 (0)