(goctl:) fix circle import in case new parser (#3750)

Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
kesonan
2023-11-29 19:13:39 +08:00
committed by GitHub
parent c46bcf7e1b
commit 5e63002cf8
4 changed files with 72 additions and 24 deletions

View File

@@ -0,0 +1,31 @@
package importstack
import "errors"
// ErrImportCycleNotAllowed defines an error for circular importing
var ErrImportCycleNotAllowed = errors.New("import cycle not allowed")
// ImportStack a stack of import paths
type ImportStack []string
func New() *ImportStack {
return &ImportStack{}
}
func (s *ImportStack) Push(p string) error {
for _, x := range *s {
if x == p {
return ErrImportCycleNotAllowed
}
}
*s = append(*s, p)
return nil
}
func (s *ImportStack) Pop() {
*s = (*s)[0 : len(*s)-1]
}
func (s *ImportStack) List() []string {
return *s
}