Skip to content

Commit b005b79

Browse files
committed
Use FunctionalInterface for Python function.
1 parent 718c0c8 commit b005b79

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

graalpy/graalpy-micronaut-pygal-charts/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ To start the demo, simply run:
2020
./mvnw package mn:run
2121
```
2222

23-
When the demo runs, open the follwing URLs in a browser:
23+
When the demo runs, open the following URLs in a browser:
2424

2525
| URL | Service |
2626
|:------------------------------|:------------------------------|

graalpy/graalpy-micronaut-pygal-charts/src/main/java/com/example/PyGalServiceMixed.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
package com.example;
88

99
import jakarta.inject.Singleton;
10-
import org.graalvm.polyglot.Value;
1110

1211
import java.util.List;
1312
import java.util.stream.IntStream;
1413

1514
@Singleton
1615
public class PyGalServiceMixed implements PyGalService {
17-
private final Value pythonFunctionXY;
16+
private final RenderXYFunction renderXYFunction;
1817

1918
PyGalServiceMixed(GraalPyContext graalPyContext) {
20-
pythonFunctionXY = graalPyContext.eval(
19+
renderXYFunction = graalPyContext.eval(
2120
// language=python
2221
"""
2322
import pygal
@@ -29,12 +28,17 @@ def render_xy(title, label_datapoint_entries):
2928
xy_chart.add(entry.label(), entry.dataPoints())
3029
return xy_chart.render().decode()
3130
32-
render_xy""");
31+
render_xy""").as(RenderXYFunction.class);
3332
}
3433

3534
public record Entry(String label, double[][] dataPoints) {
3635
}
3736

37+
@FunctionalInterface
38+
public interface RenderXYFunction {
39+
String apply(String title, List<Entry> labelDatapointEntries);
40+
}
41+
3842
@Override
3943
public String renderXYChart() {
4044
String title = "XY Cosinus";
@@ -46,6 +50,6 @@ public String renderXYChart() {
4650
new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}),
4751
new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}})
4852
);
49-
return pythonFunctionXY.execute(title, labelDatapointEntries).asString();
53+
return renderXYFunction.apply(title, labelDatapointEntries);
5054
}
5155
}

graalpy/graalpy-micronaut-pygal-charts/src/main/resources/META-INF/native-image/com.example/demo/reachability-metadata.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
{
44
"type": "com.example.PyGalServiceMixed$Entry"
55
},
6+
{
7+
"type": "com.example.PyGalServiceMixed$RenderXYFunction"
8+
},
69
{
710
"type": "com.example.PyGalServicePureJava$BytesIO"
811
},
@@ -12,6 +15,13 @@
1215
{
1316
"type": "com.example.PyGalServicePureJava$XY"
1417
},
18+
{
19+
"type": {
20+
"proxy": [
21+
"com.example.PyGalServiceMixed$RenderXYFunction"
22+
]
23+
}
24+
},
1525
{
1626
"type": {
1727
"proxy": [

graalpy/graalpy-spring-boot-pygal-charts/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ To start the demo, simply run:
2020
./mvnw package spring-boot:run
2121
```
2222

23-
When the demo runs, open the follwing URLs in a browser:
23+
When the demo runs, open the following URLs in a browser:
2424

2525
| URL | Service |
2626
|:------------------------------|:------------------------------|

graalpy/graalpy-spring-boot-pygal-charts/src/main/java/com/example/demo/PyGalServiceMixed.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
package com.example.demo;
88

99
import com.example.demo.GraalPyContextConfiguration.GraalPyContext;
10-
import org.graalvm.polyglot.Value;
1110
import org.springframework.stereotype.Service;
1211

1312
import java.util.List;
1413
import java.util.stream.IntStream;
1514

1615
@Service
1716
public class PyGalServiceMixed implements PyGalService {
18-
private final Value pythonFunctionXY;
17+
private final RenderXYFunction renderXYFunction;
1918

2019
PyGalServiceMixed(GraalPyContext graalPyContext) {
21-
pythonFunctionXY = graalPyContext.eval(
20+
renderXYFunction = graalPyContext.eval(
2221
// language=python
2322
"""
2423
import pygal
@@ -30,12 +29,17 @@ def render_xy(title, label_datapoint_entries):
3029
xy_chart.add(entry.label(), entry.dataPoints())
3130
return xy_chart.render().decode()
3231
33-
render_xy""");
32+
render_xy""").as(RenderXYFunction.class);
3433
}
3534

3635
public record Entry(String label, double[][] dataPoints) {
3736
}
3837

38+
@FunctionalInterface
39+
public interface RenderXYFunction {
40+
String apply(String title, List<Entry> labelDatapointEntries);
41+
}
42+
3943
@Override
4044
public String renderXYChart() {
4145
String title = "XY Cosinus";
@@ -47,6 +51,6 @@ public String renderXYChart() {
4751
new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}),
4852
new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}})
4953
);
50-
return pythonFunctionXY.execute(title, labelDatapointEntries).asString();
54+
return renderXYFunction.apply(title, labelDatapointEntries);
5155
}
5256
}

graalpy/graalpy-spring-boot-pygal-charts/src/main/resources/META-INF/native-image/com.example/demo/reachability-metadata.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
{
44
"type": "com.example.demo.PyGalServiceMixed$Entry"
55
},
6+
{
7+
"type": "com.example.demo.PyGalServiceMixed$RenderXYFunction"
8+
},
69
{
710
"type": "com.example.demo.PyGalServicePureJava$BytesIO"
811
},
@@ -12,6 +15,13 @@
1215
{
1316
"type": "com.example.demo.PyGalServicePureJava$XY"
1417
},
18+
{
19+
"type": {
20+
"proxy": [
21+
"com.example.demo.PyGalServiceMixed$RenderXYFunction"
22+
]
23+
}
24+
},
1525
{
1626
"type": {
1727
"proxy": [

0 commit comments

Comments
 (0)