Skip to content

Commit

Permalink
refactor(runtime): add comments to builder methods and interfaces
Browse files Browse the repository at this point in the history
- Added comments to methods in build.go
- Added comments to interfaces and methods in build_config.go
- Added comments to interfaces and methods in build_registry.go
- Added comment to New method in runtime.go
  • Loading branch information
godcong committed Nov 11, 2024
1 parent 2cbaf67 commit db2fd58
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions runtime/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type builder struct {
registryMux sync.RWMutex
}

// NewConfig creates a new Config object based on the given SourceConfig and options.
func (b *builder) NewConfig(cfg *config.SourceConfig, opts ...config.Option) (config.Config, error) {
b.configMux.RLock()
defer b.configMux.RUnlock()
Expand All @@ -30,6 +31,7 @@ func (b *builder) NewConfig(cfg *config.SourceConfig, opts ...config.Option) (co
return configBuilder.NewConfig(cfg, opts...)
}

// NewRegistrar creates a new Registrar object based on the given RegistryConfig.
func (b *builder) NewRegistrar(cfg *config.RegistryConfig) (registry.Registrar, error) {
b.registryMux.RLock()
defer b.registryMux.RUnlock()
Expand All @@ -40,6 +42,7 @@ func (b *builder) NewRegistrar(cfg *config.RegistryConfig) (registry.Registrar,
return registryBuilder.NewRegistrar(cfg)
}

// NewDiscovery creates a new Discovery object based on the given RegistryConfig.
func (b *builder) NewDiscovery(cfg *config.RegistryConfig) (registry.Discovery, error) {
b.registryMux.RLock()
defer b.registryMux.RUnlock()
Expand All @@ -50,22 +53,26 @@ func (b *builder) NewDiscovery(cfg *config.RegistryConfig) (registry.Discovery,
return registryBuilder.NewDiscovery(cfg)
}

// RegisterConfig registers a new ConfigBuilder with the given name.
func (b *builder) RegisterConfig(name string, configBuilder ConfigBuilder) {
b.configMux.Lock()
defer b.configMux.Unlock()
build.configs[name] = configBuilder
}

// RegisterConfigFunc registers a new ConfigBuilder with the given name and function.
func (b *builder) RegisterConfigFunc(name string, configBuilder ConfigBuildFunc) {
b.RegisterConfig(name, configBuilder)
}

// RegisterRegistry registers a new RegistryBuilder with the given name.
func (b *builder) RegisterRegistry(name string, registryBuilder RegistryBuilder) {
b.registryMux.Lock()
defer b.registryMux.Unlock()
build.registries[name] = registryBuilder
}

// RegisterRegistryFunc registers a new RegistryBuilder with the given name and functions.
func (b *builder) RegisterRegistryFunc(name string, registryBuilder RegistrarBuildFunc, discoveryBuilder DiscoveryBuildFunc) {
b.RegisterRegistry(name, &registryWrap{
RegistrarBuildFunc: registryBuilder,
Expand Down
6 changes: 6 additions & 0 deletions runtime/build_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ import (
"github.com/origadmin/toolkits/runtime/config"
)

// configBuildRegistry is an interface that defines a method for registering a config builder.
type configBuildRegistry interface {
// RegisterConfig registers a config builder with the given name.
RegisterConfig(name string, configBuilder ConfigBuilder)
}

// ConfigBuilder is an interface that defines a method for creating a new config.
type ConfigBuilder interface {
// NewConfig creates a new config using the given SourceConfig and a list of Options.
NewConfig(cfg *config.SourceConfig, opts ...config.Option) (config.Config, error)
}

// ConfigBuildFunc is a function type that takes a SourceConfig and a list of Options and returns a Config and an error.
type ConfigBuildFunc func(*config.SourceConfig, ...config.Option) (config.Config, error)

// NewConfig is a method that implements the ConfigBuilder interface for ConfigBuildFunc.
func (fn ConfigBuildFunc) NewConfig(cfg *config.SourceConfig, opts ...config.Option) (config.Config, error) {
// Call the function with the given SourceConfig and a list of Options.
return fn(cfg, opts...)
}
7 changes: 7 additions & 0 deletions runtime/build_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/origadmin/toolkits/runtime/registry"
)

// registryBuildRegistry is an interface that defines a method for registering a RegistryBuilder.
type registryBuildRegistry interface {
RegisterRegistry(name string, registryBuilder RegistryBuilder)
}
Expand All @@ -20,21 +21,27 @@ type RegistryBuilder interface {
NewDiscovery(cfg *config.RegistryConfig) (registry.Discovery, error)
}

// RegistrarBuildFunc is a function type that takes a *config.RegistryConfig and returns a registry.Registrar and an error.
type RegistrarBuildFunc func(cfg *config.RegistryConfig) (registry.Registrar, error)

// NewRegistrar is a method that calls the RegistrarBuildFunc with the given config.
func (fn RegistrarBuildFunc) NewRegistrar(cfg *config.RegistryConfig) (registry.Registrar, error) {
return fn(cfg)
}

// DiscoveryBuildFunc is a function type that takes a *config.RegistryConfig and returns a registry.Discovery and an error.
type DiscoveryBuildFunc func(cfg *config.RegistryConfig) (registry.Discovery, error)

// NewDiscovery is a method that calls the DiscoveryBuildFunc with the given config.
func (fn DiscoveryBuildFunc) NewDiscovery(cfg *config.RegistryConfig) (registry.Discovery, error) {
return fn(cfg)
}

// registryWrap is a struct that embeds RegistrarBuildFunc and DiscoveryBuildFunc.
type registryWrap struct {
RegistrarBuildFunc
DiscoveryBuildFunc
}

// _ is a blank identifier that is used to satisfy the interface requirement for RegistryBuilder.
var _ RegistryBuilder = &registryWrap{}
1 change: 1 addition & 0 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewRegistrar(cfg *config.RegistryConfig) (registry.Registrar, error) {
return build.NewRegistrar(cfg)
}

// New creates a new Builder.
func New() Builder {
b := &builder{}
b.init()
Expand Down

0 comments on commit db2fd58

Please sign in to comment.