Skip to content

Commit

Permalink
Add function and argument meta
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Sep 13, 2023
1 parent 3f19d40 commit 3a4243f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
60 changes: 54 additions & 6 deletions runtime/src/main/java/ortus/boxlang/runtime/types/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package ortus.boxlang.runtime.types;

import java.util.HashMap;
import java.util.Map;

import ortus.boxlang.runtime.context.FunctionBoxContext;
Expand All @@ -37,24 +38,38 @@ public enum Access {
REMOTE
}

public static Key ARGUMENT_COLLECTION = Key.of( "argumentCollection" );
public static Key ARGUMENT_COLLECTION = Key.of( "argumentCollection" );

/**
* The arguments of the function
*/
private Argument[] arguments;
private Argument[] arguments;

/**
* The name of the function
*/
private Key name;
private Key name;

/**
* Additional abitrary metadata about this function.
*/
private HashMap<Key, Object> metadata;

/**
* Constructor
*/
public Function( Key name, Argument[] arguments ) {
public Function( Key name, Argument[] arguments, HashMap<Key, Object> metadata ) {
this.name = name;
this.arguments = arguments;
this.metadata = metadata;
}

public Function( Key name, Argument[] arguments ) {
this( name, arguments, new HashMap<Key, Object>() );
}

public Function( Key name ) {
this( name, new Argument[] {}, new HashMap<Key, Object>() );
}

public Argument[] getArguments() {
Expand All @@ -65,6 +80,10 @@ public Key getName() {
return name;
}

public HashMap<Key, Object> getMetadata() {
return metadata;
}

public abstract Object invoke( FunctionBoxContext context );

public ArgumentsScope createArgumentsScope( Object[] positionalArguments ) {
Expand Down Expand Up @@ -145,8 +164,37 @@ public String asString() {
* @param defaultValue The default value of the argument
*
*/
public record Argument( boolean required, String type, Key name, Object defaultValue, String hint ) {
// The record automatically generates the constructor, getters, equals, hashCode, and toString methods.
public record Argument( boolean required, String type, Key name, Object defaultValue, String hint, HashMap<Key, Object> metadata ) {

public Argument( Key name ) {
this( false, "any", name, null, "", new HashMap<Key, Object>() );
}

public Argument( boolean required, String type, Key name ) {
this( required, type, name, null, "", new HashMap<Key, Object>() );
}

public Argument( boolean required, String type, Key name, Object defaultValue ) {
this( required, type, name, defaultValue, "", new HashMap<Key, Object>() );
}

public Argument( boolean required, String type, Key name, Object defaultValue, String hint ) {
this( required, type, name, defaultValue, hint, new HashMap<Key, Object>() );
}

public Argument( boolean required, String type, Key name, Object defaultValue, HashMap<Key, Object> metadata ) {
this( required, type, name, defaultValue, "", metadata );
}

public Argument( boolean required, String type, Key name, Object defaultValue, String hint, HashMap<Key, Object> metadata ) {
this.required = required;
this.type = type;
this.name = name;
this.defaultValue = defaultValue;
this.hint = hint;
this.metadata = metadata;
}

}

protected Object ensureArgumentType( Key name, Object value, String type ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void testCanDefaultArgs() {
Argument[] args = new Argument[] {
new Function.Argument( true, "String", firstName, "brad", "First Name" ),
new Function.Argument( true, "String", lastName, "wood", "Last Name" ),
new Function.Argument( false, "String", age, 43, null )
new Function.Argument( false, "String", age, 43 )
};
Closure Closure = new SampleClosure( args, new ScriptingBoxContext(), "Brad" );
IScope argscope = Closure.createArgumentsScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void testCanDefaultArgs() {
Argument[] args = new Argument[] {
new Function.Argument( true, "String", firstName, "brad", "First Name" ),
new Function.Argument( true, "String", lastName, "wood", "Last Name" ),
new Function.Argument( false, "String", age, 43, null )
new Function.Argument( false, "String", age, 43 )
};
UDF udf = new SampleUDF( UDF.Access.PUBLIC, Key.of( "foo" ), "any", args, "Cool function", false, null );
IScope argscope = udf.createArgumentsScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void testCanDefaultArgs() {
Argument[] args = new Argument[] {
new Function.Argument( true, "String", firstName, "brad", "First Name" ),
new Function.Argument( true, "String", lastName, "wood", "Last Name" ),
new Function.Argument( false, "String", age, 43, null )
new Function.Argument( false, "String", age, 43 )
};
Lambda Lambda = new SampleLambda( args, "Brad" );
IScope argscope = Lambda.createArgumentsScope();
Expand Down

0 comments on commit 3a4243f

Please sign in to comment.