Skip to content

Commit

Permalink
BL-951 updates - better handling for deeply nested values
Browse files Browse the repository at this point in the history
  • Loading branch information
jclausen committed Jan 18, 2025
1 parent 67a3adf commit 279a55a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/main/java/ortus/boxlang/runtime/types/util/StructUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,21 @@ public static Stream<IStruct> findKey( IStruct struct, String key ) {
returnStruct.put(
Key.owner,
keyParts.length > 1
? unFlattenKeys( flatMap, true, false ).get( Key.of( flatMapParent ) )
? unFlattenKeys(
flatMap.entrySet().stream()
.filter( mapEntry -> mapEntry.getKey().getName().length() >= flatMapParent.length() && mapEntry.getKey().getName().substring( 0, flatMapParent.length() ).equals( flatMapParent ) )
.map( mapEntry -> {
String keyname = mapEntry.getKey().getName();
String resultKeyName = keyname.substring( flatMapParent.length() + 1 );
return new AbstractMap.SimpleEntry<Key, Object>(
Key.of( resultKeyName ), mapEntry.getValue()
);
}
)
.collect( BLCollector.toStruct() ),
true,
false
)
: struct
);
returnStruct.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,26 @@ public void testOwnerValues() {
myStruct = {
horse: nullValue(),
bird: {
total: nullValue()
total: nullValue(),
species : {
parrot: {
size : "large",
total: 1,
names : [
"Polly",
"Jack",
"Fred"
]
},
finch: {
size : "small",
total: 2
},
duck: {
size : "large",
total: 3
},
}
},
cow: {
total: 12
Expand All @@ -220,6 +239,10 @@ public void testOwnerValues() {
result = StructFindKey( myStruct, "pig.total" );
resultTop = StructFindKey( myStruct, "cat" );
resultOwner = resultTop.first().owner;
resultNested = structFindKey( myStruct, "size", "all" );
nestedOwner = resultNested.first().owner;
resultParrotNames = structFindKey( myStruct, "bird.species.parrot.names", "all" );
parrotResult = resultParrotNames.first();
""",
context );
//@formatter:on
Expand All @@ -228,6 +251,14 @@ public void testOwnerValues() {
assertEquals( 1, variables.getAsArray( Key.of( "resultTop" ) ).size() );
assertEquals( Struct.class, variables.get( Key.of( "resultOwner" ) ).getClass() );
assertEquals( 3, StructCaster.cast( variables.get( Key.of( "resultOwner" ) ) ).getAsInteger( Key.of( "total" ) ) );
assertEquals( Array.class, variables.get( Key.of( "resultNested" ) ).getClass() );
assertEquals( 3, variables.getAsArray( Key.of( "resultNested" ) ).size() );
assertEquals( Struct.class, variables.get( Key.of( "nestedOwner" ) ).getClass() );
assertTrue( StructCaster.cast( variables.get( Key.of( "nestedOwner" ) ) ).containsKey( Key.of( "size" ) ) );
assertTrue( StructCaster.cast( variables.get( Key.of( "nestedOwner" ) ) ).containsKey( Key.of( "total" ) ) );
assertEquals( Array.class, variables.get( Key.of( "resultParrotNames" ) ).getClass() );
assertEquals( 1, variables.getAsArray( Key.of( "resultParrotNames" ) ).size() );
assertEquals( Array.class, variables.getAsStruct( Key.of( "parrotResult" ) ).get( Key.value ).getClass() );
}

}

0 comments on commit 279a55a

Please sign in to comment.