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

Java in-memory idiomatic wrapper #157

Open
jshook opened this issue Jan 26, 2023 · 13 comments
Open

Java in-memory idiomatic wrapper #157

jshook opened this issue Jan 26, 2023 · 13 comments

Comments

@jshook
Copy link

jshook commented Jan 26, 2023

I've been using the java interop pattern. However, I need to be able to process a text image that may or may not be in a file. I do understand that the Path abstraction roots the usage pattern into the filesystem view, which is very appropriate for most cases.
However, it would be much better for my use case to be able to access the Interpreter core via a simple and idiomatic Java wrapper that doesn't indirect through the argv pattern. It seems that this would be useful in other places.

@lihaoyi-databricks
Copy link
Contributor

Folding in #157, whatever idiomatic Java API emerges should also include a way to pass additional functions to std

@sandys
Copy link

sandys commented Jul 3, 2023

@lihaoyi-databricks are we able to use this functionality today? given #166 is merged. we are using sjsonnet functionality in a java library and are trying to call java functions during jsonnet execution.

will live with a hacky way today and then use ur standardized java api when it is ready

@sandys
Copy link

sandys commented Jul 17, 2023

@lihaoyi-databricks we tried subclassing std and adding a new function in it. the issue is that the new function is not being picked up (when we pass the new std to main0). Would appreciate any help in how to do this.

@lihaoyi-databricks
Copy link
Contributor

@sandys it looks like std has a val functions: Map[String, Val.Func] that lists out all the functions. Did you add the newly defined function to that list?

@sandys
Copy link

sandys commented Jul 17, 2023

@lihaoyi-databricks thanks for replying - appreciate it!

we are not able to add our function to it - we tried called "updated" on it, etc but didnt work. so we switched to trying to add a function to std().

would you be able to help ? even a trivial java side example would be super useful. P.S. we are already using the 0.4.5 RC

@lihaoyi-databricks
Copy link
Contributor

You need to do something like override val functions = super.functions.updated(...) I think

I haven't actually tested this yet, so try it out and let me know if it doesn't work

@sandys
Copy link

sandys commented Jul 17, 2023

@lihaoyi-databricks im actually using sjsonnet from java code. i think your code is scala..but i tried variations of this using java and it doesnt work :(

@lihaoyi-databricks
Copy link
Contributor

Can you define a small Scala module to do this and then call it from Java? If not, you'll have to wait until an implementation of this ticket is available

@sandys
Copy link

sandys commented Jul 17, 2023

we have a whole bunch of java functions that we want to use from within jsonnet.
im happy to migrate to your solution when it is available, but is there even a hacky way to use it today ? im happy to shuffle a lot of code on my java side to make it happen temporarily.

@lihaoyi-databricks
Copy link
Contributor

If you're happy with hacky and this is urgent for you, you can fork this repo and add your std.foo functions in the Scala code that can call your Java functions, then publish your own artifacts to maven central or your own assembly jar that you can use within your project.

@sandys
Copy link

sandys commented Jul 26, 2023

xtrasonnet is a fork of sjsonnet that adds support for adding additional functions.

https://github.com/jam01/xtrasonnet/blob/0.5.4/xtrasonnet/src/test/java/io/github/jam01/xtrasonnet/spi/LibraryTest.java

@aelfric
Copy link

aelfric commented Mar 27, 2024

You need to do something like override val functions = super.functions.updated(...) I think

I haven't actually tested this yet, so try it out and let me know if it doesn't work

I was looking for how to define custom functions in Scala.

Even in Scala this doesn't seem to work. With Scala 2.13, there is an error "super may not be used on value functions". I think maybe if we want to be able to override the functions map, it needs to be a lazy val.

@aelfric
Copy link

aelfric commented Mar 28, 2024

It seems the solution in Scala, at least, is to use an early initializer.

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

No branches or pull requests

4 participants