Skip to content

Commit 98b0624

Browse files
committed
feat(scala): demonstrate protobuf
1 parent 384c7f1 commit 98b0624

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

scaffold.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,6 @@ presets:
212212
scala:
213213
langs: ['Scala']
214214
lint: true
215+
proto: true
215216
minimal:
216217
langs: []

user_stories/scala.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,61 @@ output="$(bazel run src:Hello)"
5252
exit 1
5353
}
5454
~~~
55+
56+
## Using protobuf and gRPC
57+
58+
Let's introduce a small data schema.
59+
60+
~~~sh
61+
>src/foo.proto cat <<EOF
62+
syntax = "proto3";
63+
option java_package = "build.aspect.examples";
64+
option java_outer_classname = "FooOuterClass";
65+
message Foo {
66+
string message = 1;
67+
}
68+
EOF
69+
~~~
70+
71+
Generate the BUILD rules for protobuf:
72+
73+
~~~sh
74+
bazel run gazelle
75+
~~~
76+
77+
There isn't a Gazelle generator for scala_proto_library yet, so add it manually:
78+
79+
~~~sh
80+
buildozer 'new_load @rules_scala//scala_proto:scala_proto.bzl scala_proto_library' src:__pkg__
81+
buildozer 'new scala_proto_library foo_scala_proto' src:__pkg__
82+
buildozer 'add deps :foo_proto' src:foo_scala_proto
83+
buildozer 'add deps :foo_scala_proto' src:Hello
84+
~~~
85+
86+
Now overwrite `Hello.scala` to use the generated `Foo` message:
87+
88+
~~~sh
89+
>src/Hello.scala cat <<EOF
90+
import build.aspect.examples.FooOuterClass
91+
92+
object Hello {
93+
def main(args: Array[String]): Unit = {
94+
val foo = FooOuterClass.Foo.newBuilder()
95+
.setMessage("Hello from Scala Protobuf!")
96+
.build()
97+
println(foo)
98+
}
99+
}
100+
EOF
101+
~~~
102+
103+
Run the application again:
104+
105+
~~~sh
106+
output="$(bazel run src:Hello)"
107+
108+
[ "${output}" = "message: \"Hello from Scala Protobuf!\"" ] || {
109+
echo >&2 "Wanted output 'Hello from Scala Protobuf' but got '${output}'"
110+
exit 1
111+
}
112+
~~~

0 commit comments

Comments
 (0)