Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Given repeat TypeNames Goa generates invalid code #3799

@MichaelUrman

Description

@MichaelUrman

It was an incorrect design that caused this, but instead of issuing an error during goa gen, it generated conflicting type definitions.

Tested with v3.22.1

% go list -m goa.design/goa/v3
goa.design/goa/v3 v3.22.1
package design

import . "goa.design/goa/v3/dsl"

var _ = API("MyAPI", func() {})

var _ = Service("MyService", func() {
    Method("a", func() {
        Payload(Empty)
        Result(TypeA)
    })
    Method("b", func() {
        Payload(Empty)
        Result(TypeB)
    })
})

var TypeA = ResultType("application/vnd.example.com.a", func() {
    TypeName("A")
    Attribute("a", Int)
})

var TypeB = ResultType("application/vnd.example.com.b", func() {
    TypeName("A") // ❌ mistake
    Attribute("b", String)
})

Among other problems this yields the following. Note the two definitions of type AView, newA and newAView.

% grep -R type\ A
gen/my_service/views/view.go:type A struct {
gen/my_service/views/view.go:type AView struct {
gen/my_service/views/view.go:type AView struct {
gen/my_service/views/view.go:// ValidateA runs the validations defined on the viewed result type A.
gen/my_service/service.go:type A struct {
gen/my_service/service.go:// NewA initializes result type A from viewed result type A.
gen/my_service/service.go:// NewViewedA initializes viewed result type A from result type A using the
gen/my_service/service.go:// newA converts projected type A to service type A.
gen/my_service/service.go:// newAView projects result type A to projected type AView using the "default"
gen/my_service/service.go:// newA converts projected type A to service type A.
gen/my_service/service.go:// newAView projects result type A to projected type AView using the "default"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions