Skip to content

Commit

Permalink
only generate routes for partials or pages that have *h.RequestContex…
Browse files Browse the repository at this point in the history
…t as a param
  • Loading branch information
maddalax committed Nov 11, 2024
1 parent 6ec582a commit dc8a623
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
30 changes: 28 additions & 2 deletions cli/htmgo/tasks/astgen/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ func sliceCommonPrefix(dir1, dir2 string) string {
return normalizePath(slicedDir2)
}

func hasOnlyReqContextParam(funcType *ast.FuncType) bool {
if len(funcType.Params.List) != 1 {
return false
}
if funcType.Params.List[0].Names == nil {
return false
}
if len(funcType.Params.List[0].Names) != 1 {
return false
}
t := funcType.Params.List[0].Type
name, ok := t.(*ast.StarExpr)
if !ok {
return false
}
selectorExpr, ok := name.X.(*ast.SelectorExpr)
if !ok {
return false
}
ident, ok := selectorExpr.X.(*ast.Ident)
if !ok {
return false
}
return ident.Name == "h" && selectorExpr.Sel.Name == "RequestContext"
}

func findPublicFuncsReturningHPartial(dir string, predicate func(partial Partial) bool) ([]Partial, error) {
var partials []Partial
cwd := process.GetWorkingDir()
Expand Down Expand Up @@ -136,7 +162,7 @@ func findPublicFuncsReturningHPartial(dir string, predicate func(partial Partial
if selectorExpr, ok := starExpr.X.(*ast.SelectorExpr); ok {
// Check if the package name is 'h' and type is 'Partial'.
if ident, ok := selectorExpr.X.(*ast.Ident); ok && ident.Name == "h" {
if selectorExpr.Sel.Name == "Partial" {
if selectorExpr.Sel.Name == "Partial" && hasOnlyReqContextParam(funcDecl.Type) {
p := Partial{
Package: node.Name.Name,
Path: normalizePath(sliceCommonPrefix(cwd, path)),
Expand Down Expand Up @@ -203,7 +229,7 @@ func findPublicFuncsReturningHPage(dir string) ([]Page, error) {
if selectorExpr, ok := starExpr.X.(*ast.SelectorExpr); ok {
// Check if the package name is 'h' and type is 'Partial'.
if ident, ok := selectorExpr.X.(*ast.Ident); ok && ident.Name == "h" {
if selectorExpr.Sel.Name == "Page" {
if selectorExpr.Sel.Name == "Page" && hasOnlyReqContextParam(funcDecl.Type) {
pages = append(pages, Page{
Package: node.Name.Name,
Import: normalizePath(filepath.Dir(path)),
Expand Down
7 changes: 7 additions & 0 deletions cli/htmgo/tasks/astgen/project-sample/partials/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ func CountersPartial(ctx *h.RequestContext) *h.Partial {
),
)
}

func SwapFormError(ctx *h.RequestContext, error string) *h.Partial {
return h.SwapPartial(
ctx,
h.Div(),
)
}

0 comments on commit dc8a623

Please sign in to comment.