Record Class FunctionSignature

java.lang.Object
java.lang.Record
ca.marcusdunn.jsonlens.path.core.function.FunctionSignature
Record Components:
name - the function name
resultType - the declared type of the result
parameterTypes - the declared types of the parameters, in order

public record FunctionSignature(String name, FunctionType resultType, List<FunctionType> parameterTypes) extends Record

The name and the declared types of a function extension (RFC 9535, Section 2.4).

The parser uses the signatures to check that a query is well-typed. For example, a function with the result type FunctionType.LOGICAL cannot be a side of a comparison.

Standard functions

Constant Signature Result
LENGTH length(ValueType): ValueType the length of a string, an array, or an object
COUNT count(NodesType): ValueType the number of nodes in a nodelist
MATCH match(ValueType, ValueType): LogicalType true if the complete string matches a regular expression
SEARCH search(ValueType, ValueType): LogicalType true if a substring matches a regular expression
VALUE value(NodesType): ValueType the value of a nodelist with one node

Function extensions

Declare a signature, and give it to the parser. The evaluator needs an implementation with the same signature:

// first(NodesType): ValueType. The first node of a nodelist, or Nothing.
// A read-only extension: it runs with each JsonModel, also with a read-only model.
static final class First implements FunctionExtension {

    static final FunctionSignature SIGNATURE =
            new FunctionSignature("first", FunctionType.VALUE, List.of(FunctionType.NODES));

    @Override
    public FunctionSignature signature() {
        return SIGNATURE;
    }

    @Override
    public <N> Instance<N> apply(List<Instance<N>> arguments, JsonModel<N> model) {
        // The evaluator gives one instance for each parameter, of the declared type.
        List<Node<N>> nodes = ((Instance.NodesInstance<N>) arguments.getFirst()).nodes();
        // A ValueType result is an existing node, or Nothing.
        return new Instance.ValueInstance<>(nodes.isEmpty() ? Maybe.none() : Maybe.some(nodes.getFirst().value()));
    }
}

A valid name starts with a lowercase letter, followed by lowercase letters, digits, and "_". The parser checks the name when it registers the signature. A function extension cannot have the name of a standard function:

Result<JsonPathParser, FunctionRegistrationError> clash = JsonPathParser.withFunctions(List.of(
        new FunctionSignature("length", FunctionType.VALUE, List.of(FunctionType.VALUE))));
// clash: Err(DuplicateName[name=length]). A standard function cannot be replaced.
  • Field Details

    • LENGTH

      public static final FunctionSignature LENGTH
      length(ValueType): ValueType (RFC 9535, Section 2.4.4).
    • COUNT

      public static final FunctionSignature COUNT
      count(NodesType): ValueType (RFC 9535, Section 2.4.5).
    • MATCH

      public static final FunctionSignature MATCH
      match(ValueType, ValueType): LogicalType (RFC 9535, Section 2.4.6).
    • VALUE

      public static final FunctionSignature VALUE
      value(NodesType): ValueType (RFC 9535, Section 2.4.8).
    • STANDARD

      public static final List<FunctionSignature> STANDARD
      The functions of the IANA "Function Extensions" subregistry (RFC 9535, Section 3.2).
  • Constructor Details

    • FunctionSignature

      public FunctionSignature(String name, FunctionType resultType, List<FunctionType> parameterTypes)
      Makes a signature.
      Parameters:
      name - the function name
      resultType - the declared type of the result
      parameterTypes - the declared types of the parameters, in order
  • Method Details

    • isValidName

      public static boolean isValidName(String name)
      Returns true if a name agrees with the grammar rule function-name.
      Parameters:
      name - a name
      Returns:
      true for a lowercase letter followed by lowercase letters, digits, and "_"
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • resultType

      public FunctionType resultType()
      Returns the value of the resultType record component.
      Returns:
      the value of the resultType record component
    • parameterTypes

      public List<FunctionType> parameterTypes()
      Returns the value of the parameterTypes record component.
      Returns:
      the value of the parameterTypes record component