Skip to content

Conversation

@Hussain-Tinwala
Copy link
Contributor

@Hussain-Tinwala Hussain-Tinwala commented Oct 5, 2025

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

  • Tests pass: make verify or golearn verify <slug>
  • Docs updated (README/CONTRIBUTING) if needed
  • No large new dependencies

Screenshots / Output (if CLI UX)

Paste before/after where helpful.

Related issues

Fixes #79

Summary by CodeRabbit

  • New Features

    • Added a new beginner-level project: “Text Templates.”
    • Covers text templating, strings, and formatting, with three guided hints on template usage and formatting helpers.
    • Includes a simple greeting example demonstrating template-driven output.
  • Tests

    • Added automated tests for rendering, syntax error handling, missing-field behavior, and greeting formatting (including empty names).

@coderabbitai
Copy link

coderabbitai bot commented Oct 5, 2025

Walkthrough

Adds a new "Text Templates" exercise: updates the exercises catalog, provides template utilities (RenderTemplate, FormatUserGreeting) with a solution and scaffolding, and adds unit tests under internal/exercises/templates/40_text_templates.

Changes

Cohort / File(s) Summary
Catalog entry
internal/exercises/catalog.yaml
Adds a new project entry with slug 40_text_templates, title "Text Templates", difficulty beginner, topics, and three hints about using text/template, template actions, and formatting functions.
Template utilities (scaffold & solution)
internal/exercises/templates/40_text_templates/text_templates.go, internal/exercises/solutions/40_text_templates/text_templates.go
Adds exported functions RenderTemplate(tmplStr string, data map[string]any) (string, error) and FormatUserGreeting(name string) (string, error). Scaffold version returns zero values; solution parses and executes templates using text/template and bytes.Buffer.
Unit tests
internal/exercises/templates/40_text_templates/text_templates_test.go
Adds tests covering successful rendering, template syntax error, missing-field rendering (<no value>), and greeting formatting including empty name case.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

patch, hacktoberfest, hacktoberfest-accepted, hacktoberfest2025

Suggested reviewers

  • zhravan

Poem

I nibble on braces and hop through the stream,
Templates stitched tidy, names glow in my dream.
Rendered and tested, the greeting's in sight—
A rabbit-approved patch, neat and light. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title Check ⚠️ Warning The title includes noise such as the pull request number and a Hacktoberfest mention, making it overly long and less focused on the core change; while it does reference adding templates and tests, it detracts from clarity and conciseness. Please shorten the title to a clear, concise statement of the main change—for example “Add Text Templates exercise and tests”—and remove the PR number and Hacktoberfest reference.
Out of Scope Changes Check ⚠️ Warning The pull request introduces a solution implementation under internal/exercises/solutions/40_text_templates which is not requested by issue #79 and falls outside the specified objectives of adding templates, tests, and catalog entries. Remove the solution file from this PR or update the linked issue objectives to explicitly include solution code as part of the scope.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues Check ✅ Passed The changes satisfy all acceptance criteria from issue #79 by adding the exercise templates under internal/exercises/templates/40_text_templates, providing corresponding tests in the same directory, updating internal/exercises/catalog.yaml with hints, and ensuring no new dependencies are introduced.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 244f8de and cef227e.

📒 Files selected for processing (2)
  • internal/exercises/solutions/40_text_templates/text_templates.go (1 hunks)
  • internal/exercises/templates/40_text_templates/text_templates.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/exercises/templates/40_text_templates/text_templates.go
🧰 Additional context used
🧬 Code graph analysis (1)
internal/exercises/solutions/40_text_templates/text_templates.go (1)
internal/exercises/templates/40_text_templates/text_templates.go (2)
  • RenderTemplate (9-11)
  • FormatUserGreeting (14-16)
🔇 Additional comments (3)
internal/exercises/solutions/40_text_templates/text_templates.go (3)

1-6: LGTM!

Package declaration and imports are appropriate for the template rendering functionality.


8-22: LGTM!

The implementation correctly handles template parsing and execution with proper error handling. Using bytes.Buffer for rendering is the idiomatic approach in Go.


24-31: LGTM!

The function properly delegates to RenderTemplate and follows the DRY principle. The implementation is clean and straightforward, appropriate for an educational exercise solution.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a 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 using any instead of interface{}.

Since Go 1.18, any is an alias for interface{} and is the preferred, more concise way to express an empty interface. While interface{} remains valid, using any aligns 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

📥 Commits

Reviewing files that changed from the base of the PR and between e9dc638 and 55099eb.

📒 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.

@Hussain-Tinwala
Copy link
Contributor Author

Hii @zhravan, also added solution for Text Templates exercises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Concept] Text Templates - add exercise templates

1 participant