<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by REN software developer on Medium]]></title>
        <description><![CDATA[Stories by REN software developer on Medium]]></description>
        <link>https://medium.com/@ren0112ngzk?source=rss-ca13c887a697------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vmrNZHN0yK_QctWLsDGpeQ.jpeg</url>
            <title>Stories by REN software developer on Medium</title>
            <link>https://medium.com/@ren0112ngzk?source=rss-ca13c887a697------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:10:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ren0112ngzk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Resilient gRPC Client with Exponential Backoff in Go]]></title>
            <link>https://medium.com/@ren0112ngzk/building-a-resilient-grpc-client-with-exponential-backoff-in-go-2d6aa0054a29?source=rss-ca13c887a697------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d6aa0054a29</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[buf]]></category>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[retry]]></category>
            <category><![CDATA[back-off]]></category>
            <dc:creator><![CDATA[REN software developer]]></dc:creator>
            <pubDate>Wed, 02 Apr 2025 05:27:16 GMT</pubDate>
            <atom:updated>2025-04-02T05:27:16.753Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we’ll walk through building a simple gRPC system in Go that demonstrates <strong>retry logic with exponential backoff</strong> using the popular <a href="https://github.com/cenkalti/backoff">cenkalti/backoff</a> library. This pattern is extremely useful in distributed systems where transient errors (like network hiccups or temporarily overloaded services) are expected.</p><p><a href="https://github.com/cenkalti/backoff">GitHub - cenkalti/backoff: ⏱ The exponential backoff algorithm in Go</a></p><h3>Overview</h3><p>This example project includes:</p><ul><li><strong>A gRPC server</strong> that occasionally fails to simulate real-world unreliability</li><li><strong>A gRPC client</strong> that retries failed requests with exponential backoff for certain error codes</li></ul><h3>Tech Stack &amp; Dependencies</h3><ul><li>Go (≥ 1.19)</li><li>gRPC (via google.golang.org/grpc)</li><li>Retry logic via github.com/cenkalti/backoff/v4</li><li>Code generation powered by Buf</li></ul><h3>Setup Instructions</h3><h4>1. Clone the Repo</h4><p><a href="https://github.com/rensawamo/backoff_retry_example">GitHub - rensawamo/backoff_retry_example</a></p><pre><br>git clone https://github.com/your-repo/grpc-backoff-retry-example.git<br>cd grpc-backoff-retry-example</pre><h4>2. Install Dependencies</h4><pre>go mod tidy</pre><h4>3. Generate gRPC Code</h4><pre>make buf-gen</pre><h3>Running the Example</h3><h4>Start the gRPC Server</h4><p>The server will start on localhost:50051</p><pre>go run server/main.go</pre><h4>Run the gRPC Client</h4><ol><li>Run the gRPC client:</li></ol><pre>go run client/main.go</pre><p>2. The client sends a request to the server and retries upon encountering transient errors (Unavailable or DeadlineExceeded).</p><p>3. Logs will show the retry attempts and the final result.</p><h3>🔁 Retry Behavior in the Client</h3><p>The client handles two specific gRPC errors as retryable:</p><ul><li>codes.Unavailable</li><li>codes.DeadlineExceeded</li></ul><p>These simulate network failures or server unavailability.</p><h4>Exponential Backoff Configuration</h4><p>We use backoff.NewExponentialBackOff() to manage delay intervals between retries. Here’s a simplified snippet:</p><h3>Example Output</h3><h4>Server</h4><pre>gRPC server is running on port 50051...<br>Received request: Hello, gRPC with Backoff!<br>Simulating transient error: Unavailable<br>Received request: Hello, gRPC with Backoff!<br>Simulating delay: 2s<br>Sending response: Hello from gRPC server!</pre><h4>Client</h4><pre>Retryable error: rpc error: code = Unavailable desc = simulated transient error. Retrying...<br>Retryable error: rpc error: code = DeadlineExceeded desc = operation timed out. Retrying...<br>Response received: Reply:&quot;Hello from gRPC server!&quot;</pre><h3>Resources</h3><p><a href="https://github.com/cenkalti/backoff">GitHub - cenkalti/backoff: ⏱ The exponential backoff algorithm in Go</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d6aa0054a29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with Connect-Go: The Next Evolution of gRPC in Go]]></title>
            <link>https://medium.com/@ren0112ngzk/getting-started-with-connect-go-the-next-evolution-of-grpc-in-go-780398abb9aa?source=rss-ca13c887a697------2</link>
            <guid isPermaLink="false">https://medium.com/p/780398abb9aa</guid>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[connectgo]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[buf]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <dc:creator><![CDATA[REN software developer]]></dc:creator>
            <pubDate>Thu, 27 Mar 2025 03:40:59 GMT</pubDate>
            <atom:updated>2025-03-27T03:40:59.742Z</atom:updated>
            <content:encoded><![CDATA[<h3>Demo APP ⬇️</h3><p><a href="https://github.com/rensawamo/dart-go-grpc-connect">GitHub - rensawamo/dart-go-grpc-connect: [Eliza App] Demonstrating Connect integration between a Go backend and a Dart (Flutter) frontend.</a></p><h3>Who This Is For</h3><ul><li>Interested in <strong>schema-driven development</strong></li><li>Want to build <strong>flexible APIs</strong> that support both REST and gRPC</li><li>Value <strong>type-safe</strong> backend and frontend development</li><li>Curious about <strong>Buf</strong></li><li>Want to try <strong>MySQL migrations and environment setup using Docker</strong></li></ul><p>You can find the sample project here!</p><h3>What is Connect?</h3><p><strong>Connect</strong> is a library that simplifies gRPC and makes it easy to build HTTP APIs compatible with browsers. Some of its key benefits include:</p><p><a href="https://connectrpc.com/docs/go/getting-started/">Getting started | Connect</a></p><p>・<strong>Schema-driven development</strong>:<br> Define your API using Protocol Buffers and auto-generate shared code for backend and frontend.</p><p>・<strong>Simplified setup</strong>:<br> Unlike gRPC, no need for proxies or complex configurations.</p><p>・<strong>Works over HTTP/1.1</strong>:<br> Testable with curl out of the box.</p><p>・<strong>Streaming support</strong>:<br> Supports Connect, gRPC, and gRPC-Web protocols — including full-duplex streaming via HTTP/2.</p><h3>Why I Wrote This</h3><p>In my project, we use <strong>Go for the Backend/BFF</strong> and <strong>Dart for the Frontend</strong>.<br> While Connect has long supported Go, Dart support was lacking for a while — and I was patiently waiting.</p><p>But recently, the Dart docs got updated, and now Connect is officially supported for Dart too! 🙌</p><p>Alongside that, the official repo introduced a sample app called <strong>Eliza</strong>.<br> That inspired me to build an extended version featuring <strong>JWT authentication</strong> and <strong>interceptors</strong>, and write about it — hoping it might help others.</p><h3>What is Eliza?</h3><p><strong>Eliza</strong> is a simple chatbot created in the 1960s by Joseph Weizenbaum at MIT.<br> It mimicked a psychotherapist and is one of the earliest examples of natural language processing.</p><p>In this sample project, I took the core idea and modernized it with <strong>Connect-Go</strong>.<br> The app randomly returns sentences (pre-registered via database migration) when you talk to Eliza.</p><p><a href="https://en.wikipedia.org/wiki/ELIZA">ELIZA - Wikipedia</a></p><h3>Sample Project Overview</h3><p>The proto files are defined under:</p><pre>proto/</pre><p><a href="https://github.com/rensawamo/dart-go-grpc-connect/tree/main/proto">dart-go-grpc-connect/proto at main · rensawamo/dart-go-grpc-connect</a></p><p>This allows <strong>both the backend and frontend to share the same API schema</strong>.</p><h3>Flow:</h3><ol><li>Call login to receive an access token</li><li>Use that token in the request header to talk to Eliza</li><li>Eliza responds with a random sentence from the database</li></ol><h3>Directory Structure</h3><pre>project-root/<br>├── backend/     # Go backend<br>│   └── gen/     # Generated code<br>├── frontend/    # Dart frontend<br>│   └── lib/gen/ # Generated code<br>├── proto/       # Protocol Buffers definitions</pre><p>Using <strong>Buf</strong>, you can generate code for both Go and Dart from a single .proto file:</p><pre>$ make gen<br>cd backend &amp;&amp; make gen &amp;&amp; cd ../frontend &amp;&amp; make gen</pre><h3>Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*sV5twTj7XWPgztRwTW72zQ.png" /></figure><ul><li><strong>Mux</strong>: routes requests to the appropriate handler</li><li><strong>Handler</strong>: receives requests and invokes use cases</li><li><strong>Usecase</strong>: implements business logic</li><li><strong>Repository</strong>: abstracts DB access</li><li><strong>Domain</strong>: defines core data structures</li><li><strong>DI (Dependency Injection)</strong>: keeps layers loosely coupled</li></ul><pre>backend/<br>├── api/handler/<br>├── api/interceptor/<br>├── cmd/<br>├── database/<br>├── di/<br>├── domain/<br>├── gen/<br>├── internal/<br>├── repository/<br>├── usecase/</pre><h3>Project Setup</h3><h4>Generate encryption keys</h4><pre>ssh-keygen -t rsa -b 2048 -f $PRIVATE_KEY_PATH -N &quot;&quot;<br>ssh-keygen -p -m PEM -f $PRIVATE_KEY_PATH</pre><h4>Configure environment variables in backend/.envrc</h4><pre>PRIVATE_KEY_PATH=your_private_key_path</pre><h4>Load env and start MySQL via Docker</h4><pre>cd backend &amp;&amp; direnv allow<br>make up</pre><h4>Run migrations</h4><pre>make migrate-up</pre><p>This registers:</p><ul><li>Test user (test@google.com)</li><li>Random sentences Eliza can return (e.g. “Hi”, “Hola”, “Bonjour”)</li></ul><h3>Frontend Setup</h3><p>The frontend is pre-configured to auto-fill test user credentials (email/password) in TextController on load, making login a one-click process.</p><p>This helps you quickly try out the auth + API flow with minimal setup.</p><h3>Running the Server</h3><pre>make run<br>go run cmd/main.go</pre><p>Server starts on localhost:8080</p><h3>API Testing with Curl</h3><h4>login API</h4><pre>curl --location --request POST &#39;http://localhost:8080/auth.v1.AuthService/Login&#39; \<br>--header &quot;Content-Type: application/json&quot; \<br>--data &#39;{&quot;email&quot;: &quot;test@google.com&quot;, &quot;password&quot;: &quot;example&quot;}&#39;</pre><p>Or just run:</p><pre>make login</pre><h4>say API</h4><pre>curl --location --request POST &#39;http://localhost:8080/eliza.v1.ElizaService/Say&#39; \<br>--header &quot;Authorization: Bearer your_access_token&quot; \<br>--header &quot;Content-Type: application/json&quot; \<br>--data &#39;{&quot;sentence&quot;: &quot;Hi&quot;}&#39;</pre><p>Or:</p><pre>make say</pre><p>Eliza responds with a random sentence.</p><h3>createSentence API</h3><p>Want to add more variety?</p><pre>curl --location --request POST &#39;http://localhost:8080/eliza.v1.ElizaService/CreateSentence&#39; \<br>--header &quot;Authorization: Bearer your_access_token&quot; \<br>--header &quot;Content-Type: application/json&quot; \<br>--data &#39;{&quot;input&quot;: &quot;Ciao!&quot;}&#39;</pre><p>Or:</p><pre>make create_sentence input=&quot;Ciao!&quot;</pre><h3>Auth Interceptor Sample (Go)</h3><h4>Request Flow</h4><p>Public API<br>Adopted by login authentication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*EbWM3mUE3EUqc2KHIV1eWw.png" /></figure><p>API with Authentication<br>Adopted by Eliza API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*Jk1Rl-wZ2VfV2ejmy_0zsA.png" /></figure><pre>func NewAuthInterceptor(issuer, keyPath string) connect.UnaryInterceptorFunc {<br> interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {<br>  return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {<br>   authHeader := req.Header().Get(&quot;Authorization&quot;)<br>   if authHeader == &quot;&quot; {<br>    return nil, connect.NewError(connect.CodeUnauthenticated, errors.New(&quot;missing authorization header&quot;))<br>   }<br><br>   token := strings.TrimSpace(strings.TrimPrefix(authHeader, &quot;Bearer&quot;))<br>   if token == &quot;&quot; {<br>    return nil, connect.NewError(connect.CodeUnauthenticated, errors.New(&quot;empty token after trimming&quot;))<br>   }<br><br>   // Validate token...<br>   return next(ctx, req)<br>  })<br> }<br> return connect.UnaryInterceptorFunc(interceptor)<br>}</pre><p>Interceptor registration:</p><pre>authInterceptor := connect.WithInterceptors(interceptor.NewAuthInterceptor(issuer, keyPath))<br>mux.Handle(authv1connect.NewAuthServiceHandler(loginHandler))<br>mux.Handle(elizav1connect.NewElizaServiceHandler(elizaHandler, authInterceptor))</pre><h3>Wrapping Up</h3><p>Thanks to schema-first development, mocking and rapid prototyping become much easier.</p><p>This has drastically improved our dev productivity.</p><p>If you’re using Connect in Go or Dart — or want to try schema-driven APIs with strong type safety — I hope this helps!</p><p>Let me know what you think — and feel free to check out the frontend article as well!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=780398abb9aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with Connect-Dart: The Next Evolution of gRPC in Dart]]></title>
            <link>https://medium.com/@ren0112ngzk/getting-started-with-connect-dart-the-next-evolution-of-grpc-in-dart-fead942088f9?source=rss-ca13c887a697------2</link>
            <guid isPermaLink="false">https://medium.com/p/fead942088f9</guid>
            <category><![CDATA[protocol-buffers]]></category>
            <category><![CDATA[connect]]></category>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[buf]]></category>
            <dc:creator><![CDATA[REN software developer]]></dc:creator>
            <pubDate>Wed, 26 Mar 2025 01:18:56 GMT</pubDate>
            <atom:updated>2025-03-26T01:20:08.569Z</atom:updated>
            <content:encoded><![CDATA[<h3>Demo APP ⬇️</h3><p><a href="https://github.com/rensawamo/dart-go-grpc-connect">GitHub - rensawamo/dart-go-grpc-connect: [Eliza App] Demonstrating Connect integration between a Go backend and a Dart (Flutter) frontend.</a></p><p>Connect is finally available in Dart!<br> In this article, I’ll walk you through how to get started with Connect-Dart — a next-gen alternative to gRPC.<br> I believe it’s going to play a big role in future development, so I highly recommend giving it a try.</p><ul><li><a href="https://connectrpc.com/docs/dart/getting-started/">Getting started | Connect</a></li><li><a href="https://pub.dev/packages/connectrpc">connectrpc | Dart package</a></li><li><a href="https://github.com/connectrpc/connect-dart">GitHub - connectrpc/connect-dart: The Dart implementation of the Protobuf RPC that works</a></li></ul><h3>Who This Is For</h3><ul><li>Developers interested in schema-driven development</li><li>Anyone looking to build APIs that support both REST and gRPC</li><li>Teams that want type-safe communication between backend and frontend</li><li>Buf lovers or those curious about Buf</li></ul><h3>What is Connect?</h3><p>Connect is a simplified library for gRPC-like APIs that works over HTTP and is browser-compatible. Here’s what makes Connect great:</p><ul><li><strong>Schema-driven Development</strong>: Define your APIs with Protobuf and generate shared code for both frontend and backend.</li><li><strong>No Extra Setup</strong>: Unlike gRPC, you don’t need to set up proxies or complex config.</li><li><strong>HTTP/1.1 Compatible</strong>: You can test endpoints easily with tools like curl.</li><li><strong>Streaming Support</strong>: Works with Connect, gRPC, and gRPC-Web protocols. Fully compatible with bidirectional streaming over HTTP/2.</li></ul><h3>Supported Protocols</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S5OahYYhrAhiJhBgryGb1Q.png" /></figure><pre>import &#39;package:connectrpc/protocol/connect.dart&#39; as protocol;<br>// You can also switch protocols easily:<br>// import &#39;package:connectrpc/protocol/grpc.dart&#39; as protocol;<br>// import &#39;package:connectrpc/protocol/grpc_web.dart&#39; as protocol;<br><br>final transport = protocol.Transport(<br>  baseUrl: &quot;https://demo.connectrpc.com&quot;,<br>  codec: const ProtoCodec(), // Or use JsonCodec()<br>  httpClient: createHttpClient(),<br>);</pre><h3>Available HTTP Clients</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dh5NW5ZXNiE9eBABuT_dXw.png" /></figure><h3>Connect Error Codes</h3><p><a href="https://github.com/connectrpc/connect-dart/blob/main/packages/connect/lib/src/code.dart">connect-dart/packages/connect/lib/src/code.dart at main · connectrpc/connect-dart</a></p><h3>Sample App Overview</h3><p>We extended the official Eliza demo app and added authentication.<br> It runs locally and gives a great intro to Connect’s features.</p><h3>Frontend Directory Structure</h3><pre>frontend/<br>├── core/<br>│   ├── gen/          # Generated code from Protobuf<br>│   ├── provider/     # Riverpod-based dependency injection<br>│   ├── router/       # Route management<br>│   ├── state/        # SSOT state management<br>│   ├── util/         # Utilities like logger<br>├── feature/<br>│   ├── eliza/        # Eliza feature<br>│   ├── login/        # Auth feature<br>├── app.dart<br>├── main.dart</pre><h3>App Flow</h3><ol><li><strong>Login</strong> to obtain access token<br> (We register users via DB migration to simplify the UX.)</li><li><strong>Send a request to Eliza</strong>, with the token in the header</li></ol><ul><li>You get a randomized migrated response</li></ul><h3>Define APIs with Protobuf</h3><p>Just like gRPC, Connect uses Protocol Buffers for API definition.</p><p><a href="https://github.com/rensawamo/dart-go-grpc-connect/tree/main/proto">dart-go-grpc-connect/proto at main · rensawamo/dart-go-grpc-connect</a></p><h3>RPC Types</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wr8jXwPIheN9BE3QrcOf4w.png" /></figure><h3>Code Generation Setup</h3><p>Project structure:</p><pre>project-root/<br>├── backend/       # Backend in Go<br>│   └── gen/<br>├── frontend/      # Frontend in Dart<br>│   └── lib/gen/<br>├── proto/         # Protobuf definitions</pre><p>Use buf or any compatible generator to generate both server and client code:</p><pre>$ make gen<br>cd backend &amp;&amp; make gen &amp;&amp; cd ../frontend &amp;&amp; make gen</pre><p>This will output files like:</p><pre>core/gen/auth/v1/<br>├── auth.connect.client.dart<br>├── auth.connect.spec.dart<br>├── auth.pb.dart<br>...</pre><h3>Making Requests</h3><pre>final transport = protocol.Transport(<br>  baseUrl: &quot;https://demo.connectrpc.com&quot;,<br>  codec: const ProtoCodec(),<br>  httpClient: createHttpClient(),<br>);<br><br>final response = await ElizaServiceClient(transport).say(<br>  SayRequest(sentence: sentence),<br>);</pre><h3>Protobuf Definitions</h3><pre>message SayRequest {<br>  string sentence = 1;<br>}<br><br>message SayResponse {<br>  string sentence = 1;<br>}<br><br>service ElizaService {<br>  rpc Say(SayRequest) returns (SayResponse) {}<br>}</pre><h3>Streaming Examples</h3><h4>Server Streaming</h4><pre>final stream = elizaClient.introduce(IntroduceRequest());<br>await for (final next in stream) {<br>  print(next);<br>}</pre><h4>Client / Bidirectional Streaming</h4><pre>final stream = elizaClient.converse(Stream.fromIterable([ConverseRequest()]));<br>await for (final next in stream) {<br>  print(next);<br>}</pre><h3>Add Interceptors with Riverpod</h3><p>Let’s inject metadata (like auth headers) into each request:</p><pre>@riverpod<br>MetadataInterceptor metaDataInterceptor(Ref ref) {<br>  final token = ref.read(tokenNotifierProvider);<br>  return MetadataInterceptor(token);<br>}<br><br>class MetadataInterceptor {<br>  const MetadataInterceptor(this.token);<br>  final Token token;<br><br>  AnyFn&lt;I, O&gt; call&lt;I extends Object, O extends Object&gt;(AnyFn&lt;I, O&gt; next) {<br>    return (req) async {<br>      req.headers.add(&#39;Authorization&#39;, &#39;Bearer ${token.accessToken}&#39;);<br>      final signal = TimeoutSignal(Duration(seconds: 10));<br><br>      if (req is UnaryRequest&lt;I, O&gt;) {<br>        return next(UnaryRequest(req.spec, req.url, req.headers, req.message, signal));<br>      } else if (req is StreamRequest&lt;I, O&gt;) {<br>        return next(StreamRequest(req.spec, req.url, req.headers, req.message, signal));<br>      }<br>    };<br>  }<br>}</pre><h3>Add to Transport</h3><pre>@riverpod<br>Transport transport(Ref ref, {bool isRequireMetaData = true}) {<br>  final metadataInterceptor = ref.read(metaDataInterceptorProvider);<br><br>  return protocol.Transport(<br>    baseUrl: Platform.isAndroid ? &#39;http://10.0.2.2:8080&#39; : &#39;http://localhost:8080&#39;,<br>    codec: const ProtoCodec(),<br>    httpClient: createHttpClient(),<br>    interceptors: [<br>      const LoggingInterceptor().call,<br>      if (isRequireMetaData) metadataInterceptor.call,<br>    ],<br>  );<br>}</pre><h3>Final Thoughts</h3><p>I built this Eliza extension app while exploring Connect-Dart.<br> My current projects still use classic gRPC and require defining clients manually with grpcHandler.<br> However, Connect allows automatic client generation, which significantly reduces boilerplate.</p><p>That said, Connect-Dart is still in early development, and we’re carefully monitoring before fully migrating.</p><p>If you’ve already tried moving to Connect, I’d love to hear your feedback in the comments!<br> Hope this post helps you get started with Connect-Dart! ⭐️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fead942088f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Extract and Reuse Common Protocol Buffers (proto3) Definitions]]></title>
            <link>https://medium.com/@ren0112ngzk/how-to-extract-and-reuse-common-protocol-buffers-proto3-definitions-4434124c06fb?source=rss-ca13c887a697------2</link>
            <guid isPermaLink="false">https://medium.com/p/4434124c06fb</guid>
            <category><![CDATA[code-generation]]></category>
            <category><![CDATA[buf]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[protobuf]]></category>
            <category><![CDATA[commons]]></category>
            <dc:creator><![CDATA[REN software developer]]></dc:creator>
            <pubDate>Tue, 25 Mar 2025 06:28:53 GMT</pubDate>
            <atom:updated>2025-03-26T00:56:37.665Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, I’ll explain how to <strong>extract and reuse common protobuf definitions</strong>.<br> By centralizing the common definitions, you can <strong>prevent duplicate definition errors</strong> during code generation and improve code maintainability.</p><h3>Reference Repository</h3><p>You can find the reference repository here:<br> ➡️ <a href="https://github.com/rensawamo/common-proto">https://github.com/rensawamo/common-proto</a></p><h3>Folder Structure</h3><pre>.<br>├── buf.lock<br>├── buf.yaml<br>├── Makefile<br>├── README.md<br>├── proto<br>│   ├── common<br>│   │   └── common/v1<br>│   │       └── common.proto<br>│   ├── customerapi<br>│   │   └── customer/v1<br>│   │       └── customer.proto<br>│   └── employeeapi<br>│       └── employee/v1<br>│           └── employee.proto<br>└── output/go(other)<br>    ├── gen<br>    ├── Makefile<br>    ├── README.md<br>    └── buf.gen.yaml</pre><h3>proto Directory</h3><p>The proto/ directory contains the Protocol Buffers (proto3) definition files.</p><h3>🔎 common/v1/common.proto</h3><p>This file contains the <strong>common ENUM definitions</strong> that can be reused across different APIs.</p><p>Specifically, I created a common ENUM definition that could be used by both customerapi and employeeapi.<br> For example, you may want to define an ENUM for gender or prefecture depending on the project requirements.</p><pre>syntax = &quot;proto3&quot;;<br><br>package common.v1;<br><br>enum Prefecture {<br>  PREFECTURE_UNKNOWN_UNSPECIFIED = 0;<br>  PREFECTURE_TOKYO = 1;<br>  PREFECTURE_KANAGAWA = 2;<br>  PREFECTURE_CHIBA = 3;<br>  PREFECTURE_SAITAMA = 4;<br>  PREFECTURE_IBARAKI = 5;<br>  PREFECTURE_TOCHIGI = 6;<br>  PREFECTURE_GUNMA = 7;<br>}</pre><h3>customer/employee proto files</h3><p>You can import common.proto in other proto files like this:</p><pre>syntax = &quot;proto3&quot;;<br><br>package employee.v1;<br><br>import &quot;common/v1/common.proto&quot;;<br><br>message Employee {<br>  string id = 1;<br>  string name = 2;<br>  string department = 3;<br>  common.v1.Prefecture hometown = 4;<br>}</pre><p>By doing this, the common ENUM definitions can be easily reused across different APIs, reducing code duplication.</p><h3>Check buf.yaml</h3><p>The project uses <strong>Buf version 2</strong>.<br> With version 2, you can now manage multiple modules within a single buf.yaml file.</p><p>In version 1, complex configurations like buf.work.yaml were required, but version 2 makes it much simpler.</p><p>👉 <a href="https://buf.build/docs/configuration/v2/buf-yaml/">Buf YAML Configuration</a></p><h4>buf.yaml:</h4><pre># For details on buf.yaml configuration, visit https://buf.build/docs/configuration/v2/buf-yaml<br>version: v2<br>breaking:<br>  use:<br>    - PACKAGE<br>lint:<br>  use:<br>    - STANDARD<br>modules:<br>  - path: proto/common<br>  - path: proto/customerapi<br>  - path: proto/employeeapi<br>deps:<br>  - buf.build/googleapis/googleapis</pre><h3>Generate Code</h3><p>I tested generating code for the following languages:</p><ul><li>Dart</li><li>Go</li><li>Java</li><li>TypeScript</li></ul><p>Here’s an example of the generated Go files:</p><pre>.<br>├── output/go<br>│   └── gen<br>│       ├── common<br>│       │   └── v1<br>│       │       └── common.pb.go<br>│       ├── customer<br>│       │   └── v1<br>│       │       └── customer.pb.go<br>│       ├── employee<br>│       │   └── v1<br>│       │       └── employee.pb.go<br>│       └── google<br>│           └── protobuf<br>│               └── timestamp.pb.go<br>├── buf.gen.yaml<br>├── Makefile<br>└── README.md</pre><h3>Check buf.gen.yaml</h3><p>To generate code, you need to define buf.gen.yaml.</p><p>👉 <a href="https://buf.build/docs/configuration/v2/buf-gen-yaml/">Buf Generation Configuration</a></p><pre>version: v2<br>clean: true<br>managed:<br>  enabled: true<br>  override:<br>    - file_option: go_package_prefix<br>      value: https://github.com/rensawamo/common-proto/output/go/gen<br>plugins:<br>  - remote: buf.build/protocolbuffers/go:v1.36.6<br>    include_imports: true<br>    include_wkt: true<br>    out: gen<br>    opt: paths=source_relative</pre><h3>🏆 Summary</h3><p>This post explains how to:</p><p>✅ Extract and reuse common proto definitions<br> ✅ Prevent duplicate definition errors<br> ✅ Improve code maintenance and consistency</p><p>I wrote this because I struggled to find a clear guide on how to extract and reuse common proto definitions.<br> I hope this helps others who are facing similar issues! 😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4434124c06fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>