Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function description support to FunctionEntry #105

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 60 additions & 30 deletions pkg/functions/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -170,47 +188,54 @@ 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{
{Types: []JpType{JpString}},
{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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions pkg/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down