Skip to content

Commit 14b0562

Browse files
committed
- ConfigParams.withNamespace()
- ConfigParams.withInfoLog() - ConfigParams.withDebugLog()
1 parent 4c49387 commit 14b0562

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
## [Unreleased]
1616

17+
### Added
18+
19+
- ConfigParams.withNamespace()
20+
- ConfigParams.withInfoLog()
21+
- ConfigParams.withDebugLog()
22+
1723
## [1.3.1 - 2024-04-10]
1824

1925
### Added

simple-config/src/main/java/org/fugerit/java/simple/config/ConfigParams.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,33 @@
44

55
public interface ConfigParams {
66

7-
String getValue( String name );
7+
String getValue( final String name );
88

9-
Optional<String> getOptionalValue( String name );
9+
Optional<String> getOptionalValue( final String name );
10+
11+
default ConfigParams withDebugLog() {
12+
return ConfigParamsLogger.wrapLogDebug( this );
13+
}
14+
15+
default ConfigParams withInfoLog() {
16+
return ConfigParamsLogger.wrapLogInfo( this );
17+
}
18+
19+
default ConfigParams withNamespace( final String namespace ) {
20+
return new ConfigParamsWrapper( this ) {
21+
@Override
22+
public String getValue(String name) {
23+
return super.getValue(namespace+name);
24+
}
25+
@Override
26+
public Optional<String> getOptionalValue(String name) {
27+
return super.getOptionalValue(namespace+name);
28+
}
29+
@Override
30+
public String toString() {
31+
return ConfigParams.class.getSimpleName()+"[namespace:"+namespace+"]";
32+
}
33+
};
34+
}
1035

1136
}

simple-config/src/main/java/org/fugerit/java/simple/config/ConfigParamsLogger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public static ConfigParams wrapLogDebug( ConfigParams params ) {
1212
return new ConfigParamsLogger( params );
1313
}
1414

15+
public static ConfigParams wrapLogInfo( ConfigParams params ) {
16+
return new ConfigParamsLogger( params, ( n, v ) -> log.debug( "wraps : {}, name : {}, value : {} ", params, n, v ) );
17+
}
18+
1519
private BiConsumer<String, Object> logFun;
1620

1721
public ConfigParamsLogger(final ConfigParams wrapped) {

simple-config/src/test/java/teat/org/fugerit/java/simple/config/TestConfigParamsDefault.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package teat.org.fugerit.java.simple.config;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.fugerit.java.core.util.PropsIO;
45
import org.fugerit.java.simple.config.*;
56
import org.junit.jupiter.api.Assertions;
@@ -9,20 +10,21 @@
910
import java.util.Optional;
1011
import java.util.Properties;
1112

13+
@Slf4j
1214
class TestConfigParamsDefault {
1315

1416
@Test
1517
void testConfigParams() throws IOException {
1618
Properties configProperties = PropsIO.loadFromClassLoaderSafe( "testconfig.properties" );
17-
ConfigParams config = ConfigParamsLogger.wrapLogDebug( new ConfigParamsDefault( configProperties ) );
19+
ConfigParams config = new ConfigParamsDefault( configProperties ).withDebugLog();
1820
String value1 = config.getValue( "testconfig.param1" );
1921
Assertions.assertEquals( "value1", value1 );
2022
Optional<String> value2 = config.getOptionalValue( "testconfig.param2" );
2123
Assertions.assertEquals( "value2", value2.get() );
2224
Optional<String> valueX = config.getOptionalValue( "testconfig.paramX" );
2325
Assertions.assertFalse( valueX.isPresent() );
2426
// test null namepsace
25-
ConfigParams configAlt = new ConfigParamsDefault( null, configProperties );
27+
ConfigParams configAlt = new ConfigParamsDefault( null, configProperties ).withInfoLog();
2628
String value3 = config.getValue( "testconfig.param3" );
2729
Assertions.assertEquals( "value3", value3 );
2830
// simple configuration
@@ -31,6 +33,11 @@ void testConfigParams() throws IOException {
3133
Assertions.assertNotNull( simpleConfig );
3234
String typeKo = typeOk+"KO";
3335
Assertions.assertNull( SimpleConfigFacade.configure( typeKo, config ) );
36+
// test with namespace
37+
ConfigParams withNamespace = config.withNamespace( "testconfig." );
38+
Assertions.assertEquals( "value1", withNamespace.getValue( "param1" ) );
39+
Assertions.assertTrue( withNamespace.getOptionalValue( "param3" ).isPresent() );
40+
log.info( "with namespace : {}", withNamespace );
3441
}
3542

3643
}

0 commit comments

Comments
 (0)