Skip to content

Commit 78b2e67

Browse files
committed
docs: fix examples
1 parent e0a92e0 commit 78b2e67

File tree

2 files changed

+41
-46
lines changed

2 files changed

+41
-46
lines changed

docs/examplegen/main.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -630,35 +630,35 @@ func writeMain(base string, fd *FuncDoc, moduleImportPath, importPath string) er
630630
}
631631

632632
buf.WriteString("func main() {\n")
633-
for i := range fd.Examples {
634-
buf.WriteString(fmt.Sprintf("\texample%d()\n", i+1))
633+
if fd.Description != "" {
634+
for _, line := range strings.Split(fd.Description, "\n") {
635+
buf.WriteString("\t// " + line + "\n")
636+
}
637+
buf.WriteString("\n")
635638
}
636-
buf.WriteString("}\n\n")
637639

638640
for i, ex := range fd.Examples {
639-
buf.WriteString(fmt.Sprintf("func example%d() {\n", i+1))
640-
641-
if i == 0 && fd.Description != "" {
642-
for _, line := range strings.Split(fd.Description, "\n") {
643-
buf.WriteString("\t// " + line + "\n")
644-
}
641+
if i > 0 {
645642
buf.WriteString("\n")
646643
}
647-
648644
if ex.Label != "" {
649645
buf.WriteString("\t// Example: " + ex.Label + "\n")
650646
}
647+
// Isolate each example body in an IIFE so snippets can reuse names and
648+
// use `return` without exiting the whole generated main().
649+
buf.WriteString("\tfunc() {\n")
651650

652651
ex.Code = strings.TrimLeft(ex.Code, "\n")
653652
for _, line := range strings.Split(ex.Code, "\n") {
654653
if strings.TrimSpace(line) == "" {
655654
buf.WriteString("\n")
656655
} else {
657-
buf.WriteString("\t" + line + "\n")
656+
buf.WriteString("\t\t" + line + "\n")
658657
}
659658
}
660-
buf.WriteString("}\n\n")
659+
buf.WriteString("\t}()\n")
661660
}
661+
buf.WriteString("}\n")
662662

663663
return os.WriteFile(filepath.Join(dir, "main.go"), buf.Bytes(), 0o644)
664664
}

examples/job-payload/main.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,41 @@ package main
88
import "github.com/goforj/queue"
99

1010
func main() {
11-
example1()
12-
example2()
13-
example3()
14-
}
15-
16-
func example1() {
1711
// Payload sets job payload from common value types.
1812

1913
// Example: payload bytes
20-
jobBytes := queue.NewJob("emails:send").Payload([]byte(`{"id":1}`))
21-
_ = jobBytes
14+
func() {
15+
jobBytes := queue.NewJob("emails:send").Payload([]byte(`{"id":1}`))
16+
_ = jobBytes
2217

23-
}
18+
}()
2419

25-
func example2() {
2620
// Example: payload struct
27-
type Meta struct {
28-
Nested bool `json:"nested"`
29-
}
30-
type EmailPayload struct {
31-
ID int `json:"id"`
32-
To string `json:"to"`
33-
Meta Meta `json:"meta"`
34-
}
35-
jobStruct := queue.NewJob("emails:send").Payload(EmailPayload{
36-
ID: 1,
37-
To: "user@example.com",
38-
Meta: Meta{Nested: true},
39-
})
40-
_ = jobStruct
21+
func() {
22+
type Meta struct {
23+
Nested bool `json:"nested"`
24+
}
25+
type EmailPayload struct {
26+
ID int `json:"id"`
27+
To string `json:"to"`
28+
Meta Meta `json:"meta"`
29+
}
30+
jobStruct := queue.NewJob("emails:send").Payload(EmailPayload{
31+
ID: 1,
32+
To: "user@example.com",
33+
Meta: Meta{Nested: true},
34+
})
35+
_ = jobStruct
36+
37+
}()
4138

42-
}
43-
44-
func example3() {
4539
// Example: payload map
46-
jobMap := queue.NewJob("emails:send").Payload(map[string]any{
47-
"id": 1,
48-
"to": "user@example.com",
49-
"meta": map[string]any{"nested": true},
50-
})
51-
_ = jobMap
40+
func() {
41+
jobMap := queue.NewJob("emails:send").Payload(map[string]any{
42+
"id": 1,
43+
"to": "user@example.com",
44+
"meta": map[string]any{"nested": true},
45+
})
46+
_ = jobMap
47+
}()
5248
}
53-

0 commit comments

Comments
 (0)