-
Notifications
You must be signed in to change notification settings - Fork 23
Add Text Templates exercise templates and tests (#79) - Hacktoberfest Contribution #146
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughAdds a new "Text Templates" exercise: updates the exercises catalog, provides template utilities ( Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Tester
participant Utils as TemplateUtils
participant Tmpl as text/template
participant Buf as bytes.Buffer
Note over Tester,Utils: FormatUserGreeting flow
Tester->>Utils: FormatUserGreeting(name)
Utils->>Utils: data := {"Name": name}
Utils->>Utils: RenderTemplate("Hello, {{.Name}}!", data)
Utils->>Tmpl: Parse(template string)
Tmpl-->>Utils: template / error
Utils->>Buf: Execute(template, data)
Buf-->>Utils: rendered string / error
Utils-->>Tester: rendered string / error
Note over Tester,Utils: Generic RenderTemplate flow
Tester->>Utils: RenderTemplate(tmplStr, data)
Utils->>Tmpl: Parse(tmplStr)
Tmpl-->>Utils: template / error
Utils->>Buf: Execute(template, data)
Buf-->>Utils: rendered string / error
Utils-->>Tester: rendered string / error
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used🧬 Code graph analysis (1)internal/exercises/solutions/40_text_templates/text_templates.go (1)
🔇 Additional comments (3)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
internal/exercises/templates/40_text_templates/text_templates_test.go (1)
7-33: Consider adding test cases for error scenarios.The current tests cover happy paths well. For a more comprehensive test suite that better demonstrates the text/template package's behavior, consider adding tests for:
- Invalid template syntax (e.g.,
"{{.MissingField}}"with data that lacks the field)- Template execution errors (e.g., calling methods on nil values)
- Edge cases like empty names or empty template strings
These additional tests would strengthen the exercise's educational value by showing learners how to handle template errors properly.
Example test for invalid template:
func TestRenderTemplate_InvalidSyntax(t *testing.T) { tmpl := "Hello {{.Name" // Missing closing braces data := map[string]interface{}{"Name": "Alice"} _, err := RenderTemplate(tmpl, data) if err == nil { t.Error("Expected error for invalid template syntax, got nil") } }internal/exercises/templates/40_text_templates/text_templates.go (1)
9-9: Consider usinganyinstead ofinterface{}.Since Go 1.18,
anyis an alias forinterface{}and is the preferred, more concise way to express an empty interface. Whileinterface{}remains valid, usinganyaligns with modern Go conventions.Apply this diff to line 9:
-func RenderTemplate(tmplStr string, data map[string]interface{}) (string, error) { +func RenderTemplate(tmplStr string, data map[string]any) (string, error) {And this diff to line 27:
- data := map[string]interface{}{ + data := map[string]any{Also applies to: 27-27
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
internal/exercises/catalog.yaml(1 hunks)internal/exercises/templates/40_text_templates/text_templates.go(1 hunks)internal/exercises/templates/40_text_templates/text_templates_test.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
internal/exercises/templates/40_text_templates/text_templates_test.go (1)
internal/exercises/templates/40_text_templates/text_templates.go (2)
RenderTemplate(9-22)FormatUserGreeting(25-31)
🔇 Additional comments (3)
internal/exercises/catalog.yaml (1)
254-261: LGTM! Well-structured catalog entry.The new Text Templates exercise entry is properly formatted and follows the established pattern. The hints are informative and will guide learners effectively through the text/template package basics.
internal/exercises/templates/40_text_templates/text_templates.go (2)
10-21: LGTM! Clean implementation.The template parsing and execution logic is correct and follows best practices. Error handling is appropriate, and using a buffer for template execution is the right approach.
26-30: LGTM! Good use of composition.The function correctly delegates to
RenderTemplate, demonstrating good code reuse. The template string and data structure are appropriate for the greeting use case.
internal/exercises/templates/40_text_templates/text_templates.go
Outdated
Show resolved
Hide resolved
internal/exercises/templates/40_text_templates/text_templates.go
Outdated
Show resolved
Hide resolved
|
Hii @zhravan, also added solution for Text Templates exercises. |
Summary
Describe the change and its motivation.
This PR adds the Text Templates exercise templates and corresponding tests under internal/exercises/templates/40_text_templates, as well as updates the catalog.yaml to include this new exercise. The exercise focuses on using Go's text/template package to create and execute templates, including injecting data and formatting output.
This contribution is part of Hacktoberfest 2025.
Checklist
make verifyorgolearn verify <slug>Screenshots / Output (if CLI UX)
Paste before/after where helpful.
Related issues
Fixes #79
Summary by CodeRabbit
New Features
Tests