Skip to content

Custom Functions

John Kouraklis edited this page Feb 18, 2019 · 3 revisions

Introduction

Matchers in model files can resolve functions. There are built-in functions but you can expand functionality by adding your own custom functions to suit your needs.

Location of the file

Create a .pas file and store it in \SourceCode\Common\Functions folder

Writing the Function

The file should define a function with the following signature:

function newFunc (const aArgs: array of string): Boolean;

where:

  • newFunc: the name of the function
  • aArgs: the arguments that need to be passed. They are defined as dynamic array
  • result: the function should return a true/false value to indicate whether the "new functionality" is resolved successfully or not

Registering the function

Once the function is created, you need to let Casbin4D know the file exists and, consequently, to load it. Uncomment (or add) and include directive {$I} in this location in Casbin.Functions.pas

Then, you have to register it in the internal dictionary in and in particularly in loadCustomFunctions as follows:

  procedure TFunctions.loadCustomFunctions;
  begin
    ...
    fDictionary.Add('FuncName', newFunc);
    ...
  end;

Testing the function

Make sure you write tests for your new function. If you want to share it with others, you should add Tests in the Test files

Use the function

Now you can use the function in a model file by referring to it using the FuncName from loadCustomFunctions