From 7ab336695e8a95c2d1be3ae77733ecdb307267ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Mon, 16 Sep 2024 12:00:53 +0200 Subject: [PATCH] feat: add function description support to FunctionEntry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/functions/default.go | 90 +++++++++++++++++++++++++------------- pkg/functions/functions.go | 7 +-- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/pkg/functions/default.go b/pkg/functions/default.go index 8b942db..8807945 100644 --- a/pkg/functions/default.go +++ b/pkg/functions/default.go @@ -6,33 +6,38 @@ func GetDefaultFunctions() []FunctionEntry { Arguments: []ArgSpec{ {Types: []JpType{JpNumber}}, }, - Handler: jpfAbs, + Handler: jpfAbs, + Description: "Returns the absolute value of the provided argument.", }, { Name: "avg", Arguments: []ArgSpec{ {Types: []JpType{JpArrayNumber}}, }, - Handler: jpfAvg, + Handler: jpfAvg, + Description: "Returns the average of the elements in the provided array. An empty array will produce a return value of null.", }, { Name: "ceil", Arguments: []ArgSpec{ {Types: []JpType{JpNumber}}, }, - Handler: jpfCeil, + Handler: jpfCeil, + Description: "Returns the next highest integer value by rounding up if necessary.", }, { Name: "contains", Arguments: []ArgSpec{ {Types: []JpType{JpArray, JpString}}, {Types: []JpType{JpAny}}, }, - Handler: jpfContains, + Handler: jpfContains, + Description: "Returns `true` if the given subject contains the provided search value. If the subject is an array, this function returns `true` if one of the elements in the array is equal to the provided search value. If the provided subject is a string, this function returns `true` if the string contains the provided search argument.", }, { Name: "ends_with", Arguments: []ArgSpec{ {Types: []JpType{JpString}}, {Types: []JpType{JpString}}, }, - Handler: jpfEndsWith, + Handler: jpfEndsWith, + Description: "Reports whether the given string ends with the provided suffix argument.", }, { Name: "find_first", Arguments: []ArgSpec{ @@ -56,7 +61,8 @@ func GetDefaultFunctions() []FunctionEntry { Arguments: []ArgSpec{ {Types: []JpType{JpNumber}}, }, - Handler: jpfFloor, + Handler: jpfFloor, + Description: "Returns the next lowest integer value by rounding down if necessary.", }, { Name: "from_items", Arguments: []ArgSpec{ @@ -75,77 +81,89 @@ func GetDefaultFunctions() []FunctionEntry { Arguments: []ArgSpec{ {Types: []JpType{JpObject}}, }, - Handler: jpfItems, + Handler: jpfItems, + Description: "Converts a given object into an array of key-value pairs.", }, { Name: "join", Arguments: []ArgSpec{ {Types: []JpType{JpString}}, {Types: []JpType{JpArrayString}}, }, - Handler: jpfJoin, + Handler: jpfJoin, + Description: "Returns all of the elements from the provided array joined together using the glue argument as a separator between each.", }, { Name: "keys", Arguments: []ArgSpec{ {Types: []JpType{JpObject}}, }, - Handler: jpfKeys, + Handler: jpfKeys, + Description: "Returns an array containing the keys of the provided object.", }, { Name: "length", Arguments: []ArgSpec{ {Types: []JpType{JpString, JpArray, JpObject}}, }, - Handler: jpfLength, + Handler: jpfLength, + Description: "Returns the length of the given argument. If the argument is a string this function returns the number of code points in the string. If the argument is an array this function returns the number of elements in the array. If the argument is an object this function returns the number of key-value pairs in the object.", }, { Name: "lower", Arguments: []ArgSpec{ {Types: []JpType{JpString}}, }, - Handler: jpfLower, + Handler: jpfLower, + Description: "Returns the given string with all Unicode letters mapped to their lower case.", }, { Name: "map", Arguments: []ArgSpec{ {Types: []JpType{JpExpref}}, {Types: []JpType{JpArray}}, }, - Handler: jpfMap, + Handler: jpfMap, + Description: "Transforms elements in a given array and returns the result.", }, { Name: "max", Arguments: []ArgSpec{ {Types: []JpType{JpArrayNumber, JpArrayString}}, }, - Handler: jpfMax, + Handler: jpfMax, + Description: "Returns the highest found element in the provided array argument. An empty array will produce a return value of null.", }, { Name: "max_by", Arguments: []ArgSpec{ {Types: []JpType{JpArray}}, {Types: []JpType{JpExpref}}, }, - Handler: jpfMaxBy, + Handler: jpfMaxBy, + Description: "Returns the highest found element using a custom expression to compute the associated value for each element in the input array.", }, { Name: "merge", Arguments: []ArgSpec{ {Types: []JpType{JpObject}, Variadic: true}, }, - Handler: jpfMerge, + Handler: jpfMerge, + Description: "Meges a list of objects together and returns the result.", }, { Name: "min", Arguments: []ArgSpec{ {Types: []JpType{JpArrayNumber, JpArrayString}}, }, - Handler: jpfMin, + Handler: jpfMin, + Description: "Returns the lowest found element in the provided array argument.", }, { Name: "min_by", Arguments: []ArgSpec{ {Types: []JpType{JpArray}}, {Types: []JpType{JpExpref}}, }, - Handler: jpfMinBy, + Handler: jpfMinBy, + Description: "Returns the lowest found element using a custom expression to compute the associated value for each element in the input array.", }, { Name: "not_null", Arguments: []ArgSpec{ {Types: []JpType{JpAny}, Variadic: true}, }, - Handler: jpfNotNull, + Handler: jpfNotNull, + Description: "Returns the first non null element in the input array.", }, { Name: "pad_left", Arguments: []ArgSpec{ @@ -170,26 +188,30 @@ func GetDefaultFunctions() []FunctionEntry { {Types: []JpType{JpString}}, {Types: []JpType{JpNumber}, Optional: true}, }, - Handler: jpfReplace, + Handler: jpfReplace, + Description: "Returns a copy of the input string with instances of old string argument replaced by new string argument.", }, { Name: "reverse", Arguments: []ArgSpec{ {Types: []JpType{JpArray, JpString}}, }, - Handler: jpfReverse, + Handler: jpfReverse, + Description: "Reverses the input string or array and returns the result.", }, { Name: "sort", Arguments: []ArgSpec{ {Types: []JpType{JpArrayString, JpArrayNumber}}, }, - Handler: jpfSort, + Handler: jpfSort, + Description: "This function accepts an array argument and returns the sorted elements as an array.", }, { Name: "sort_by", Arguments: []ArgSpec{ {Types: []JpType{JpArray}}, {Types: []JpType{JpExpref}}, }, - Handler: jpfSortBy, + Handler: jpfSortBy, + Description: "This function accepts an array argument and returns the sorted elements as an array using a custom expression to compute the associated value for each element.", }, { Name: "split", Arguments: []ArgSpec{ @@ -197,20 +219,23 @@ func GetDefaultFunctions() []FunctionEntry { {Types: []JpType{JpString}}, {Types: []JpType{JpNumber}, Optional: true}, }, - Handler: jpfSplit, + Handler: jpfSplit, + Description: "Slices input string into substrings separated by a string argument and returns an array of the substrings between those separators.", }, { Name: "starts_with", Arguments: []ArgSpec{ {Types: []JpType{JpString}}, {Types: []JpType{JpString}}, }, - Handler: jpfStartsWith, + Handler: jpfStartsWith, + Description: "Reports whether the input string begins with the provided string prefix argument.", }, { Name: "sum", Arguments: []ArgSpec{ {Types: []JpType{JpArrayNumber}}, }, - Handler: jpfSum, + Handler: jpfSum, + Description: "Returns the sum of all numbers contained in the provided array.", }, { Name: "to_array", Arguments: []ArgSpec{ @@ -222,13 +247,15 @@ func GetDefaultFunctions() []FunctionEntry { Arguments: []ArgSpec{ {Types: []JpType{JpAny}}, }, - Handler: jpfToNumber, + Handler: jpfToNumber, + Description: "Returns the parsed number.", }, { Name: "to_string", Arguments: []ArgSpec{ {Types: []JpType{JpAny}}, }, - Handler: jpfToString, + Handler: jpfToString, + Description: "The JSON encoded value of the given argument.", }, { Name: "trim", Arguments: []ArgSpec{ @@ -255,19 +282,22 @@ func GetDefaultFunctions() []FunctionEntry { Arguments: []ArgSpec{ {Types: []JpType{JpAny}}, }, - Handler: jpfType, + Handler: jpfType, + Description: "Returns the JavaScript type of the given argument as a string value.", }, { Name: "upper", Arguments: []ArgSpec{ {Types: []JpType{JpString}}, }, - Handler: jpfUpper, + Handler: jpfUpper, + Description: "Returns the given string with all Unicode letters mapped to their upper case.", }, { Name: "values", Arguments: []ArgSpec{ {Types: []JpType{JpObject}}, }, - Handler: jpfValues, + Handler: jpfValues, + Description: "Returns the values of the provided object.", }, { Name: "zip", Arguments: []ArgSpec{ diff --git a/pkg/functions/functions.go b/pkg/functions/functions.go index 84364d3..94424a5 100644 --- a/pkg/functions/functions.go +++ b/pkg/functions/functions.go @@ -35,9 +35,10 @@ const ( ) type FunctionEntry struct { - Name string - Arguments []ArgSpec - Handler JpFunction + Name string + Arguments []ArgSpec + Handler JpFunction + Description string } type ArgSpec struct {