forked from metaschema-framework/metaschema-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the Metapath function map:for-each.
- Loading branch information
1 parent
b2e2409
commit 9728d2d
Showing
5 changed files
with
206 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
.../src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/MapForEach.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* SPDX-FileCopyrightText: none | ||
* SPDX-License-Identifier: CC0-1.0 | ||
*/ | ||
|
||
package gov.nist.secauto.metaschema.core.metapath.function.library; | ||
|
||
import gov.nist.secauto.metaschema.core.metapath.DynamicContext; | ||
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; | ||
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; | ||
import gov.nist.secauto.metaschema.core.metapath.function.IArgument; | ||
import gov.nist.secauto.metaschema.core.metapath.function.IFunction; | ||
import gov.nist.secauto.metaschema.core.metapath.item.IItem; | ||
import gov.nist.secauto.metaschema.core.metapath.item.ISequence; | ||
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; | ||
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem; | ||
import gov.nist.secauto.metaschema.core.util.ObjectUtils; | ||
|
||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
/** | ||
* Implements the XPath 3.1 | ||
* <a href= "https://www.w3.org/TR/xpath-functions-31/#func-map-contains">map:contains</a> function. | ||
*/ | ||
public final class MapForEach { | ||
private static final String NAME = "for-each"; | ||
@NonNull | ||
static final IFunction SIGNATURE = IFunction.builder() | ||
.name(NAME) | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_MAP) | ||
.deterministic() | ||
.contextIndependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("map") | ||
.type(IMapItem.type()) | ||
.one() | ||
.build()) | ||
.argument(IArgument.builder() | ||
.name("action") | ||
.type(IFunction.type()) | ||
.one() | ||
.build()) | ||
.returnType(IItem.type()) | ||
.returnOne() | ||
.functionHandler(MapForEach::execute) | ||
.build(); | ||
|
||
private MapForEach() { | ||
// disable construction | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<?> execute(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
IMapItem<?> map = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true))); | ||
IFunction action = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); | ||
|
||
BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> lambda = adaptFunction(action, dynamicContext); | ||
return forEach(map, lambda); | ||
} | ||
|
||
/** | ||
* Adapts the provided function call to a lambda expression for use with | ||
* {@link #forEach(IMapItem, BiFunction)}. | ||
* | ||
* @param function | ||
* the function to adapt | ||
* @param dynamicContext | ||
* the dynamic context for use in evaluating the function | ||
* @return | ||
*/ | ||
@NonNull | ||
private static BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> adaptFunction( | ||
@NonNull IFunction function, | ||
@NonNull DynamicContext dynamicContext) { | ||
return (key, value) -> { | ||
List<ISequence<?>> arguments = ObjectUtils.notNull(List.of( | ||
ISequence.of(key), | ||
value.toSequence())); | ||
|
||
return function.execute(arguments, dynamicContext, ISequence.empty()); | ||
}; | ||
} | ||
|
||
/** | ||
* An implementation of XPath 3.1 | ||
* <a href= "https://www.w3.org/TR/xpath-functions-31/#func-map-for-each">map:for-each</a>. | ||
* | ||
* @param map | ||
* the map of Metapath items that is the target of retrieval | ||
* @param action | ||
* the action to perform on each entry in the map | ||
* @return a stream resulting from concatenating the resulting streams provided by the action result | ||
*/ | ||
@NonNull | ||
public static ISequence<?> forEach( | ||
@NonNull IMapItem<?> map, | ||
@NonNull BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> action) { | ||
return ISequence.of(ObjectUtils.notNull(map.entrySet().stream() | ||
.flatMap(entry -> { | ||
IAnyAtomicItem key = entry.getKey().getKey(); | ||
ISequence<?> values = entry.getValue().contentsAsSequence(); | ||
return action.apply(key, values).stream(); | ||
}))); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
.../test/java/gov/nist/secauto/metaschema/core/metapath/function/library/MapForEachTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* SPDX-FileCopyrightText: none | ||
* SPDX-License-Identifier: CC0-1.0 | ||
*/ | ||
|
||
package gov.nist.secauto.metaschema.core.metapath.function.library; | ||
|
||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.entry; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.map; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.sequence; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.string; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase; | ||
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression; | ||
import gov.nist.secauto.metaschema.core.metapath.item.IItem; | ||
import gov.nist.secauto.metaschema.core.metapath.item.ISequence; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
class MapForEachTest | ||
extends ExpressionTestBase { | ||
|
||
private static Stream<Arguments> provideValues() { // NOPMD - false positive | ||
return Stream.of( | ||
Arguments.of( | ||
sequence(integer(1), integer(2)), | ||
"map:for-each(map{1:'yes', 2:'no'}, function($k, $v){$k})"), | ||
Arguments.of( | ||
sequence(string("yes"), string("no")), | ||
"distinct-values(map:for-each(map{1:'yes', 2:'no'}, function($k, $v){$v}))"), | ||
Arguments.of( | ||
sequence(map(entry(string("a"), integer(2)), entry(string("b"), integer(3)))), | ||
"map:merge(map:for-each(map{'a':1, 'b':2}, function($k, $v){map:entry($k, $v+1)}))")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValues") | ||
void testExpression(@NonNull ISequence<IItem> expected, @NonNull String metapath) { | ||
|
||
ISequence<IItem> result = IMetapathExpression.compile(metapath).evaluate(null, newDynamicContext()); | ||
assertThat(result).containsAll(expected); | ||
} | ||
} |