Skip to content

Commit

Permalink
refactor(runtime): rename types to avoid conflicts with kratos package
Browse files Browse the repository at this point in the history
- Rename types in config, log, and middleware packages to start with 'K'
- Update related functions and variables to use new type names
- Add prefix to middleware stack to distinguish from kratos middleware
- Update README with information about type naming convention
  • Loading branch information
godcong committed Dec 20, 2024
1 parent 9e7fd7e commit 09502fd
Show file tree
Hide file tree
Showing 26 changed files with 243 additions and 221 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ The `Runtime` controls the resources required when a project is started, includi
monitoring,
caches, and databases.

### Before You Start

Before you start using the Runtime package, ensure that you have the following prerequisites:
In order to prevent import conflicts caused by packages with the same name as `kratos`, packages with the same name in
this database will import the export content from `kratos`.
All type definitions will be prefixed with the `K` fixed prefix.
Note: Only `type` declarations are prefixed, not functions.

### Available Packages

- **[bootstrap](bootstrap)**: The bootstrap package contains Configuration file reading and writing, initialization
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Option struct {
Service *service.Option
Selector *selector.Option
Customize *customize.Option
Source config.SourceOption
Source config.KOption
}

// OptionSetting is a function that takes a pointer to a Option struct and modifies it.
Expand Down
26 changes: 13 additions & 13 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type builder struct {
middlewares map[string]MiddlewareBuilder
}

