Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define a ResultType for Cycle struct? #2514

Closed
JermineHu opened this issue Mar 20, 2020 · 3 comments · Fixed by #2517
Closed

How to define a ResultType for Cycle struct? #2514

JermineHu opened this issue Mar 20, 2020 · 3 comments · Fixed by #2517

Comments

@JermineHu
Copy link

My first time try code

My DSL

var CategoryResult = ResultType("application/vnd.category", func() {
	Description("category")
	Attributes(func() {
		Field(1, "id", Int, "ID")
		Field(2, "p_category_id", Int, "p_category_id")
		Field(3, "children_category", CollectionOf("application/vnd.category"), "children_category")
		Field(4, "name", String, "name")
	})

	View("default", func() {
		Field(1, "id")
		Field(2, "p_category_id")
		Field(3, "name")
	})

	View("full", func() {
		Field(1, "id")
		Field(2, "p_category_id")
		Field(3, "name")
	})
})

The error info

invalid CollectionOf argument: not a result type and not a known result type identifier in attribute

My second time try code

My DSL

var CategoryResult = ResultType("application/vnd.category", func() {
	Description("category")
	Attributes(func() {
		Field(1, "id", Int, "ID")
		Field(2, "p_category_id", Int, "p_category_id")
		Field(3, "children_category", CollectionOf(CategoryResult), "children_category")
		Field(4, "name", String, "name")
	})

	View("default", func() {
		Field(1, "id")
		Field(2, "p_category_id")
		Field(3, "name")
	})

	View("full", func() {
		Field(1, "id")
		Field(2, "p_category_id")
		Field(3, "name")
	})
})

The error info

 initialization loop:
        _resources.go:26:5: CategoryResult refers to
       _resources.go:28:13: glob..func39.1 refers to
        _resources.go:26:5: CategoryResult

My question

How to define a struct like the follow code in goa by DSL ?

type Category  struct {
	ID int `json:"id"`
	PCategoryID int `json:"p_category_id"`
	ChildrenCategory []Category `json:"children_category"`
	Name string `json:"name"`
}
@nitinmohan87
Copy link
Contributor

@JermineHu To define cyclic attributes here is what you need to do

var CategoryResult = ResultType("application/vnd.category", func() {
  TypeName("CategoryResult")   <--- Set a type name for your type
  Attributes(func() {
    Field(1, "id", Int, "ID")
    Field(3, "children_category", ArrayOf("CategoryResult"))  <---- Put the type name in quotes
    Field(4, "name", String, "name")
  })
})

This above DSL will create a type like this in the generated service code

// CategoryResult is the result type of the calc service addurd method.
type CategoryResult struct {
  // ID
  ID               *int
  ChildrenCategory []*CategoryResult
  // name
  Name *string
}

Is this what you were trying to achieve?

If you are using this type on a HTTP endpoint you should be good to go. However, if you are using it on a GRPC endpoint there seems to be a bug in Goa which I have filed separately here - #2515

@nitinmohan87
Copy link
Contributor

nitinmohan87 commented Mar 24, 2020

You did find another bug when trying to define cyclic types using CollectionOf. That should be fixed in this PR.
So you can now define using CollectionOf as shown below

var CategoryResult = ResultType("application/vnd.category", func() {
  TypeName("CategoryResult")
  Attributes(func() {
    Field(1, "id", Int, "ID")
    // this - Field(3, "children_category", CollectionOf("CategoryResult"))
    // or
    // this - Field(3, "children_category", CollectionOf("application/vnd.category"))
    Field(4, "name", String, "name")
  })
})

@JermineHu
Copy link
Author

@nitinmohan87 Thank you for your contribution. Your code solved my problem in HTTP endpoints. Look forward to issue #2515 be solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants