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

add experimental module support #129

Merged
merged 2 commits into from
Sep 27, 2021
Merged

Conversation

eiiches
Copy link
Owner

@eiiches eiiches commented Sep 24, 2021

This PR adds an experimental Module support to jackson-jq.

To use this feature, users need to set ModuleLoader to the scope (by default all import fails).

rootScope.setModuleLoader(new ChainedModuleLoader(new ModuleLoader[] {
	BuiltinModuleLoader.getInstance(), // enable loading modules via ServiceLoader
	new FileSystemModuleLoader(rootScope, Versions.JQ_1_6,
		FileSystems.getDefault().getPath("").toAbsolutePath(), // enable loading modules from current directory,
		Paths.get(Scope.class.getClassLoader().getResource("classpath_modules").toURI())), // and from Java classpath
}));

Todo:

  • Implement FileSystemModuleLoader
    • We should also be able to use Java classpath instead of the actual file system
    • We must prevent directory traversals
    • Support altering search path in the import directive: import "foo" as foo {search: "./"}
    • Cache loaded modules
    • Forbid recursive imports
  • module directive
  • modulemeta/0
  • Add Map<String, Module> BuiltinModuleLoader#loadAllModules() method
    • Hopefully Quarkus extension can make use of this method to enumerate and serialize built-in Modules
  • NullModuleLoader should be in the internal package
  • Add tests

resolves #43

@eiiches eiiches force-pushed the feature/20210924-module-support branch from 6467d52 to 1d6103d Compare September 24, 2021 05:39
@eiiches
Copy link
Owner Author

eiiches commented Sep 24, 2021

It might be useful if Java classes (limited to ones that are allowed in configuration) can be directly imported into jq.

import "java.lang.Math" as jmath;

jmath::sqrt(3)

I'm not gonna implement this in this PR, but I believe this could be done by writing a ModuleLoader that reflectively generates Function instances from Java methods.

@eiiches eiiches force-pushed the feature/20210924-module-support branch 3 times, most recently from bb32c1c to d3a2b7e Compare September 27, 2021 03:49
@eiiches
Copy link
Owner Author

eiiches commented Sep 27, 2021

modulemeta/0 will not be implemented in this PR because I couldn't figure out what it does or how to use it.

$ cat foo.jq
module {"name": "foo"};
def one: 1;
$ jq-1.6 -n 'import "foo" as foo; foo::one, ("foo" | modulemeta)'
1
jq: error (at <unknown>): module not found: foo

@eiiches eiiches marked this pull request as ready for review September 27, 2021 06:39
@eiiches eiiches force-pushed the feature/20210924-module-support branch from 2bdbf2c to 552ce19 Compare September 27, 2021 13:03
@eiiches eiiches merged commit 0b384df into develop/1.x Sep 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Module support
1 participant