Skip to content

Commit

Permalink
Fix struct:field:name Meta to work for HTTP payloads (#3402)
Browse files Browse the repository at this point in the history
* Add test cases to TestClientTypes in http/codegen

* Add test cases of sttuct:field:name Meta for HTTP payloads

* Fix struct:field:name Meta to work for HTTP payloads
  • Loading branch information
tchssk authored Oct 25, 2023
1 parent 88ce4e6 commit 79317b6
Show file tree
Hide file tree
Showing 10 changed files with 493 additions and 4 deletions.
78 changes: 78 additions & 0 deletions http/codegen/client_body_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ func TestClientTypes(t *testing.T) {
{"client-payload-extend-validate", testdata.PayloadExtendedValidateDSL, PayloadExtendedValidateClientTypesFile},
{"client-result-type-validate", testdata.ResultTypeValidateDSL, ResultTypeValidateClientTypesFile},
{"client-with-result-collection", testdata.ResultWithResultCollectionDSL, WithResultCollectionClientTypesFile},
{"client-with-result-view", testdata.ResultWithResultViewDSL, ResultWithResultViewClientTypesFile},
{"client-empty-error-response-body", testdata.EmptyErrorResponseBodyDSL, EmptyErrorResponseBodyClientTypesFile},
{"client-with-error-custom-pkg", testdata.WithErrorCustomPkgDSL, WithErrorCustomPkgClientTypesFile},
{"client-body-custom-name", testdata.PayloadBodyCustomNameDSL, BodyCustomNameClientTypesFile},
{"client-path-custom-name", testdata.PayloadPathCustomNameDSL, ""},
{"client-query-custom-name", testdata.PayloadQueryCustomNameDSL, ""},
{"client-header-custom-name", testdata.PayloadHeaderCustomNameDSL, ""},
{"client-cookie-custom-name", testdata.PayloadCookieCustomNameDSL, ""},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down Expand Up @@ -711,6 +718,34 @@ func ValidateRtResponseBody(body *RtResponseBody) (err error) {
}
`

const ResultWithResultViewClientTypesFile = `// MethodResultWithResultViewResponseBody is the type of the
// "ServiceResultWithResultView" service "MethodResultWithResultView" endpoint
// HTTP response body.
type MethodResultWithResultViewResponseBody struct {
Name *string ` + "`" + `form:"name,omitempty" json:"name,omitempty" xml:"name,omitempty"` + "`" + `
Rt *RtResponseBody ` + "`" + `form:"rt,omitempty" json:"rt,omitempty" xml:"rt,omitempty"` + "`" + `
}
// RtResponseBody is used to define fields on response body types.
type RtResponseBody struct {
X *string ` + "`" + `form:"x,omitempty" json:"x,omitempty" xml:"x,omitempty"` + "`" + `
}
// NewMethodResultWithResultViewResulttypeOK builds a
// "ServiceResultWithResultView" service "MethodResultWithResultView" endpoint
// result from a HTTP "OK" response.
func NewMethodResultWithResultViewResulttypeOK(body *MethodResultWithResultViewResponseBody) *serviceresultwithresultviewviews.ResulttypeView {
v := &serviceresultwithresultviewviews.ResulttypeView{
Name: body.Name,
}
if body.Rt != nil {
v.Rt = unmarshalRtResponseBodyToServiceresultwithresultviewviewsRtView(body.Rt)
}
return v
}
`

const EmptyErrorResponseBodyClientTypesFile = `// NewMethodEmptyErrorResponseBodyInternalError builds a
// ServiceEmptyErrorResponseBody service MethodEmptyErrorResponseBody endpoint
// internal_error error.
Expand All @@ -735,3 +770,46 @@ func NewMethodEmptyErrorResponseBodyNotFound(inHeader string) serviceemptyerrorr
return v
}
`
const WithErrorCustomPkgClientTypesFile = `// MethodWithErrorCustomPkgErrorNameResponseBody is the type of the
// "ServiceWithErrorCustomPkg" service "MethodWithErrorCustomPkg" endpoint HTTP
// response body for the "error_name" error.
type MethodWithErrorCustomPkgErrorNameResponseBody struct {
Name *string ` + "`" + `form:"name,omitempty" json:"name,omitempty" xml:"name,omitempty"` + "`" + `
}
// NewMethodWithErrorCustomPkgErrorName builds a ServiceWithErrorCustomPkg
// service MethodWithErrorCustomPkg endpoint error_name error.
func NewMethodWithErrorCustomPkgErrorName(body *MethodWithErrorCustomPkgErrorNameResponseBody) *custom.CustomError {
v := &custom.CustomError{
Name: *body.Name,
}
return v
}
// ValidateMethodWithErrorCustomPkgErrorNameResponseBody runs the validations
// defined on MethodWithErrorCustomPkg_error_name_Response_Body
func ValidateMethodWithErrorCustomPkgErrorNameResponseBody(body *MethodWithErrorCustomPkgErrorNameResponseBody) (err error) {
if body.Name == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("name", "body"))
}
return
}
`

const BodyCustomNameClientTypesFile = `// MethodBodyCustomNameRequestBody is the type of the "ServiceBodyCustomName"
// service "MethodBodyCustomName" endpoint HTTP request body.
type MethodBodyCustomNameRequestBody struct {
Body *string ` + "`" + `form:"b,omitempty" json:"b,omitempty" xml:"b,omitempty"` + "`" + `
}
// NewMethodBodyCustomNameRequestBody builds the HTTP request body from the
// payload of the "MethodBodyCustomName" endpoint of the
// "ServiceBodyCustomName" service.
func NewMethodBodyCustomNameRequestBody(p *servicebodycustomname.MethodBodyCustomNamePayload) *MethodBodyCustomNameRequestBody {
body := &MethodBodyCustomNameRequestBody{
Body: p.Body,
}
return body
}
`
5 changes: 5 additions & 0 deletions http/codegen/client_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func TestClientCLIFiles(t *testing.T) {
{"map-query-object", testdata.PayloadMapQueryObjectDSL, testdata.MapQueryObjectBuildCode, 1, 1},
{"empty-body-build", testdata.PayloadBodyPrimitiveFieldEmptyDSL, testdata.EmptyBodyBuildCode, 1, 1},
{"with-params-and-headers-dsl", testdata.WithParamsAndHeadersBlockDSL, testdata.WithParamsAndHeadersBlockBuildCode, 1, 1},
{"body-custom-name", testdata.PayloadBodyCustomNameDSL, testdata.PayloadBodyCustomNameBuildCode, 1, 1},
{"path-custom-name", testdata.PayloadPathCustomNameDSL, testdata.PayloadPathCustomNameBuildCode, 1, 1},
{"query-custom-name", testdata.PayloadQueryCustomNameDSL, testdata.PayloadQueryCustomNameBuildCode, 1, 1},
{"header-custom-name", testdata.PayloadHeaderCustomNameDSL, testdata.PayloadHeaderCustomNameBuildCode, 1, 1},
{"cookie-custom-name", testdata.PayloadCookieCustomNameDSL, testdata.PayloadCookieCustomNameBuildCode, 1, 1},
}

for _, c := range cases {
Expand Down
6 changes: 6 additions & 0 deletions http/codegen/client_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func TestClientEncode(t *testing.T) {
{"query-map-alias", testdata.QueryMapAliasDSL, testdata.QueryMapAliasEncodeCode},
{"query-map-alias-validate", testdata.QueryMapAliasValidateDSL, testdata.QueryMapAliasValidateEncodeCode},
{"query-array-nested-alias-validate", testdata.QueryArrayNestedAliasValidateDSL, testdata.QueryArrayNestedAliasValidateEncodeCode},

{"body-custom-name", testdata.PayloadBodyCustomNameDSL, testdata.PayloadBodyCustomNameEncodeCode},
// path-custom-name is not needed because no encoder is created.
{"query-custom-name", testdata.PayloadQueryCustomNameDSL, testdata.PayloadQueryCustomNameEncodeCode},
{"header-custom-name", testdata.PayloadHeaderCustomNameDSL, testdata.PayloadHeaderCustomNameEncodeCode},
{"cookie-custom-name", testdata.PayloadCookieCustomNameDSL, testdata.PayloadCookieCustomNameEncodeCode},
}
golden := makeGolden(t, "testdata/payload_encode_functions.go")
if golden != nil {
Expand Down
6 changes: 6 additions & 0 deletions http/codegen/server_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ func TestDecode(t *testing.T) {
{"decode-query-array-nested-alias-validate", testdata.QueryArrayNestedAliasValidateDSL, testdata.QueryArrayNestedAliasValidateDecodeCode},
{"decode-header-int-alias", testdata.HeaderIntAliasDSL, testdata.HeaderIntAliasDecodeCode},
{"decode-path-int-alias", testdata.PathIntAliasDSL, testdata.PathIntAliasDecodeCode},

{"decode-body-custom-name", testdata.PayloadBodyCustomNameDSL, testdata.PayloadBodyCustomNameDecodeCode},
{"decode-path-custom-name", testdata.PayloadPathCustomNameDSL, testdata.PayloadPathCustomNameDecodeCode},
{"decode-query-custom-name", testdata.PayloadQueryCustomNameDSL, testdata.PayloadQueryCustomNameDecodeCode},
{"decode-header-custom-name", testdata.PayloadHeaderCustomNameDSL, testdata.PayloadHeaderCustomNameDecodeCode},
{"decode-cookie-custom-name", testdata.PayloadCookieCustomNameDSL, testdata.PayloadCookieCustomNameDecodeCode},
}
golden := makeGolden(t, "testdata/payload_decode_functions.go")
if golden != nil {
Expand Down
60 changes: 60 additions & 0 deletions http/codegen/server_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func TestServerTypes(t *testing.T) {
{"server-with-result-view", testdata.ResultWithResultViewDSL, ResultWithResultViewServerTypesFile},
{"server-empty-error-response-body", testdata.EmptyErrorResponseBodyDSL, ""},
{"server-with-error-custom-pkg", testdata.WithErrorCustomPkgDSL, WithErrorCustomPkgServerTypesFile},
{"server-body-custom-name", testdata.PayloadBodyCustomNameDSL, BodyCustomNameServerTypesFile},
{"server-path-custom-name", testdata.PayloadPathCustomNameDSL, PathCustomNameServerTypesFile},
{"server-query-custom-name", testdata.PayloadQueryCustomNameDSL, QueryCustomNameServerTypesFile},
{"server-header-custom-name", testdata.PayloadHeaderCustomNameDSL, HeaderCustomNameServerTypesFile},
{"server-cookie-custom-name", testdata.PayloadCookieCustomNameDSL, CookieCustomNameServerTypesFile},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down Expand Up @@ -321,3 +326,58 @@ func NewMethodWithErrorCustomPkgErrorNameResponseBody(res *custom.CustomError) *
return body
}
`

