From cace66d980732a4bf2439d1d6cf3adababea937a Mon Sep 17 00:00:00 2001 From: "igor.petrenko" Date: Mon, 30 Mar 2026 19:56:08 +0300 Subject: [PATCH] CE-140 Dictionary: extends Map or List --- .../java/oap/dictionary/DictionaryTest.java | 19 ++++++- .../test-dictionary-extends-filter-map.conf | 49 +++++++++++++++++++ .../java/oap/dictionary/DictionaryParser.java | 32 ++++++++++-- pom.xml | 2 +- 4 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 oap-stdlib-test/src/test/resources/dictionary/test-dictionary-extends-filter-map.conf diff --git a/oap-stdlib-test/src/test/java/oap/dictionary/DictionaryTest.java b/oap-stdlib-test/src/test/java/oap/dictionary/DictionaryTest.java index 6cc089955..86575fb3a 100644 --- a/oap-stdlib-test/src/test/java/oap/dictionary/DictionaryTest.java +++ b/oap-stdlib-test/src/test/java/oap/dictionary/DictionaryTest.java @@ -109,8 +109,8 @@ public void extendIgnoreDuplicate() { } @Test - public void extendFilter() { - var values = Dictionaries + public void testExtendFilter() { + List values = Dictionaries .getDictionary( "test-dictionary-extends-filter" ) .getValue( "id2" ) .getValues(); @@ -122,4 +122,19 @@ public void extendFilter() { assertThat( values.get( 0 ).getExternalId() ).isEqualTo( 111 ); assertThat( values.get( 1 ).getExternalId() ).isEqualTo( 113 ); } + + @Test + public void testExtendFilterMap() { + List values = Dictionaries + .getDictionary( "test-dictionary-extends-filter-map" ) + .getValue( "id2" ) + .getValues(); + + assertThat( values ).hasSize( 2 ); + assertThat( values.get( 0 ).getId() ).isEqualTo( "id111" ); + assertThat( values.get( 1 ).getId() ).isEqualTo( "id22" ); + + assertThat( values.get( 0 ).getExternalId() ).isEqualTo( 111 ); + assertThat( values.get( 1 ).getExternalId() ).isEqualTo( 113 ); + } } diff --git a/oap-stdlib-test/src/test/resources/dictionary/test-dictionary-extends-filter-map.conf b/oap-stdlib-test/src/test/resources/dictionary/test-dictionary-extends-filter-map.conf new file mode 100644 index 000000000..906f31097 --- /dev/null +++ b/oap-stdlib-test/src/test/resources/dictionary/test-dictionary-extends-filter-map.conf @@ -0,0 +1,49 @@ +{ + name = test-dictionary-extends + version = 1 + values { + id1 { + title = title1 + enabled = true + eid = 1 + values { + id11 { + title = title11 + enabled = true + eid = 11 + values { + id111 { + title = title111 + eid = 111 + tags = [test] + } + id112 { + title = title112 + eid = 112 + } + } + } + id12 { + title = title12 + enabled = true + eid = 12 + } + } + } + id2 { + title = title2 + enabled = true + eid = 2 + values { + = [{ + path = /id1/id11 + filter = test + }] + id22 { + title = title21 + eid = 1 + } + } + } + } +} diff --git a/oap-stdlib/src/main/java/oap/dictionary/DictionaryParser.java b/oap-stdlib/src/main/java/oap/dictionary/DictionaryParser.java index 211ca648d..5f67a95cb 100644 --- a/oap-stdlib/src/main/java/oap/dictionary/DictionaryParser.java +++ b/oap-stdlib/src/main/java/oap/dictionary/DictionaryParser.java @@ -35,6 +35,7 @@ import oap.util.function.Try; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; +import org.jspecify.annotations.NonNull; import java.io.IOException; import java.io.UncheckedIOException; @@ -297,10 +298,29 @@ private static ArrayList parseValues( Map values, String path, ArrayList dv = new ArrayList<>(); values.forEach( ( key, value ) -> { - if( value instanceof Map map ) { - map.put( "id", key ); + if( "".equals( key ) ) { + if( value instanceof Map map ) { + Extends anExtends = getExtends( map ); + dv.add( new DictionaryExtends( anExtends ) ); + } else if( value instanceof List list ) { + for( Object v : list ) { + if( v instanceof Map map ) { + Extends anExtends = getExtends( map ); + dv.add( new DictionaryExtends( anExtends ) ); + } else { + throw new DictionaryError( " = [{path = ...[, filter = ...]}]" ); + } + } + } else { + throw new DictionaryError( " = list | map" ); + } + } else { + + if( value instanceof Map map ) { + map.put( "id", key ); + } + dv.add( parseAsDictionaryValue( value, path + "['" + key + "']", false, idStrategy ) ); } - dv.add( parseAsDictionaryValue( value, path + "['" + key + "']", false, idStrategy ) ); } ); return dv; @@ -310,11 +330,15 @@ private static Optional getExtendsOpt( Map map ) { Map m = getValueOpt( Map.class, map, "extends", _ -> Optional.empty() ).orElse( null ); if( m == null ) return Optional.empty(); + return Optional.of( getExtends( m ) ); + } + + private static @NonNull Extends getExtends( Map m ) { String path = getString( m, "path" ); Optional filter = getStringOpt( m, "filter" ); Boolean ignoreDuplicate = getBooleanOpt( m, "ignoreDuplicate" ).orElse( false ); - return Optional.of( new Extends( path, filter, ignoreDuplicate ) ); + return new Extends( path, filter, ignoreDuplicate ); } private static String getString( Map map, String field ) { diff --git a/pom.xml b/pom.xml index f495da37d..daa029348 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ - 25.4.9 + 25.4.10 25.0.1 25.0.0