adobe / json-formula

Query language for JSON documents
http://opensource.adobe.com/json-formula/
Apache License 2.0
19 stars 8 forks source link

Example of how to add a custom function with signature using constants #184

Open VentuzVictorPerez opened 1 month ago

VentuzVictorPerez commented 1 month ago

Hi there,

I need to add a custom function to the JSONFormula instance. While I have been able to define and add the function, I'm having trouble specifying the data types for the function signature using the constants such as "TYPE_STRING" as I am unable to import it.

Expected Behaviour

"dataTypes" must be imported in some way.

Actual Behaviour

I am not able to import dataTypes constants.

Sample Code that illustrates the problem

I installed the package using NPM:

npm install @adobe/json-formula

import JsonFormula, { dataTypes }  from "@adobe/json-formula"; //  "import JsonFormula, { TYPE_STRING }  from "@adobe/json-formula";" also does not work.

function test () {

    const customFunctions = {
        myFunc: {
          _func: resolvedArgs => console.log(resolvedArgs[0]),
          _signature: [{ types: [dataTypes.TYPE_STRING] }] // <= "dataTypes" is not imported resulting in "undefined", using a '2' works though.
        }
      };

    const instance = new JsonFormula(customFunctions, 'null', []);
}

Could you please provide an example of how to add custom signatured functions?

Thanks!

JohnBrinkman commented 1 month ago

You're right, we're missing an export. I will add it to the next release. Until then, you'll need to redefine them in your project:

const dataTypes = {
  TYPE_NUMBER: 0,
  TYPE_ANY: 1,
  TYPE_STRING: 2,
  TYPE_ARRAY: 3,
  TYPE_OBJECT: 4,
  TYPE_BOOLEAN: 5,
  TYPE_EXPREF: 6,
  TYPE_NULL: 7,
  TYPE_ARRAY_NUMBER: 8,
  TYPE_ARRAY_STRING: 9,
  TYPE_ARRAY_ARRAY: 10,
  TYPE_EMPTY_ARRAY: 11,
};