Skip to content

Commit fb72345

Browse files
committed
Polish JsonWriter API
1 parent 3e7412f commit fb72345

File tree

7 files changed

+96
-98
lines changed

7 files changed

+96
-98
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
final class PulsarPropertiesMapper {
5555

5656
private static final JsonWriter<Map<String, String>> jsonWriter = JsonWriter
57-
.of((members) -> members.addSelf().as(TreeMap::new).usingPairs(Map::forEach));
57+
.of((members) -> members.add().as(TreeMap::new).usingPairs(Map::forEach));
5858

5959
private final PulsarProperties properties;
6060

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonWriter.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ default JsonWriter<T> withSuffix(String suffix) {
142142
* @return a {@link JsonWriter} instance
143143
*/
144144
static <T> JsonWriter<T> standard() {
145-
return of(Members::addSelf);
145+
return of(Members::add);
146146
}
147147

148148
/**
@@ -306,8 +306,8 @@ public String toString() {
306306
* the various {@code add(...)} methods. Typically, members are declared with a
307307
* {@code "name"} and a {@link Function} that will extract the value from the
308308
* instance. Members can also be declared using a static value or a {@link Supplier}.
309-
* The {@link #addSelf(String)} and {@link #addSelf()} methods may be used to access
310-
* the actual instance being written.
309+
* The {@link #add(String)} and {@link #add()} methods may be used to access the
310+
* actual instance being written.
311311
* <p>
312312
* Members can be added without a {@code name} when a {@code Member.using(...)} method
313313
* is used to complete the definition.
@@ -343,7 +343,7 @@ final class Members<T> {
343343
* @param name the member name
344344
* @return the added {@link Member} which may be configured further
345345
*/
346-
public Member<T> addSelf(String name) {
346+
public Member<T> add(String name) {
347347
return add(name, (instance) -> instance);
348348
}
349349

@@ -389,58 +389,55 @@ public <V> Member<V> add(String name, Function<T, V> extractor) {
389389
* complete the configuration.
390390
* @return the added {@link Member} which may be configured further
391391
*/
392-
public Member<T> addSelf() {
393-
return add((instance) -> instance);
392+
public Member<T> add() {
393+
return from(Function.identity());
394394
}
395395

396396
/**
397-
* Add a new member with a static value. The member is added without a name, so
398-
* one of the {@code Member.using(...)} methods must be used to complete the
399-
* configuration.
397+
* Add all entries from the given {@link Map} to the JSON.
398+
* @param <M> the map type
399+
* @param <K> the key type
400400
* @param <V> the value type
401-
* @param value the member value
401+
* @param extractor a function to extract the map
402402
* @return the added {@link Member} which may be configured further
403403
*/
404-
public <V> Member<V> add(V value) {
405-
return add((instance) -> value);
404+
public <M extends Map<K, V>, K, V> Member<M> addMapEntries(Function<T, M> extractor) {
405+
return from(extractor).usingPairs(Map::forEach);
406406
}
407407

408408
/**
409-
* Add a new member with a supplied value.The member is added without a name, so
410-
* one of the {@code Member.using(...)} methods must be used to complete the
411-
* configuration.
409+
* Add members from a static value. One of the {@code Member.using(...)} methods
410+
* must be used to complete the configuration.
412411
* @param <V> the value type
413-
* @param supplier a supplier of the value
412+
* @param value the member value
414413
* @return the added {@link Member} which may be configured further
415414
*/
416-
public <V> Member<V> add(Supplier<V> supplier) {
417-
Assert.notNull(supplier, "'supplier' must not be null");
418-
return add((instance) -> supplier.get());
415+
public <V> Member<V> from(V value) {
416+
return from((instance) -> value);
419417
}
420418

421419
/**
422-
* Add a new member with an extracted value. The member is added without a name,
423-
* so one of the {@code Member.using(...)} methods must be used to complete the
424-
* configuration.
420+
* Add members from a supplied value. One of the {@code Member.using(...)} methods
421+
* must be used to complete the configuration.
425422
* @param <V> the value type
426-
* @param extractor a function to extract the value
423+
* @param supplier a supplier of the value
427424
* @return the added {@link Member} which may be configured further
428425
*/
429-
public <V> Member<V> add(Function<T, V> extractor) {
430-
Assert.notNull(extractor, "'extractor' must not be null");
431-
return addMember(null, extractor);
426+
public <V> Member<V> from(Supplier<V> supplier) {
427+
Assert.notNull(supplier, "'supplier' must not be null");
428+
return from((instance) -> supplier.get());
432429
}
433430

434431
/**
435-
* Add all entries from the given {@link Map} to the JSON.
436-
* @param <M> the map type
437-
* @param <K> the key type
432+
* Add members from an extracted value. One of the {@code Member.using(...)}
433+
* methods must be used to complete the configuration.
438434
* @param <V> the value type
439-
* @param extractor a function to extract the map
435+
* @param extractor a function to extract the value
440436
* @return the added {@link Member} which may be configured further
441437
*/
442-
public <M extends Map<K, V>, K, V> Member<M> addMapEntries(Function<T, M> extractor) {
443-
return add(extractor).usingPairs(Map::forEach);
438+
public <V> Member<V> from(Function<T, V> extractor) {
439+
Assert.notNull(extractor, "'extractor' must not be null");
440+
return addMember(null, extractor);
444441
}
445442

446443
private <V> Member<V> addMember(String name, Function<T, V> extractor) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ElasticCommonSchemaStructuredLogFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ private static void jsonMembers(ApplicationPid pid, ElasticCommonSchemaService s
5353
service.jsonMembers(members);
5454
members.add("log.logger", LogEvent::getLoggerName);
5555
members.add("message", LogEvent::getMessage).as(Message::getFormattedMessage);
56-
members.add(LogEvent::getContextData)
56+
members.from(LogEvent::getContextData)
5757
.whenNot(ReadOnlyStringMap::isEmpty)
5858
.usingPairs((contextData, pairs) -> contextData.forEach(pairs::accept));
59-
members.add(LogEvent::getThrownProxy).whenNotNull().usingMembers((thrownProxyMembers) -> {
59+
members.from(LogEvent::getThrownProxy).whenNotNull().usingMembers((thrownProxyMembers) -> {
6060
thrownProxyMembers.add("error.type", ThrowableProxy::getThrowable)
6161
.whenNotNull()
6262
.as(ObjectUtils::nullSafeClassName);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/LogstashStructuredLogFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static void jsonMembers(JsonWriter.Members<LogEvent> members) {
5656
members.add("thread_name", LogEvent::getThreadName);
5757
members.add("level", LogEvent::getLevel).as(Level::name);
5858
members.add("level_value", LogEvent::getLevel).as(Level::intLevel);
59-
members.add(LogEvent::getContextData)
59+
members.from(LogEvent::getContextData)
6060
.whenNot(ReadOnlyStringMap::isEmpty)
6161
.usingPairs((contextData, pairs) -> contextData.forEach(pairs::accept));
6262
members.add("tags", LogEvent::getMarker)

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ElasticCommonSchemaStructuredLogFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ private static void jsonMembers(ApplicationPid pid, ElasticCommonSchemaService s
5656
members.add("log.logger", ILoggingEvent::getLoggerName);
5757
members.add("message", ILoggingEvent::getFormattedMessage);
5858
members.addMapEntries(ILoggingEvent::getMDCPropertyMap);
59-
members.add(ILoggingEvent::getKeyValuePairs)
59+
members.from(ILoggingEvent::getKeyValuePairs)
6060
.whenNotEmpty()
6161
.usingExtractedPairs(Iterable::forEach, keyValuePairExtractor);
62-
members.addSelf().whenNotNull(ILoggingEvent::getThrowableProxy).usingMembers((throwableMembers) -> {
62+
members.add().whenNotNull(ILoggingEvent::getThrowableProxy).usingMembers((throwableMembers) -> {
6363
throwableMembers.add("error.type", ILoggingEvent::getThrowableProxy).as(IThrowableProxy::getClassName);
6464
throwableMembers.add("error.message", ILoggingEvent::getThrowableProxy).as(IThrowableProxy::getMessage);
6565
throwableMembers.add("error.stack_trace", throwableProxyConverter::convert);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogstashStructuredLogFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void jsonMembers(ThrowableProxyConverter throwableProxyConverter,
6262
members.add("level", ILoggingEvent::getLevel);
6363
members.add("level_value", ILoggingEvent::getLevel).as(Level::toInt);
6464
members.addMapEntries(ILoggingEvent::getMDCPropertyMap);
65-
members.add(ILoggingEvent::getKeyValuePairs)
65+
members.from(ILoggingEvent::getKeyValuePairs)
6666
.whenNotEmpty()
6767
.usingExtractedPairs(Iterable::forEach, keyValuePairExtractor);
6868
members.add("tags", ILoggingEvent::getMarkerList)

0 commit comments

Comments
 (0)