const BodyCustomNameServerTypesFile = `// MethodBodyCustomNameRequestBody is the type of the "ServiceBodyCustomName"
// service "MethodBodyCustomName" endpoint HTTP request body.
type MethodBodyCustomNameRequestBody struct {
Body *string ` + "`" + `form:"b,omitempty" json:"b,omitempty" xml:"b,omitempty"` + "`" + `
}
// NewMethodBodyCustomNamePayload builds a ServiceBodyCustomName service
// MethodBodyCustomName endpoint payload.
func NewMethodBodyCustomNamePayload(body *MethodBodyCustomNameRequestBody) *servicebodycustomname.MethodBodyCustomNamePayload {
v := &servicebodycustomname.MethodBodyCustomNamePayload{
Body: body.Body,
}
return v
}
`
const PathCustomNameServerTypesFile = `// NewMethodPathCustomNamePayload builds a ServicePathCustomName service
// MethodPathCustomName endpoint payload.
func NewMethodPathCustomNamePayload(p string) *servicepathcustomname.MethodPathCustomNamePayload {
v := &servicepathcustomname.MethodPathCustomNamePayload{}
v.Path = p
return v
}
`
const QueryCustomNameServerTypesFile = `// NewMethodQueryCustomNamePayload builds a ServiceQueryCustomName service
// MethodQueryCustomName endpoint payload.
func NewMethodQueryCustomNamePayload(q *string) *servicequerycustomname.MethodQueryCustomNamePayload {
v := &servicequerycustomname.MethodQueryCustomNamePayload{}
v.Query = q
return v
}
`

