Skip to content

Commit

Permalink
BL-920 Resolve - support Lucee method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
jclausen committed Jan 11, 2025
1 parent a91b066 commit 30a8c0a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
*/
package ortus.boxlang.modules.compat.bifs.cache;

import java.util.Set;

import ortus.boxlang.modules.compat.util.KeyDictionary;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.cache.providers.ICacheProvider;
Expand All @@ -26,6 +25,7 @@
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.types.Array;
import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException;
import ortus.boxlang.runtime.validation.Validator;

@BoxBIF
Expand All @@ -40,7 +40,8 @@ public CacheGet() {
super();
declaredArguments = new Argument[] {
new Argument( true, Argument.ANY, Key.id ),
new Argument( false, Argument.STRING, Key.cacheName, Key._DEFAULT, Set.of( cacheExistsValidator ) )
new Argument( false, Argument.ANY, Key.cacheName, Key._DEFAULT ),
new Argument( false, Argument.ANY, KeyDictionary.throwWhenNotExist, false )
};
}

Expand All @@ -58,6 +59,20 @@ public CacheGet() {
* @return The value of the object in the cache or null if not found, or the default value if provided
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
if ( arguments.get( Key.cacheName ) instanceof Boolean ) {
// Handle lucees method signature with a boolean as the second arg
Key cacheName = Key._DEFAULT;
if ( arguments.get( KeyDictionary.throwWhenNotExist ) != null ) {
cacheName = Key.of( arguments.getAsString( KeyDictionary.throwWhenNotExist ) );
}
arguments.put( KeyDictionary.throwWhenNotExist, arguments.getAsBoolean( Key.cacheName ) );
arguments.put( Key.cacheName, cacheName );
}

if ( arguments.get( Key.cacheName ) instanceof String ) {
arguments.put( Key.cacheName, Key.of( arguments.getAsString( Key.cacheName ) ) );
}

// Get the requested cache
ICacheProvider cache = cacheService.getCache( arguments.getAsKey( Key.cacheName ) );

Expand All @@ -68,9 +83,16 @@ public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
}

// Get the value
Attempt<Object> results = cache.get( arguments.getAsString( Key.id ) );
Attempt<Object> results = cache.get( arguments.getAsString( Key.id ) );
// If we have a value return it, else do we have a defaultValue, else return null
return results.orElse( null );
Object item = results.orElse( null );
if ( item == null && arguments.getAsBoolean( KeyDictionary.throwWhenNotExist ) ) {
throw new BoxRuntimeException(
"Item not found in cache [" + arguments.getAsKey( Key.cacheName ).getName() + "]: " + arguments.getAsString( Key.id )
);
} else {
return item;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class KeyDictionary {
public static final Key client = Key.of( "client" );
public static final Key clientStorage = Key.of( "clientStorage" );
public static final Key clientTimeout = Key.of( "clientTimeout" );
public static final Key throwWhenNotExist = Key.of( "throwWhenNotExist" );
public static final Key ON_CLIENT_CREATED = Key.of( "onClientCreated" );
public static final Key ON_CLIENT_DESTROYED = Key.of( "onClientDestroyed" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package ortus.boxlang.modules.compat.bifs.cache;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException;

public class CacheGetTest extends BaseCacheTest {

@Test
Expand Down Expand Up @@ -60,4 +63,29 @@ public void canGetNullFromInvalidCacheKey() {
assertThat( variables.get( "result" ) ).isNull();
}

@Test
@DisplayName( "Can retrieve a cache object using Lucees method signature" )
public void canGetWithLuceeSignature() {
runtime.executeSource(
"""
result = cacheGet( "tdd", false, "default" );
""",
context );

assertThat( variables.get( "result" ) ).isEqualTo( "rocks" );
}

@Test
@DisplayName( "Can retrieve a cache object using Lucees method signature with throwWhenNotExist" )
public void canGetWithLuceeSignatureAndThrowWhenNotExist() {
assertThrows(
BoxRuntimeException.class,
() -> runtime.executeSource(
"""
result = cacheGet( "invalid", true, "default" );
""",
context )
);
}

}

0 comments on commit 30a8c0a

Please sign in to comment.