Skip to content

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.

2 participants