Record Class FunctionSignature
java.lang.Object
java.lang.Record
ca.marcusdunn.jsonlens.path.core.function.FunctionSignature
- Record Components:
name- the function nameresultType- the declared type of the resultparameterTypes- 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 Summary
FieldsModifier and TypeFieldDescriptionstatic final FunctionSignaturecount(NodesType): ValueType(RFC 9535, Section 2.4.5).static final FunctionSignaturelength(ValueType): ValueType(RFC 9535, Section 2.4.4).static final FunctionSignaturematch(ValueType, ValueType): LogicalType(RFC 9535, Section 2.4.6).static final FunctionSignaturesearch(ValueType, ValueType): LogicalType(RFC 9535, Section 2.4.7).static final List<FunctionSignature> The functions of the IANA "Function Extensions" subregistry (RFC 9535, Section 3.2).static final FunctionSignaturevalue(NodesType): ValueType(RFC 9535, Section 2.4.8). -
Constructor Summary
ConstructorsConstructorDescriptionFunctionSignature(String name, FunctionType resultType, List<FunctionType> parameterTypes) Makes a signature. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.static booleanisValidName(String name) Returns true if a name agrees with the grammar rulefunction-name.name()Returns the value of thenamerecord component.Returns the value of theparameterTypesrecord component.Returns the value of theresultTyperecord component.final StringtoString()Returns a string representation of this record class.
-
Field Details
-
LENGTH
length(ValueType): ValueType(RFC 9535, Section 2.4.4). -
COUNT
count(NodesType): ValueType(RFC 9535, Section 2.4.5). -
MATCH
match(ValueType, ValueType): LogicalType(RFC 9535, Section 2.4.6). -
SEARCH
search(ValueType, ValueType): LogicalType(RFC 9535, Section 2.4.7). -
VALUE
value(NodesType): ValueType(RFC 9535, Section 2.4.8). -
STANDARD
The functions of the IANA "Function Extensions" subregistry (RFC 9535, Section 3.2).
-
-
Constructor Details
-
FunctionSignature
Makes a signature.- Parameters:
name- the function nameresultType- the declared type of the resultparameterTypes- the declared types of the parameters, in order
-
-
Method Details
-
isValidName
Returns true if a name agrees with the grammar rulefunction-name.- Parameters:
name- a name- Returns:
- true for a lowercase letter followed by lowercase letters, digits, and "_"
-
toString
-
hashCode
-
equals
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 withObjects::equals(Object,Object). -
name
-
resultType
Returns the value of theresultTyperecord component.- Returns:
- the value of the
resultTyperecord component
-
parameterTypes
Returns the value of theparameterTypesrecord component.- Returns:
- the value of the
parameterTypesrecord component
-