Skip to content

Commit

Permalink
Merge pull request #136 from jamezp/blog-fix
Browse files Browse the repository at this point in the history
Fix the gRPC array blog post.
  • Loading branch information
jamezp committed Jan 24, 2024
2 parents 47caaf6 + 79b5344 commit 9ea6267
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions _posts/2024-01-23-grpc-jakarta-rs-arrays.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
layout: post
title: "resteasy-grpc: Handling arrays"
subtitle: ""
date: 2024-01-23
author: Ron Sigal
layout: post
title: "resteasy-grpc: Handling arrays"
subtitle: ""
date: 2024-01-23
author: Ron Sigal
---

Release 1.0.0.Alpha5 of resteasy-grpc has a new feature for handling arbitrary arrays. Although protobuf comes
Expand Down Expand Up @@ -56,6 +56,7 @@ arrays.proto looks like this:
}
```

It starts with a definition of array message types for

1. all primitive types,
Expand All @@ -68,6 +69,7 @@ hold any of these array message types. The self-referential field
```
dev_resteasy_grpc_arrays___ArrayHolderArray arrayHolderArray_field = 13;
```

is what allows a `dev_resteasy_grpc_arrays___ArrayHolder` to represent arrays with any number of
dimension.

Expand Down Expand Up @@ -96,35 +98,50 @@ The output would be
int_field: 2
}
```
A similar, but rather longer, sequence would be required to build a javabuf representation of `int[][] {{3, 5}, {7, 11, 13}}`.
To avoid the mess, grpc-bridge-runtime includes the class `dev.resteasy.grpc.arrays.ArrayUtility`. With `ArrayUtility`,

{% raw %}
A similar, but rather longer, sequence would be required to build a javabuf representation
of `int[][] {{3, 5}, {7, 11, 13}}`.
To avoid the mess, grpc-bridge-runtime includes the class `dev.resteasy.grpc.arrays.ArrayUtility`. With `ArrayUtility`,
building the javabuf representation of `int[][] {{3, 5}, {7, 11, 13}}` is as easy as

```
dev_resteasy_grpc_arrays___ArrayHolder holder = ArrayUtility.getHolder(new int[][] {{3, 5}, {7, 11, 13}});
```

Moreover, `ArrayUtility` can turn the `dev_resteasy_grpc_arrays___ArrayHolder` back to the original array:

```
Object array = ArrayUtility.getArray(holder);
Assert.assertArrayEquals(new int[][] {{3, 5}, {7, 11, 13}}, (int[][]) array);
```

These two calls to `ArrayUtility` depend on the fact that the target array is built from a primitive Java type. If the
array uses an application specific type, then there are two alternative calls that can be used:

```
public static dev_resteasy_grpc_arrays___ArrayHolder getHolder(JavabufTranslator translator, Object o);

```

and

```
public static Object getArray(JavabufTranslator translator, Array_proto.dev_resteasy_grpc_arrays___ArrayHolder ah) throws Exception;
```

Also, if an application uses arrays, the generated `JavabufTranslator` incorporates `ArrayUtility`, so that it can be
used instead:

```
dev_resteasy_grpc_arrays___ArrayHolder ah = (dev_resteasy_grpc_arrays___ArrayHolder) translator.translateToJavabuf(new int[][] {{3, 5}, {7, 11, 13}});
Object array = translator.translateFromJavabuf(ah);
Assert.assertArrayEquals(new int[][] {{3, 5}, {7, 11, 13}}, (int[][]) array);
```
{% endraw %}

**Note.** The latter point can be usefully expanded, independent of the presence of arrays. Consider the class

```
package dev.resteasy.grpc.example;
public class C {
Expand All @@ -138,21 +155,27 @@ used instead:
this.s = s;
}
}
```

Using the fluent methods created in, say, `C_proto` by the protobuf parser, an instance of
Using the fluent methods created in, say, `C_proto` by the protobuf parser, an instance of
`C_proto.dev_resteasy_grpc_example___C` can be created by

```
C_proto.dev_resteasy_grpc_example___C.Builder cb = C_proto.dev_resteasy_grpc_example___C.newBuilder();
C_proto.dev_resteasy_grpc_example___C c1 = cb.setI(3).setD(5.0).setS("seven").build();
```

Note that each field must be set individually. On the other hand, given the `C(int, double, String)` constructor,
an instance of `C_proto.dev_resteasy_grpc_example___C` can be created more directly:

C_proto.dev_resteasy_grpc_example___C c2 = (C_proto.dev_resteasy_grpc_example___C) translator.translateToJavabuf(new C(3, 5.0, "seven"));
```
C_proto.dev_resteasy_grpc_example___C c2 = (C_proto.dev_resteasy_grpc_example___C) translator.translateToJavabuf(new C(3, 5.0, "seven"));
```

They accomplish the same thing, so the choice is a matter of taste.

## Notes

[^javabuf]: See the documentation at [https://github.com/resteasy/resteasy-grpc/blob/main/docs/grpc-bridge.md](https://github.com/resteasy/resteasy-grpc/blob/main/docs/grpc-bridge.md)
for a discussion of javabuf classes
[^javabuf]: See the documentation
at [https://github.com/resteasy/resteasy-grpc/blob/main/docs/grpc-bridge.md](https://github.com/resteasy/resteasy-grpc/blob/main/docs/grpc-bridge.md)
for a discussion of javabuf classes

0 comments on commit 9ea6267

Please sign in to comment.