Skip to content

Make OptFunc Public to Allow Dynamic and Conditional Options (#9) #10

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

Merged
merged 1 commit into from
Apr 1, 2025

Conversation

MuhammadSaim
Copy link
Owner

This PR addresses Issue #9 by making the optFunc type public as OptFunc. This change enables users to create and apply functional options dynamically and conditionally at runtime.

Changes Introduced

✅ Renamed optFuncOptFunc to make it publicly accessible.
✅ Allows users to define and apply their own functional options.
✅ Ensures backward compatibility with existing option functions.
✅ Improves flexibility in configuring avatar options dynamically.

Why This Change?

Currently, users can only apply predefined options, limiting customization. With this change:

  • Users can define their own option functions.
  • Options can be applied conditionally at runtime based on logic.
  • The functional options pattern becomes fully extensible.

Example usage

var opts []avatar.OptFunc

	// Get size from query and apply if valid
	if sizeStr := r.URL.Query().Get("size"); sizeStr != "" {
		if size, err := strconv.Atoi(sizeStr); err == nil && size > 0 {
			opts = append(opts, avatar.WithSize(size))
		}
	}

	// Get color from query and apply if valid
	if colorStr := r.URL.Query().Get("color"); colorStr != "" {
		switch colorStr {
		case "red":
			opts = append(opts, avatar.WithFgColor(color.RGBA{255, 0, 0, 255}))
		case "blue":
			opts = append(opts, avatar.WithFgColor(color.RGBA{0, 0, 255, 255}))
		case "green":
			opts = append(opts, avatar.WithFgColor(color.RGBA{0, 255, 0, 255}))
		}
	}

- Renamed `optFunc` to `OptFunc` to make it publicly accessible.
- Enables users to define and apply custom functional options.
- Allows dynamic and conditional configuration of `Options`.
- Ensures backward compatibility with existing option functions.
- Improves flexibility for customizing avatar settings.

Closes #9
@MuhammadSaim MuhammadSaim merged commit 354a367 into master Apr 1, 2025
@MuhammadSaim MuhammadSaim deleted the feature/public-options-config branch April 2, 2025 16:19
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.

1 participant