Skip to content

Commit 5421505

Browse files
authored
Make Goa proto wrapper field optional for arrays (goadesign#3167)
Since proto will serialize empty arrays as nil.
1 parent d96229f commit 5421505

File tree

4 files changed

+13
-48
lines changed

4 files changed

+13
-48
lines changed

grpc/codegen/protobuf.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ func makeProtoBufMessage(att *expr.AttributeExpr, tname string, sd *ServiceData)
6666
}
6767
return att
6868
case expr.IsPrimitive(att.Type):
69-
wrapAttr(att, tname, sd)
69+
wrapAttr(att, tname, true, sd)
7070
return att
7171
case isut:
7272
if expr.IsArray(ut) {
73-
wrapAttr(att, tname, sd)
73+
wrapAttr(att, tname, false, sd)
7474
}
7575
case expr.IsArray(att.Type) || expr.IsMap(att.Type):
76-
wrapAttr(att, tname, sd)
76+
wrapAttr(att, tname, false, sd)
7777
case expr.IsObject(att.Type) || expr.IsUnion(att.Type):
7878
att.Type = &expr.UserTypeExpr{
7979
TypeName: tname,
@@ -102,12 +102,12 @@ func makeProtoBufMessageR(att *expr.AttributeExpr, tname *string, sd *ServiceDat
102102
switch {
103103
case expr.IsArray(att.Type):
104104
wrapAttr(att, "ArrayOf"+tname+
105-
protoBufify(protoBufMessageDef(expr.AsArray(att.Type).ElemType, sd), true, true), sd)
105+
protoBufify(protoBufMessageDef(expr.AsArray(att.Type).ElemType, sd), true, true), true, sd)
106106
case expr.IsMap(att.Type):
107107
m := expr.AsMap(att.Type)
108108
wrapAttr(att, tname+"MapOf"+
109109
protoBufify(protoBufMessageDef(m.KeyType, sd), true, true)+
110-
protoBufify(protoBufMessageDef(m.ElemType, sd), true, true), sd)
110+
protoBufify(protoBufMessageDef(m.ElemType, sd), true, true), true, sd)
111111
}
112112
}
113113

@@ -116,7 +116,7 @@ func makeProtoBufMessageR(att *expr.AttributeExpr, tname *string, sd *ServiceDat
116116
return
117117
case isut:
118118
if expr.IsArray(ut) {
119-
wrapAttr(ut.Attribute(), ut.Name(), sd)
119+
wrapAttr(ut.Attribute(), ut.Name(), false, sd)
120120
}
121121
makeProtoBufMessageR(ut.Attribute(), tname, sd, seen)
122122
case expr.IsArray(att.Type):
@@ -140,9 +140,9 @@ func makeProtoBufMessageR(att *expr.AttributeExpr, tname *string, sd *ServiceDat
140140

141141
// wrapAttr makes the attribute type a user type by wrapping the given
142142
// attribute into an attribute named "field".
143-
func wrapAttr(att *expr.AttributeExpr, tname string, sd *ServiceData) {
143+
func wrapAttr(att *expr.AttributeExpr, tname string, req bool, sd *ServiceData) {
144144
wrap := func(attr *expr.AttributeExpr) *expr.AttributeExpr {
145-
return &expr.AttributeExpr{
145+
res := &expr.AttributeExpr{
146146
Type: &expr.Object{
147147
&expr.NamedAttributeExpr{
148148
Name: "field",
@@ -153,10 +153,13 @@ func wrapAttr(att *expr.AttributeExpr, tname string, sd *ServiceData) {
153153
},
154154
},
155155
},
156-
Validation: &expr.ValidationExpr{
156+
}
157+
if req {
158+
res.Validation = &expr.ValidationExpr{
157159
Required: []string{"field"},
158-
},
160+
}
159161
}
162+
return res
160163
}
161164
switch dt := att.Type.(type) {
162165
case expr.UserType:

grpc/codegen/testdata/client_type_code.go

-29
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,6 @@ func NewMethodResultWithCollectionResult(message *service_result_with_collection
183183
return result
184184
}
185185
186-
// ValidateMethodResultWithCollectionResponse runs the validations defined on
187-
// MethodResultWithCollectionResponse.
188-
func ValidateMethodResultWithCollectionResponse(message *service_result_with_collectionpb.MethodResultWithCollectionResponse) (err error) {
189-
if message.Result != nil {
190-
if err2 := ValidateResultT(message.Result); err2 != nil {
191-
err = goa.MergeErrors(err, err2)
192-
}
193-
}
194-
return
195-
}
196-
197-
// ValidateResultT runs the validations defined on ResultT.
198-
func ValidateResultT(result *service_result_with_collectionpb.ResultT) (err error) {
199-
if result.CollectionField != nil {
200-
if err2 := ValidateRTCollection(result.CollectionField); err2 != nil {
201-
err = goa.MergeErrors(err, err2)
202-
}
203-
}
204-
return
205-
}
206-
207-
// ValidateRTCollection runs the validations defined on RTCollection.
208-
func ValidateRTCollection(collectionField *service_result_with_collectionpb.RTCollection) (err error) {
209-
if collectionField.Field == nil {
210-
err = goa.MergeErrors(err, goa.MissingFieldError("field", "collectionField"))
211-
}
212-
return
213-
}
214-
215186
// svcServiceresultwithcollectionResultTToServiceResultWithCollectionpbResultT
216187
// builds a value of type *service_result_with_collectionpb.ResultT from a
217188
// value of type *serviceresultwithcollection.ResultT.

grpc/codegen/testdata/request_decoder_code.go

-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ func DecodeMethodUnaryRPCNoResultRequest(ctx context.Context, v interface{}, md
3232
if message, ok = v.(*service_unary_rpc_no_resultpb.MethodUnaryRPCNoResultRequest); !ok {
3333
return nil, goagrpc.ErrInvalidType("ServiceUnaryRPCNoResult", "MethodUnaryRPCNoResult", "*service_unary_rpc_no_resultpb.MethodUnaryRPCNoResultRequest", v)
3434
}
35-
if err := ValidateMethodUnaryRPCNoResultRequest(message); err != nil {
36-
return nil, err
37-
}
3835
}
3936
var payload []string
4037
{

grpc/codegen/testdata/streaming_code.go

-6
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@ func (s *MethodServerStreamingArrayClientStream) Recv() ([]int, error) {
172172
if err != nil {
173173
return res, err
174174
}
175-
if err = ValidateMethodServerStreamingArrayResponse(v); err != nil {
176-
return res, err
177-
}
178175
return NewMethodServerStreamingArrayResponse(v), nil
179176
}
180177
`
@@ -197,9 +194,6 @@ func (s *MethodServerStreamingMapClientStream) Recv() (map[string]*serviceserver
197194
if err != nil {
198195
return res, err
199196
}
200-
if err = ValidateMethodServerStreamingMapResponse(v); err != nil {
201-
return res, err
202-
}
203197
return NewMethodServerStreamingMapResponse(v), nil
204198
}
205199
`

0 commit comments

Comments
 (0)