Skip to main content
go-swarm-icons
On this page

Config Builder

Documents every Config method, from provider registration through the final Build call.

The fluent builder for constructing an IconManager with multiple providers, attribute defaults, aliases, and fallbacks.

Overview

Config is a fluent builder for constructing an IconManager. Chain configuration methods and call Build to produce a fully configured manager.

For guides on the features configured through the builder, see Aliases & Fallbacks and Attribute Management.

Types

Config

Go
type Config struct {
// unexported fields
}

Functions

NewConfig

Go
func NewConfig() *Config

Creates an empty Config ready for method chaining.

Methods

All methods return *Config for chaining unless noted otherwise.

Provider registration

AddProvider

Go
func (c *Config) AddProvider(prefix string, provider Provider) *Config

Registers a pre-built provider under prefix. Immediate: the provider is used as-is during Build.

AddDirectory

Go
func (c *Config) AddDirectory(prefix, dir string) *Config

Registers a DirectoryProvider for dir under prefix. Deferred: the provider is constructed during Build. Returns an error from Build if the directory is invalid.

AddJsonCollection

Go
func (c *Config) AddJsonCollection(prefix, path string) *Config

Registers a JsonCollectionProvider for the JSON file at path under prefix. Deferred: the file is read during Build. Returns an error from Build if the file cannot be read.

AddIconifySet

Go
func (c *Config) AddIconifySet(prefix string) *Config

Registers an IconifyProvider under prefix. Deferred: the provider is constructed during Build.

AddHybridSet

Go
func (c *Config) AddHybridSet(prefix, dir string) *Config

Registers a ChainProvider combining a DirectoryProvider (for dir) and an IconifyProvider, both under prefix. The directory is checked first. Deferred: both providers are constructed during Build. Returns an error from Build if the directory is invalid.

DiscoverJsonSets

Go
func (c *Config) DiscoverJsonSets(dir string) *Config

Scans dir for *.json files and registers each as a JsonCollectionProvider using the filename (without extension) as the prefix. Files are sorted alphabetically. Silently skips the call if the glob fails. Deferred.

Attribute configuration

DefaultPrefix

Go
func (c *Config) DefaultPrefix(prefix string) *Config

Sets the prefix used when Get receives a bare name with no colon.

DefaultAttributes

Go
func (c *Config) DefaultAttributes(attrs map[string]string) *Config

Sets attributes applied globally to every icon at layer 2 (after the icon's own attributes). Calling this method multiple times is additive: later calls merge into earlier ones.

PrefixAttributes

Go
func (c *Config) PrefixAttributes(prefix string, attrs map[string]string) *Config

Sets attributes applied to all icons from prefix at layer 3. Calling multiple times for the same prefix is additive.

PrefixSuffix

Go
func (c *Config) PrefixSuffix(prefix, suffix string, attrs map[string]string) *Config

Sets attributes applied at layer 4 to icons under prefix whose name ends with "-{suffix}". Pass an empty suffix as a catch-all for icons under that prefix that match no other suffix.

Resolution configuration

Alias

Go
func (c *Config) Alias(alias, target string) *Config

Maps alias to target. When Get(alias) is called, the target name is substituted before resolution.

FallbackIcon

Go
func (c *Config) FallbackIcon(name string) *Config

Sets the global fallback icon name returned when a requested icon is not found.

FallbackIconForPrefix

Go
func (c *Config) FallbackIconForPrefix(prefix, name string) *Config

Sets a fallback icon used only when an icon from prefix is not found. Checked before the global fallback.

IgnoreNotFound

Go
func (c *Config) IgnoreNotFound() *Config

Configures the built manager to return an empty *Icon (no error) instead of ErrIconNotFound or ErrProviderNotFound.

Terminal

Build

Go
func (c *Config) Build() (*IconManager, error)

Constructs and returns an IconManager from the accumulated configuration. Initializes all deferred providers and returns an error if any provider fails to load. The IconRenderer is built from the configured default, prefix, and suffix attributes.

Deferred vs. immediate

Method Construction
AddProvider Immediate: provider passed as-is.
AddDirectory Deferred: NewDirectoryProvider called during Build.
AddJsonCollection Deferred: NewJsonCollectionProvider called during Build.
AddIconifySet Deferred: NewIconifyProvider called during Build.
AddHybridSet Deferred: NewDirectoryProvider + NewIconifyProvider + NewChainProvider during Build.
DiscoverJsonSets Deferred: glob runs immediately, but providers constructed during Build.

Example

Go
import (
swarmicons "github.com/frostybee/go-swarm-icons"
"github.com/frostybee/go-swarm-icons/lucide"
)
manager, err := swarmicons.NewConfig().
AddProvider("lucide", lucide.Provider()).
AddDirectory("custom", "./icons").
AddIconifySet("mdi").
DefaultPrefix("lucide").
DefaultAttributes(map[string]string{
"fill": "none",
"stroke": "currentColor",
"stroke-width": "2",
}).
PrefixAttributes("mdi", map[string]string{
"fill": "currentColor",
}).
Alias("check", "lucide:check-circle").
FallbackIcon("lucide:help-circle").
IgnoreNotFound().
Build()

See also

Edit this page

Last updated: