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

go generics cannot parse #1213

Open
medivh-jay opened this issue May 23, 2022 · 5 comments
Open

go generics cannot parse #1213

medivh-jay opened this issue May 23, 2022 · 5 comments

Comments

@medivh-jay
Copy link

Describe the bug
An error occurred while parsing a data structure that contains generics.

ParseComment error in file news.go :unknown field type &ast.IndexListExpr{X:(*ast.SelectorExpr)(0xc00047a750), Lbrack:267, Indices:[]ast.Expr{(*ast.StarExpr)(0xc00047a780), (*ast.SelectorExpr)(0xc00047a798)}, Rbrack:301}

This is my code:

type Filter interface {
	~func(selector *sql.Selector)
}

type query[T any, F Filter] interface {
	Where(ps ...F) T
}

type Pager[T query[T, F], F Filter] struct {
	Rows   uint8                `json:"rows" form:"rows"`
	Page   int                  `json:"page" form:"page"`
	NextID *valuer.StringValuer `json:"next_id" form:"next_id"`
	PrevID *valuer.StringValuer `json:"prev_id" form:"prev_id"`
	query  T
}

type NewsPager struct {
	request.Pager[*entity.NewsQuery, predicate.News]
	Search string `json:"search" form:"search"`
}

and this is my comment:

// @Summary xxx
// @Tags xxx
// @Description xxx
// @Param   data query  request.NewsPager true ""
// @Success 200 {object} response.Response{data=newsResponse} "ok"
// @Router  /api/v1/news [get]

Your swag version
e.g. 1.8.2

Your go version
1.18

Desktop (please complete the following information):

  • OS: Windows 11
@medivh-jay
Copy link
Author

create a project directly using the following code:

package main

type NewsSelector func(selector func())

type Filter interface {
	~func(selector func())
}

type query[T any, F Filter] interface {
	Where(ps ...F) T
}

type Pager[T query[T, F], F Filter] struct {
	Rows   uint8   `json:"rows" form:"rows"`
	Page   int     `json:"page" form:"page"`
	NextID *string `json:"next_id" form:"next_id"`
	PrevID *string `json:"prev_id" form:"prev_id"`
	query  T
}

type String string

func (String) Where(ps ...NewsSelector) String {
	return ""
}

type NewsPager struct {
	Pager[String, NewsSelector]
	Search string `json:"search" form:"search"`
}

// @Summary xxx
// @Tags xxx
// @Description xxx
// @Param   data query  NewsPager true "1"
// @Success 200 {object} string "ok"
// @Router  /api/v1/news [get]
func route() {

}

Then use swag init

@ubogdan
Copy link
Contributor

ubogdan commented May 24, 2022

Unfortunately, we don't have support for Generics now.

@medivh-jay
Copy link
Author

when will generics be supported
Thank you very much for your reply

@ubogdan
Copy link
Contributor

ubogdan commented May 24, 2022

As you may know, swag is an Open Source project. Most of its code comes from community contributions. It's hard to say when I hope we will bring that functionality asap.

@dlaweb
Copy link

dlaweb commented Jun 6, 2022

I also have Parsing issue on golang 1.18 based on the dep "golang.org/x/exp/constraints" yet I don't use generics in my project.

ParseFile error:vendor/golang.org/x/exp/constraints/constraints.go:13:2: illegal character U+007E '~' (and 10 more errors)

This appends on a command: swag init --parseInternal --parseVendor I will remove the vendor parsing to see if its working.

FabianMartin added a commit to FabianMartin/swag that referenced this issue Jul 31, 2022
- get generic field type
- support built in types in structs

refs swaggo#1213
ubogdan pushed a commit that referenced this issue Aug 2, 2022
* feat: add support for nested generics

nested generics support and related tests added

* fix: Multiple usage of same generic generate different definition paths

cache generic definitions by full name

* feat: add support for generic array parameter

- allow usage of arrays as parameter definitions
- tests extended and new body param added to tests

* feat: Add support for generic properties

- get generic field type
- support built in types in structs

refs #1213

* feat: Support custom model names for generics

add prefix to generic model names, to prevent renaming, if name annotation exists

* fix: Check if generic name starts with pkgName

- The first underscore was replaced instead of checking if the generated name even starts with the package name.
- New Tests added to test the name generation
- schema test extended to test the new behavior

* refactor: Apply suggested changes from PR
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

No branches or pull requests

3 participants