Skip to content

Commit

Permalink
Remove bases from attributes once they are finalized. (#3608)
Browse files Browse the repository at this point in the history
This improves performance and also fixes potential bugs where
codegen algorithms make modifications to base types that are
not reflected in dups created for example when projecting views.
  • Loading branch information
raphael authored Nov 9, 2024
1 parent 405ca1c commit be78cc6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions expr/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ func (a *AttributeExpr) Finalize() {
}
a.Merge(ru.Attribute())
}

// Now that we've merged the bases, we can clear them. This
// avoids issues where the bases are dupped and modifications
// made to the originals are not reflected in the dups.
a.Bases = nil

for _, nat := range *AsObject(a.Type) {
if pkgPath != "" {
if u := AsUnion(nat.Attribute.Type); u != nil {
Expand Down
1 change: 1 addition & 0 deletions http/codegen/server_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestDecode(t *testing.T) {
{"decode-body-map-string-validate", testdata.PayloadBodyMapStringValidateDSL, testdata.PayloadBodyMapStringValidateDecodeCode},
{"decode-body-map-user", testdata.PayloadBodyMapUserDSL, testdata.PayloadBodyMapUserDecodeCode},
{"decode-body-map-user-validate", testdata.PayloadBodyMapUserValidateDSL, testdata.PayloadBodyMapUserValidateDecodeCode},
{"decode-deep-user", testdata.PayloadDeepUserDSL, testdata.PayloadDeepUserDecodeCode},

{"decode-body-primitive-string-validate", testdata.PayloadBodyPrimitiveStringValidateDSL, testdata.PayloadBodyPrimitiveStringValidateDecodeCode},
{"decode-body-primitive-bool-validate", testdata.PayloadBodyPrimitiveBoolValidateDSL, testdata.PayloadBodyPrimitiveBoolValidateDecodeCode},
Expand Down
16 changes: 16 additions & 0 deletions http/codegen/testdata/payload_decode_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4338,6 +4338,22 @@ func DecodeMethodBodyMapUserValidateRequest(mux goahttp.Muxer, decoder func(*htt
}
`

var PayloadDeepUserDecodeCode = `// marshalServicedeepuserviewsImmediatechildextenderViewToImmediatechildextenderResponseBody
// builds a value of type *ImmediatechildextenderResponseBody from a value of
// type *servicedeepuserviews.ImmediatechildextenderView.
func marshalServicedeepuserviewsImmediatechildextenderViewToImmediatechildextenderResponseBody(v *servicedeepuserviews.ImmediatechildextenderView) *ImmediatechildextenderResponseBody {
if v == nil {
return nil
}
res := &ImmediatechildextenderResponseBody{}
if v.DeepChild != nil {
res.DeepChild = marshalServicedeepuserviewsDeepchildViewToDeepchildResponseBody(v.DeepChild)
}
return res
}
`

var PayloadBodyPrimitiveStringValidateDecodeCode = `// DecodeMethodBodyPrimitiveStringValidateRequest returns a decoder for
// requests sent to the ServiceBodyPrimitiveStringValidate
// MethodBodyPrimitiveStringValidate endpoint.
Expand Down
34 changes: 34 additions & 0 deletions http/codegen/testdata/payload_dsls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,40 @@ var PayloadBodyMapUserValidateDSL = func() {
})
}

var PayloadDeepUserDSL = func() {
var DeepChild = ResultType("DeepChild", func() {
Attribute("name", String)
})
var ImmediateChild = ResultType("ImmediateChild", func() {
Attributes(func() {
Attribute("deep_child", DeepChild)
})
View("other", func() {
Attribute("deep_child")
})
})
var ImmediateChildExtender = ResultType("ImmediateChildExtender", func() {
Extend(ImmediateChild)
Attributes(func() {
Attribute("deep_child", DeepChild)
})
View("other", func() {
Attribute("deep_child")
})
})
var TopLevel = ResultType("TopLevel", func() {
Attributes(func() {
Attribute("immediate_child", ImmediateChildExtender)
})
})
Service("ServiceDeepUser", func() {
Method("MethodDeepUser", func() {
Result(TopLevel)
HTTP(func() { GET("/") })
})
})
}

var PayloadBodyPrimitiveStringValidateDSL = func() {
Service("ServiceBodyPrimitiveStringValidate", func() {
Method("MethodBodyPrimitiveStringValidate", func() {
Expand Down

0 comments on commit be78cc6

Please sign in to comment.