const HeaderCustomNameServerTypesFile = `// NewMethodHeaderCustomNamePayload builds a ServiceHeaderCustomName service
// MethodHeaderCustomName endpoint payload.
func NewMethodHeaderCustomNamePayload(h *string) *serviceheadercustomname.MethodHeaderCustomNamePayload {
v := &serviceheadercustomname.MethodHeaderCustomNamePayload{}
v.Header = h
return v
}
`

const CookieCustomNameServerTypesFile = `// NewMethodCookieCustomNamePayload builds a ServiceCookieCustomName service
// MethodCookieCustomName endpoint payload.
func NewMethodCookieCustomNamePayload(c2 *string) *servicecookiecustomname.MethodCookieCustomNamePayload {
v := &servicecookiecustomname.MethodCookieCustomNamePayload{}
v.Cookie = c2
return v
}
`
8 changes: 4 additions & 4 deletions http/codegen/service_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,7 @@ func extractPathParams(a *expr.MappedAttributeExpr, service *expr.AttributeExpr,

fptr bool
)
fieldName := codegen.Goify(name, true)
fieldName := codegen.GoifyAtt(c, name, true)
if !expr.IsObject(service.Type) {
fieldName = ""
} else {
Expand Down Expand Up @@ -2363,7 +2363,7 @@ func extractQueryParams(a *expr.MappedAttributeExpr, service *expr.AttributeExpr
if pointer {
typeRef = "*" + typeRef
}
fieldName := codegen.Goify(name, true)
fieldName := codegen.GoifyAtt(c, name, true)
if !expr.IsObject(service.Type) {
fieldName = ""
} else {
Expand Down Expand Up @@ -2435,7 +2435,7 @@ func extractHeaders(a *expr.MappedAttributeExpr, svcAtt *expr.AttributeExpr, svc
{
pointer = a.IsPrimitivePointer(name, true)
if expr.IsObject(svcAtt.Type) {
fieldName = codegen.Goify(name, true)
fieldName = codegen.GoifyAtt(attr, name, true)
fptr = svcCtx.IsPrimitivePointer(name, svcAtt)
}
if pointer {
Expand Down Expand Up @@ -2494,7 +2494,7 @@ func extractCookies(a *expr.MappedAttributeExpr, svcAtt *expr.AttributeExpr, svc
{
pointer = a.IsPrimitivePointer(name, true)
if expr.IsObject(svcAtt.Type) {
fieldName = codegen.Goify(name, true)
fieldName = codegen.GoifyAtt(hattr, name, true)
fptr = svcCtx.IsPrimitivePointer(name, svcAtt)
ft = svcAtt.Find(name).Type
}
Expand Down
81 changes: 81 additions & 0 deletions http/codegen/testdata/parse_endpoint_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,3 +1374,84 @@ func BuildMethodAPayload(serviceWithParamsAndHeadersBlockMethodABody string, ser
return v, nil
}
`

var PayloadBodyCustomNameBuildCode = `// BuildMethodBodyCustomNamePayload builds the payload for the
// ServiceBodyCustomName MethodBodyCustomName endpoint from CLI flags.
func BuildMethodBodyCustomNamePayload(serviceBodyCustomNameMethodBodyCustomNameBody string) (*servicebodycustomname.MethodBodyCustomNamePayload, error) {
var err error
var body MethodBodyCustomNameRequestBody
{
err = json.Unmarshal([]byte(serviceBodyCustomNameMethodBodyCustomNameBody), &body)
if err != nil {
return nil, fmt.Errorf("invalid JSON for body, \nerror: %s, \nexample of valid JSON:\n%s", err, "'{\n \"b\": \"Doloribus qui quia.\"\n }'")
}
}
v := &servicebodycustomname.MethodBodyCustomNamePayload{
Body: body.Body,
}
return v, nil
}
`

var PayloadPathCustomNameBuildCode = `// BuildMethodPathCustomNamePayload builds the payload for the
// ServicePathCustomName MethodPathCustomName endpoint from CLI flags.
func BuildMethodPathCustomNamePayload(servicePathCustomNameMethodPathCustomNameP string) (*servicepathcustomname.MethodPathCustomNamePayload, error) {
var p string
{
p = servicePathCustomNameMethodPathCustomNameP
}
v := &servicepathcustomname.MethodPathCustomNamePayload{}
v.Path = p
return v, nil
}
`

var PayloadQueryCustomNameBuildCode = `// BuildMethodQueryCustomNamePayload builds the payload for the
// ServiceQueryCustomName MethodQueryCustomName endpoint from CLI flags.
func BuildMethodQueryCustomNamePayload(serviceQueryCustomNameMethodQueryCustomNameQ string) (*servicequerycustomname.MethodQueryCustomNamePayload, error) {
var q *string
{
if serviceQueryCustomNameMethodQueryCustomNameQ != "" {
q = &serviceQueryCustomNameMethodQueryCustomNameQ
}
}
v := &servicequerycustomname.MethodQueryCustomNamePayload{}
v.Query = q
return v, nil
}
`

var PayloadHeaderCustomNameBuildCode = `// BuildMethodHeaderCustomNamePayload builds the payload for the
// ServiceHeaderCustomName MethodHeaderCustomName endpoint from CLI flags.
func BuildMethodHeaderCustomNamePayload(serviceHeaderCustomNameMethodHeaderCustomNameH string) (*serviceheadercustomname.MethodHeaderCustomNamePayload, error) {
var h *string
{
if serviceHeaderCustomNameMethodHeaderCustomNameH != "" {
h = &serviceHeaderCustomNameMethodHeaderCustomNameH
}
}
v := &serviceheadercustomname.MethodHeaderCustomNamePayload{}
v.Header = h
return v, nil
}
`

var PayloadCookieCustomNameBuildCode = `// BuildMethodCookieCustomNamePayload builds the payload for the
// ServiceCookieCustomName MethodCookieCustomName endpoint from CLI flags.
func BuildMethodCookieCustomNamePayload(serviceCookieCustomNameMethodCookieCustomNameC2 string) (*servicecookiecustomname.MethodCookieCustomNamePayload, error) {
var c2 *string
{
if serviceCookieCustomNameMethodCookieCustomNameC2 != "" {
c2 = &serviceCookieCustomNameMethodCookieCustomNameC2
}
}
v := &servicecookiecustomname.MethodCookieCustomNamePayload{}
v.Cookie = c2
return v, nil
}
`
Loading

0 comments on commit 79317b6

Please sign in to comment.