pages API

pages

package

API reference for the pages package.

S
struct

plugin

cmd/rfw/plugins/pages/pages.go:13-16
type plugin struct

Methods

Name
Method

Returns

string
func (*plugin) Name() string
{ return "pages" }
Priority
Method

Returns

int
func (*plugin) Priority() int
{ return 0 }
PreBuild
Method

Parameters

Returns

error
func (*plugin) PreBuild(raw json.RawMessage) error
{
	cfg := struct {
		Dir string `json:"dir"`
	}{Dir: "pages"}
	if len(raw) > 0 {
		_ = json.Unmarshal(raw, &cfg)
	}
	p.dir = cfg.Dir
	routes := []struct{ Path, Comp string }{}
	err := filepath.WalkDir(p.dir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			return nil
		}
		if filepath.Ext(path) != ".go" || strings.HasSuffix(d.Name(), "_test.go") || strings.HasSuffix(d.Name(), "_gen.go") {
			return nil
		}
		rel, err := filepath.Rel(p.dir, path)
		if err != nil {
			return err
		}
		r, c := deriveRoute(rel)
		routes = append(routes, struct{ Path, Comp string }{r, c})
		return nil
	})
	if err != nil {
		return err
	}
	if len(routes) == 0 {
		return nil
	}
	var b strings.Builder
	b.WriteString("// Code generated by pages plugin. DO NOT EDIT.\n")
	b.WriteString("package pages\n\n")
	b.WriteString("import \"github.com/rfwlab/rfw/v2/router\"\n\n")
	b.WriteString("func init() {\n")
	for _, r := range routes {
		b.WriteString("\trouter.RegisterRoute(router.Route{Path: \"" + r.Path + "\", Component: " + r.Comp + "})\n")
	}
	b.WriteString("}\n")
	p.file = filepath.Join(p.dir, "routes_gen.go")
	return os.WriteFile(p.file, []byte(b.String()), 0o644)
}
Build
Method

Parameters

Returns

error
func (*plugin) Build(raw json.RawMessage) error
{ return nil }
PostBuild
Method

Parameters

Returns

error
func (*plugin) PostBuild(raw json.RawMessage) error
{
	if p.file != "" {
		_ = os.Remove(p.file)
	}
	return nil
}
ShouldRebuild
Method

Parameters

path string

Returns

bool
func (*plugin) ShouldRebuild(path string) bool
{
	if !strings.HasPrefix(path, p.dir) {
		return false
	}
	return !strings.HasSuffix(path, "_gen.go")
}

Fields

Name Type Description
dir string
file string
F
function

init

cmd/rfw/plugins/pages/pages.go:18-18
func init()

{ plugins.Register(&plugin{}) }
F
function

deriveRoute

Parameters

rel
string

Returns

string
string
cmd/rfw/plugins/pages/pages.go:86-112
func deriveRoute(rel string) (string, string)

{
	rel = strings.TrimSuffix(rel, ".go")
	segments := strings.Split(rel, string(filepath.Separator))
	pathSegs := []string{}
	nameSegs := []string{}
	for i, s := range segments {
		isLast := i == len(segments)-1
		if isLast && s == "index" {
			nameSegs = append(nameSegs, "Index")
			continue
		}
		if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
			param := s[1 : len(s)-1]
			pathSegs = append(pathSegs, ":"+param)
			nameSegs = append(nameSegs, toPascal(param))
		} else {
			pathSegs = append(pathSegs, s)
			nameSegs = append(nameSegs, toPascal(s))
		}
	}
	path := "/" + strings.Join(pathSegs, "/")
	if len(pathSegs) == 0 {
		path = "/"
	}
	comp := strings.Join(nameSegs, "")
	return path, comp
}
F
function

toPascal

Parameters

s
string

Returns

string
cmd/rfw/plugins/pages/pages.go:114-122
func toPascal(s string) string

{
	s = strings.ReplaceAll(s, "-", " ")
	s = strings.ReplaceAll(s, "_", " ")
	parts := strings.Fields(s)
	for i, p := range parts {
		parts[i] = strings.ToUpper(p[:1]) + strings.ToLower(p[1:])
	}
	return strings.Join(parts, "")
}
F
function

TestDeriveRoute

Parameters

cmd/rfw/plugins/pages/pages_test.go:5-19
func TestDeriveRoute(t *testing.T)

{
	tests := map[string]struct{ path, comp string }{
		"index.go":           {"/", "Index"},
		"about.go":           {"/about", "About"},
		"blog/index.go":      {"/blog", "BlogIndex"},
		"posts/[id].go":      {"/posts/:id", "PostsId"},
		"[user]/settings.go": {"/:user/settings", "UserSettings"},
	}
	for in, exp := range tests {
		p, c := deriveRoute(in)
		if p != exp.path || c != exp.comp {
			t.Errorf("%s => (%s,%s), want (%s,%s)", in, p, c, exp.path, exp.comp)
		}
	}
}