Skip to content

Commit 422ce31

Browse files
fix(examples): remove proto3 optional to avoid warnings
Remove `optional` keyword from proto files to eliminate the "proto3 optional" warning from protoc-gen-go-http. Update main.go implementations to use zero-value checks instead of nil pointer checks. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 45e09b3 commit 422ce31

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

examples/multi-service-api/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ func (s *UserServiceImpl) GetCurrentUser(ctx context.Context, req *models.GetCur
100100

101101
func (s *UserServiceImpl) UpdateProfile(ctx context.Context, req *models.UpdateProfileRequest) (*models.User, error) {
102102
user := s.users["user-xyz789"]
103-
if req.Name != nil {
104-
user.Name = *req.Name
103+
if req.Name != "" {
104+
user.Name = req.Name
105105
}
106-
if req.AvatarUrl != nil {
107-
user.AvatarUrl = *req.AvatarUrl
106+
if req.AvatarUrl != "" {
107+
user.AvatarUrl = req.AvatarUrl
108108
}
109109
return user, nil
110110
}

examples/multi-service-api/proto/models/user.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ message GetCurrentUserRequest {}
2626

2727
// UpdateProfileRequest for updating the current user's profile.
2828
message UpdateProfileRequest {
29-
optional string name = 1 [
29+
string name = 1 [
3030
(buf.validate.field).string = { min_len: 2, max_len: 100 },
3131
(sebuf.http.field_examples) = { values: ["Updated Name"] }
3232
];
33-
optional string avatar_url = 2 [
33+
string avatar_url = 2 [
3434
(sebuf.http.field_examples) = { values: ["https://example.com/new-avatar.png"] }
3535
];
3636
}

examples/restful-crud/main.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,21 @@ func (s *ProductService) PatchProduct(ctx context.Context, req *models.PatchProd
186186
return nil, fmt.Errorf("product not found: %s", req.ProductId)
187187
}
188188

189-
// Partial update - only update provided fields
190-
if req.Name != nil {
191-
product.Name = *req.Name
189+
// Partial update - only update non-zero fields
190+
if req.Name != "" {
191+
product.Name = req.Name
192192
}
193-
if req.Description != nil {
194-
product.Description = *req.Description
193+
if req.Description != "" {
194+
product.Description = req.Description
195195
}
196-
if req.Price != nil {
197-
product.Price = *req.Price
196+
if req.Price != 0 {
197+
product.Price = req.Price
198198
}
199-
if req.StockQuantity != nil {
200-
product.StockQuantity = *req.StockQuantity
199+
if req.StockQuantity != 0 {
200+
product.StockQuantity = req.StockQuantity
201201
}
202-
if req.CategoryId != nil {
203-
product.CategoryId = *req.CategoryId
202+
if req.CategoryId != "" {
203+
product.CategoryId = req.CategoryId
204204
}
205205
product.UpdatedAt = time.Now().Unix()
206206

examples/restful-crud/proto/models/product.proto

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,32 +197,32 @@ message PatchProductRequest {
197197
(sebuf.http.field_examples) = { values: ["123e4567-e89b-12d3-a456-426614174000"] }
198198
];
199199

200-
// Updated name (optional).
201-
optional string name = 2 [
200+
// Updated name.
201+
string name = 2 [
202202
(buf.validate.field).string = { min_len: 1, max_len: 200 },
203203
(sebuf.http.field_examples) = { values: ["New Product Name"] }
204204
];
205205

206-
// Updated description (optional).
207-
optional string description = 3 [
206+
// Updated description.
207+
string description = 3 [
208208
(buf.validate.field).string = { max_len: 2000 },
209209
(sebuf.http.field_examples) = { values: ["New description"] }
210210
];
211211

212-
// Updated price (optional).
213-
optional double price = 4 [
212+
// Updated price.
213+
double price = 4 [
214214
(buf.validate.field).double = { gt: 0 },
215215
(sebuf.http.field_examples) = { values: ["149.99"] }
216216
];
217217

218-
// Updated stock quantity (optional).
219-
optional int32 stock_quantity = 5 [
218+
// Updated stock quantity.
219+
int32 stock_quantity = 5 [
220220
(buf.validate.field).int32 = { gte: 0 },
221221
(sebuf.http.field_examples) = { values: ["50"] }
222222
];
223223

224-
// Updated category ID (optional).
225-
optional string category_id = 6 [(sebuf.http.field_examples) = { values: ["cat-audio"] }];
224+
// Updated category ID.
225+
string category_id = 6 [(sebuf.http.field_examples) = { values: ["cat-audio"] }];
226226
}
227227

228228
// Request to delete a product.

examples/validation-showcase/proto/models/order.proto

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ message ContactInfo {
7474
(sebuf.http.field_examples) = { values: ["+14155551234"] }
7575
];
7676

77-
// Alternative contact name (optional, 2-100 chars if provided).
78-
optional string alternative_name = 3 [
79-
(buf.validate.field).string = { min_len: 2, max_len: 100 },
77+
// Alternative contact name (2-100 chars if provided).
78+
string alternative_name = 3 [
79+
(buf.validate.field).string = { max_len: 100 },
8080
(sebuf.http.field_examples) = { values: ["Jane Doe"] }
8181
];
8282
}
@@ -148,7 +148,7 @@ message OrderItem {
148148
bool gift_wrap = 8 [(sebuf.http.field_examples) = { values: ["false"] }];
149149

150150
// Gift message (if gift_wrap is true, max 500 chars).
151-
optional string gift_message = 9 [
151+
string gift_message = 9 [
152152
(buf.validate.field).string = { max_len: 500 },
153153
(sebuf.http.field_examples) = { values: ["Happy Birthday!"] }
154154
];
@@ -251,7 +251,7 @@ message Order {
251251
PaymentMethod payment_method = 16;
252252

253253
// Special instructions (max 1000 chars).
254-
optional string special_instructions = 17 [
254+
string special_instructions = 17 [
255255
(buf.validate.field).string = { max_len: 1000 },
256256
(sebuf.http.field_examples) = { values: ["Please leave at front door"] }
257257
];
@@ -307,7 +307,7 @@ message CreateOrderRequest {
307307
];
308308

309309
// Special delivery instructions.
310-
optional string special_instructions = 7 [
310+
string special_instructions = 7 [
311311
(buf.validate.field).string = { max_len: 1000 }
312312
];
313313

0 commit comments

Comments
 (0)