func (b *builder) NewConfig(sourceConfig *configv1.SourceConfig, setting ...config.OptionSetting) (config.SourceConfig, error) {
func (b *builder) NewConfig(sourceConfig *configv1.SourceConfig, setting ...config.OptionSetting) (config.KConfig, error) {
return b.ConfigBuilder.NewConfig(sourceConfig, setting...)
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func Global() Builder {
}

// NewConfig creates a new Selector using the registered ConfigBuilder.
func NewConfig(cfg *configv1.SourceConfig, ss ...config.OptionSetting) (config.SourceConfig, error) {
func NewConfig(cfg *configv1.SourceConfig, ss ...config.OptionSetting) (config.KConfig, error) {
return build.ConfigBuilder.NewConfig(cfg, ss...)
}

Expand All @@ -106,12 +106,12 @@ func RegisterConfigSync(name string, syncFunc ConfigSyncFunc) {
}

// NewDiscovery creates a new discovery using the registered RegistryBuilder.
func NewDiscovery(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.Discovery, error) {
func NewDiscovery(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.KDiscovery, error) {
return build.NewDiscovery(cfg, ss...)
}

// NewRegistrar creates a new Registrar using the registered RegistryBuilder.
func NewRegistrar(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.Registrar, error) {
// NewRegistrar creates a new KRegistrar using the registered RegistryBuilder.
func NewRegistrar(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.KRegistrar, error) {
return build.NewRegistrar(cfg, ss...)
}

Expand All @@ -120,23 +120,23 @@ func RegisterRegistry(name string, factory registry.Factory) {
build.RegisterRegistryBuilder(name, factory)
}

// NewMiddlewareClient creates a new Middleware with the builder.
func NewMiddlewareClient(name string, cm *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.Middleware, error) {
// NewMiddlewareClient creates a new KMiddleware with the builder.
func NewMiddlewareClient(name string, cm *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.KMiddleware, error) {
return build.NewMiddlewareClient(name, cm, ss...)
}

// NewMiddlewareServer creates a new Middleware with the builder.
func NewMiddlewareServer(name string, cm *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.Middleware, error) {
// NewMiddlewareServer creates a new KMiddleware with the builder.
func NewMiddlewareServer(name string, cm *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.KMiddleware, error) {
return build.NewMiddlewareServer(name, cm, ss...)
}

// NewMiddlewaresClient creates a new Middleware with the builder.
func NewMiddlewaresClient(cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.Middleware {
// NewMiddlewaresClient creates a new KMiddleware with the builder.
func NewMiddlewaresClient(cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.KMiddleware {
return build.NewMiddlewaresClient(nil, cc, ss...)
}

// NewMiddlewaresServer creates a new Middleware with the builder.
func NewMiddlewaresServer(cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.Middleware {
// NewMiddlewaresServer creates a new KMiddleware with the builder.
func NewMiddlewaresServer(cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.KMiddleware {
return build.NewMiddlewaresServer(nil, cc, ss...)
}

Expand Down
4 changes: 2 additions & 2 deletions build_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
configv1 "github.com/origadmin/runtime/gen/go/config/v1"
)

// ConfigSyncFunc is a function type that takes a SourceConfig and a list of Options and returns an error.
// ConfigSyncFunc is a function type that takes a KConfig and a list of Options and returns an error.
type ConfigSyncFunc func(*configv1.SourceConfig, any, ...config.OptionSetting) error

// SyncConfig is a method that implements the ConfigSyncer interface for ConfigSyncFunc.
func (fn ConfigSyncFunc) SyncConfig(cfg *configv1.SourceConfig, v any, ss ...config.OptionSetting) error {
// Call the function with the given SourceConfig and a list of Options.
// Call the function with the given KConfig and a list of Options.
return fn(cfg, v, ss...)
}

Expand Down
26 changes: 13 additions & 13 deletions build_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ type (
// MiddlewareBuilders middleware builders for runtime
MiddlewareBuilders interface {
// NewMiddlewaresClient build middleware
NewMiddlewaresClient([]middleware.Middleware, *configv1.Customize, ...middleware.OptionSetting) []middleware.Middleware
NewMiddlewaresClient([]middleware.KMiddleware, *configv1.Customize, ...middleware.OptionSetting) []middleware.KMiddleware
// NewMiddlewaresServer build middleware
NewMiddlewaresServer([]middleware.Middleware, *configv1.Customize, ...middleware.OptionSetting) []middleware.Middleware
NewMiddlewaresServer([]middleware.KMiddleware, *configv1.Customize, ...middleware.OptionSetting) []middleware.KMiddleware
// NewMiddlewareClient build middleware
NewMiddlewareClient(string, *configv1.Customize_Config, ...middleware.OptionSetting) (middleware.Middleware, error)
NewMiddlewareClient(string, *configv1.Customize_Config, ...middleware.OptionSetting) (middleware.KMiddleware, error)
// NewMiddlewareServer build middleware
NewMiddlewareServer(string, *configv1.Customize_Config, ...middleware.OptionSetting) (middleware.Middleware, error)
NewMiddlewareServer(string, *configv1.Customize_Config, ...middleware.OptionSetting) (middleware.KMiddleware, error)
}

// MiddlewareBuilder middleware builder interface
MiddlewareBuilder interface {
// NewMiddlewareClient build middleware
NewMiddlewareClient(*configv1.Customize_Config, ...middleware.OptionSetting) (middleware.Middleware, error)
NewMiddlewareClient(*configv1.Customize_Config, ...middleware.OptionSetting) (middleware.KMiddleware, error)
// NewMiddlewareServer build middleware
NewMiddlewareServer(*configv1.Customize_Config, ...middleware.OptionSetting) (middleware.Middleware, error)
NewMiddlewareServer(*configv1.Customize_Config, ...middleware.OptionSetting) (middleware.KMiddleware, error)
}

// MiddlewareBuildFunc is an interface that defines methods for creating middleware.
MiddlewareBuildFunc = func(*configv1.Customize_Config, ...middleware.OptionSetting) (middleware.Middleware, error)
MiddlewareBuildFunc = func(*configv1.Customize_Config, ...middleware.OptionSetting) (middleware.KMiddleware, error)
)

func (b *builder) NewMiddlewareClient(name string, config *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.Middleware, error) {
func (b *builder) NewMiddlewareClient(name string, config *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.KMiddleware, error) {
b.middlewareMux.RLock()
defer b.middlewareMux.RUnlock()
if builder, ok := b.middlewares[name]; ok {
Expand All @@ -50,7 +50,7 @@ func (b *builder) NewMiddlewareClient(name string, config *configv1.Customize_Co
return nil, ErrNotFound
}

func (b *builder) NewMiddlewareServer(name string, config *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.Middleware, error) {
func (b *builder) NewMiddlewareServer(name string, config *configv1.Customize_Config, ss ...middleware.OptionSetting) (middleware.KMiddleware, error) {
b.middlewareMux.RLock()
defer b.middlewareMux.RUnlock()
if builder, ok := b.middlewares[name]; ok {
Expand All @@ -59,7 +59,7 @@ func (b *builder) NewMiddlewareServer(name string, config *configv1.Customize_Co
return nil, ErrNotFound
}

func (b *builder) NewMiddlewaresClient(mms []middleware.Middleware, cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.Middleware {
func (b *builder) NewMiddlewaresClient(mms []middleware.KMiddleware, cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.KMiddleware {
configs := customize.ConfigsFromType(cc, middleware.Type)
var mbs []*middlewareWrap
b.middlewareMux.RLock()
Expand All @@ -81,7 +81,7 @@ func (b *builder) NewMiddlewaresClient(mms []middleware.Middleware, cc *configv1
return mms
}

func (b *builder) NewMiddlewaresServer(mms []middleware.Middleware, cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.Middleware {
func (b *builder) NewMiddlewaresServer(mms []middleware.KMiddleware, cc *configv1.Customize, ss ...middleware.OptionSetting) []middleware.KMiddleware {
configs := customize.ConfigsFromType(cc, middleware.Type)
var mbs []*middlewareWrap
b.middlewareMux.RLock()
Expand Down Expand Up @@ -116,10 +116,10 @@ type middlewareWrap struct {
Builder MiddlewareBuilder
}

func (m middlewareWrap) NewClient(ss ...middleware.OptionSetting) (middleware.Middleware, error) {
func (m middlewareWrap) NewClient(ss ...middleware.OptionSetting) (middleware.KMiddleware, error) {
return m.Builder.NewMiddlewareClient(m.Config, ss...)
}

func (m middlewareWrap) NewServer(ss ...middleware.OptionSetting) (middleware.Middleware, error) {
func (m middlewareWrap) NewServer(ss ...middleware.OptionSetting) (middleware.KMiddleware, error) {
return m.Builder.NewMiddlewareServer(m.Config, ss...)
}
6 changes: 3 additions & 3 deletions build_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/origadmin/runtime/registry"
)

// NewRegistrar creates a new Registrar object based on the given RegistryConfig.
func (b *builder) NewRegistrar(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.Registrar, error) {
// NewRegistrar creates a new KRegistrar object based on the given RegistryConfig.
func (b *builder) NewRegistrar(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.KRegistrar, error) {
return b.RegistryBuilder.NewRegistrar(cfg, ss...)
}

// NewDiscovery creates a new discovery object based on the given RegistryConfig.
func (b *builder) NewDiscovery(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.Discovery, error) {
func (b *builder) NewDiscovery(cfg *configv1.Registry, ss ...registry.OptionSetting) (registry.KDiscovery, error) {
return b.RegistryBuilder.NewDiscovery(cfg, ss...)
}

Expand Down
12 changes: 6 additions & 6 deletions config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ func (b *builder) RegisterConfigFunc(name string, buildFunc BuildFunc) {
b.RegisterConfigBuilder(name, buildFunc)
}

// BuildFunc is a function type that takes a SourceConfig and a list of Options and returns a Selector and an error.
type BuildFunc func(*configv1.SourceConfig, ...OptionSetting) (SourceConfig, error)
// BuildFunc is a function type that takes a KConfig and a list of Options and returns a Selector and an error.
type BuildFunc func(*configv1.SourceConfig, ...OptionSetting) (KConfig, error)

// NewConfig is a method that implements the ConfigBuilder interface for ConfigBuildFunc.
func (fn BuildFunc) NewConfig(cfg *configv1.SourceConfig, ss ...OptionSetting) (SourceConfig, error) {
// Call the function with the given SourceConfig and a list of Options.
func (fn BuildFunc) NewConfig(cfg *configv1.SourceConfig, ss ...OptionSetting) (KConfig, error) {
// Call the function with the given KConfig and a list of Options.
return fn(cfg, ss...)
}

// NewConfig creates a new Selector object based on the given SourceConfig and options.
func (b *builder) NewConfig(cfg *configv1.SourceConfig, ss ...OptionSetting) (SourceConfig, error) {
// NewConfig creates a new Selector object based on the given KConfig and options.
func (b *builder) NewConfig(cfg *configv1.SourceConfig, ss ...OptionSetting) (KConfig, error) {
b.factoryMux.RLock()
defer b.factoryMux.RUnlock()
configBuilder, ok := b.factories[cfg.Type]
Expand Down
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type (

// Factory is an interface that defines a method for creating a new config.
Factory interface {
// NewConfig creates a new config using the given SourceConfig and a list of Options.
NewConfig(*configv1.SourceConfig, ...OptionSetting) (SourceConfig, error)
// NewConfig creates a new config using the given KConfig and a list of Options.
NewConfig(*configv1.SourceConfig, ...OptionSetting) (KConfig, error)
}

// Syncer is an interface that defines a method for synchronizing a config.
Expand Down Expand Up @@ -68,11 +68,11 @@ func (v *EnvVars) Get(key string) (string, bool) {
type Config struct {
EnvPrefixes []string
envVars EnvVars
source SourceConfig
source KConfig
}

func (c *Config) LoadFromFile(path string, opts ...SourceOption) error {
var sources = []Source{file.NewSource(path)}
func (c *Config) LoadFromFile(path string, opts ...KOption) error {
var sources = []KSource{file.NewSource(path)}
if c.EnvPrefixes != nil {
sources = append(sources, configenv.NewSource(c.EnvPrefixes...))
opts = append(opts, WithSource(sources...))
Expand Down
34 changes: 17 additions & 17 deletions config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (

// Define types from kratos config package
type (
Decoder = kratosconfig.Decoder
KeyValue = kratosconfig.KeyValue
Merge = kratosconfig.Merge
Observer = kratosconfig.Observer
Reader = kratosconfig.Reader
Resolver = kratosconfig.Resolver
Source = kratosconfig.Source
SourceOption = kratosconfig.Option
SourceConfig = kratosconfig.Config
Value = kratosconfig.Value
Watcher = kratosconfig.Watcher
KDecoder = kratosconfig.Decoder
KKeyValue = kratosconfig.KeyValue
KMerge = kratosconfig.Merge
KObserver = kratosconfig.Observer
KReader = kratosconfig.Reader
KResolver = kratosconfig.Resolver
KSource = kratosconfig.Source
KOption = kratosconfig.Option
KConfig = kratosconfig.Config
KValue = kratosconfig.Value
KWatcher = kratosconfig.Watcher
)

var (
Expand All @@ -30,31 +30,31 @@ var (
)

// NewSourceConfig returns a new config instance
func NewSourceConfig(opts ...SourceOption) SourceConfig {
func NewSourceConfig(opts ...KOption) KConfig {
return kratosconfig.New(opts...)
}

// WithDecoder sets the decoder
func WithDecoder(d Decoder) SourceOption {
func WithDecoder(d KDecoder) KOption {
return kratosconfig.WithDecoder(d)
}

// WithMergeFunc sets the merge function
func WithMergeFunc(m Merge) SourceOption {
func WithMergeFunc(m KMerge) KOption {
return kratosconfig.WithMergeFunc(m)
}

// WithResolveActualTypes enables resolving actual types
func WithResolveActualTypes(enableConvertToType bool) SourceOption {
func WithResolveActualTypes(enableConvertToType bool) KOption {
return kratosconfig.WithResolveActualTypes(enableConvertToType)
}

// WithResolver sets the resolver
func WithResolver(r Resolver) SourceOption {
func WithResolver(r KResolver) KOption {
return kratosconfig.WithResolver(r)
}

// WithSource sets the source
func WithSource(s ...Source) SourceOption {
func WithSource(s ...KSource) KOption {
return kratosconfig.WithSource(s...)
}
10 changes: 5 additions & 5 deletions config/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
package config

type Option struct {
SourceOptions []SourceOption
Decoder Decoder
SourceOptions []KOption
Decoder KDecoder
Encoder Encoder
}

// Encoder is a function that takes a value and returns a byte slice and an error.
type Encoder func(v any) ([]byte, error)

// OptionSetting is a function that takes a pointer to a SourceOption struct and modifies it.
// OptionSetting is a function that takes a pointer to a KOption struct and modifies it.
type OptionSetting = func(s *Option)

// WithOptions sets the options field of the SourceOption struct.
func WithOptions(options ...SourceOption) OptionSetting {
// WithOptions sets the options field of the KOption struct.
func WithOptions(options ...KOption) OptionSetting {
return func(option *Option) {
option.SourceOptions = options
}
Expand Down
16 changes: 8 additions & 8 deletions log/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const (
)

type (
Logger = log.Logger
Level = log.Level
Valuer = log.Valuer
FilterOption = log.FilterOption
Filter = log.Filter
Option = log.Option
Helper = log.Helper
WriterOptionFn = log.WriterOptionFn
KLogger = log.Logger
KLevel = log.Level
KValuer = log.Valuer
KFilterOption = log.FilterOption
KFilter = log.Filter
KOption = log.Option
KHelper = log.Helper
KWriterOptionFn = log.WriterOptionFn
)

var (
Expand Down
3 changes: 3 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

// Package log implements the functions, types, and interfaces for the module.
package log

type Logging struct {
}
2 changes: 1 addition & 1 deletion middleware/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import (
)

func CircuitBreakerClient(f Filter) Filter {
log.Debug("[Middleware] CircuitBreaker client middleware enabled")
log.Debug("[KMiddleware] CircuitBreaker client middleware enabled")
return f.Filter("CircuitBreaker", circuitbreaker.Client())
}
Loading

0 comments on commit 09502fd

Please sign in to comment.