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

Scripting: Converters can adapt return values #60937

Merged

Conversation

stu-elastic
Copy link
Contributor

@stu-elastic stu-elastic commented Aug 10, 2020

Add ability to explicitly coerce the return value from a script.

Runtime fields wants to avoid returning Object from the execute method
in each context. Instead, they will return an array of primitives, such as
long[].

However, it's convenient to allow a user to return a single primitive
type rather than allocating a length-one array.

To achieve this, an implementer can add explicit conversion functions to
a context with the signature:
public static <context-return-value> convertFrom<Suffix>(<any type>)

When a user returns a type other than the context return value, at
compile-time, painless will insert a call to their convertFrom method.

This commit is Phase 1 of this work. It handles explicit converters for
all painless types EXCEPT def type.

Refs: #59647

Add ability to explicitly coerce the return value from a script.

Runtime fields want to avoid returning `Object` from the execute method
in each context.  Instead, they will return an array of primitive
objects, such as `long[]`.

However, it's convenient to allow a user to return a single primitive
type rather than allocating a length-one array.

To achieve this, an implementer can add explicit conversion functions to
a context with signature:
`public static <context-return-value> convertFrom<Suffix>(<any type>)`

When a user returns a type other than the context return value, at
compile-time, painless will insert a call to their `convertFrom` method.

This commit is Phase 1 of this work.  It handles explicit converters for
all painless types EXCEPT def type.

Refs: elastic#59647
@stu-elastic stu-elastic added >enhancement :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache labels Aug 10, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (:Core/Infra/Scripting)

@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Aug 10, 2020
Copy link
Contributor

@jdconrad jdconrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid first step! Just as a note, when we address primitives I think it's better to do this as a standard cast from the type to the parameter of the conversion method and then a second step where the method is actually called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement Team:Core/Infra Meta label for core/infra team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants