kratos-config

module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2026 License: MIT

README ΒΆ

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

kratos-config

In-memory config source implementation to use with Kratos framework.


CHINESE README

δΈ­ζ–‡θ―΄ζ˜Ž

Features

  • Byte Slice Source: Load config from byte data instead of files
  • Multiple Formats: Support JSON, YAML, XML, Proto plus additional formats
  • Dynamic Updates: Runtime config updates with watch mechanism (DataSource)
  • Static Mode: Lightweight static config without watch overhead (DataStatic)
  • Type Safe: Complete support to Kratos config.Source interface
  • Thread Safe: Concurrent-safe config updates

Installation

go get github.com/yylego/kratos-config/configkratos

Quick Start

package main

import (
	"fmt"

	"github.com/go-kratos/kratos/v3/config"
	"github.com/yylego/kratos-config/configkratos"
	"github.com/yylego/must"
	"github.com/yylego/rese"
)

type AppConfig struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

func main() {
	jsonData := []byte(`{"name":"my-app","version":"1.0.0"}`)
	source := configkratos.NewJsonStatic(jsonData)

	cfg := config.New(config.WithSource(source))
	defer rese.F0(cfg.Close)

	must.Done(cfg.Load())

	var app AppConfig
	must.Done(cfg.Scan(&app))

	fmt.Printf("App: %s, Version: %s\n", app.Name, app.Version)
}

⬆️ Source: Source

Dynamic Config (Support runtime updates)
package main

import (
	"fmt"
	"time"

	"github.com/go-kratos/kratos/v3/config"
	"github.com/yylego/kratos-config/configkratos"
	"github.com/yylego/must"
	"github.com/yylego/rese"
)

type ServerConfig struct {
	Port int `json:"port"`
}

func main() {
	jsonData := []byte(`{"port":8080}`)
	source := configkratos.NewJsonSource(jsonData)

	cfg := config.New(config.WithSource(source))
	defer rese.F0(cfg.Close)

	must.Done(cfg.Load())

	var server ServerConfig
	must.Done(cfg.Scan(&server))
	fmt.Printf("Start with port: %d\n", server.Port)

	must.Done(cfg.Watch("port", func(key string, value config.Value) {
		var port int
		must.Done(value.Scan(&port))
		fmt.Printf("Port update: %d\n", port)
	}))

	time.Sleep(time.Second)
	newData := []byte(`{"port":9090}`)
	must.Done(source.Update(newData))

	time.Sleep(time.Second)
}

⬆️ Source: Source

YAML Config (Static mode with YAML format)
package main

import (
	"fmt"

	"github.com/go-kratos/kratos/v3/config"
	"github.com/yylego/kratos-config/configkratos"
	"github.com/yylego/must"
	"github.com/yylego/rese"
)

type DatabaseConfig struct {
	Host string `yaml:"host"`
	Port int    `yaml:"port"`
}

func main() {
	yamlData := []byte(`host: localhost
port: 5432`)
	source := configkratos.NewYamlStatic(yamlData)

	cfg := config.New(config.WithSource(source))
	defer rese.F0(cfg.Close)

	must.Done(cfg.Load())

	var res DatabaseConfig
	must.Done(cfg.Scan(&res))

	fmt.Printf("Database: %s:%d\n", res.Host, res.Port)
}

⬆️ Source: Source

API Reference

DataSource (Dynamic Config)

Create Source

// Generic function with format option
source := configkratos.NewDataSource(data, "json")

// Convenient functions to specific formats
jsonSource := configkratos.NewJsonSource(data)
yamlSource := configkratos.NewYamlSource(data)

Update Config

newData := []byte(`{"key":"new-value"}`)
err := source.Update(newData)
DataStatic (Static Config)

Create Static Source

// Generic function with format parameter
source := configkratos.NewDataStatic(data, "yaml")

// Convenient functions to specific formats
jsonSource := configkratos.NewJsonStatic(data)
yamlSource := configkratos.NewYamlStatic(data)

Supported Formats

  • json - JSON format
  • yaml - YAML format
  • xml - XML format
  • proto - Protocol Buffers
  • form - URL encoded form
  • Other formats registered in Kratos encoding

Use Cases

  • Unit Testing: Mock config data without creating temp files
  • Embedded Config: Package config data into binary
  • Dynamic Config: Runtime config updates from remote sources
  • Config Aggregation: Combine multiple config sources

Design Philosophy

DataSource vs DataStatic

  • DataSource: Full-featured with watch mechanism, suitable to scenarios requiring runtime updates
  • DataStatic: Lightweight without watch overhead, suitable to scenarios with immutable config

The dual implementation gives flexibility in choosing the best approach to different scenarios.

πŸ“„ License

MIT License. See LICENSE.


🀝 Contributing

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • πŸ› Found a mistake? Open an issue on GitHub with reproduction steps
  • πŸ’‘ Have a feature idea? Create an issue to discuss the suggestion
  • πŸ“– Documentation confusing? Report it so we can improve
  • πŸš€ Need new features? Share the use cases to help us understand requirements
  • ⚑ Performance issue? Help us optimize through reporting slow operations
  • πŸ”§ Configuration problem? Ask questions about complex setups
  • πŸ“’ Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • πŸ’¬ Feedback? We welcome suggestions and comments

πŸ”§ Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/kratos-config.git).
  3. Navigate: Navigate to the cloned project (cd kratos-config)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • ⭐ Give GitHub stars if this project helps you
  • 🀝 Share with teammates and (golang) programming friends
  • πŸ“ Write tech blogs about development tools and workflows - we provide content writing support
  • 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! πŸŽ‰πŸŽ‰πŸŽ‰


GitHub Stars

Stargazers

Directories ΒΆ

Path Synopsis
Package configkratos provides in-memory config source implementations to use with Kratos framework Implements both dynamic (DataSource) and static (DataStatic) config sources from byte slice data Supports runtime config updates through watch mechanism and multiple encoding formats Designed as lightweight alternative to file-based config sources in testing and embedded scenarios
Package configkratos provides in-memory config source implementations to use with Kratos framework Implements both dynamic (DataSource) and static (DataStatic) config sources from byte slice data Supports runtime config updates through watch mechanism and multiple encoding formats Designed as lightweight alternative to file-based config sources in testing and embedded scenarios
internal
demos/demo1x command
demo1x: Static JSON config basic usage example Shows how to load config from JSON bytes with NewJsonStatic
demo1x: Static JSON config basic usage example Shows how to load config from JSON bytes with NewJsonStatic
demos/demo2x command
demo2x: Dynamic config with runtime updates example Shows how to use Watch mechanism and Update config at runtime
demo2x: Dynamic config with runtime updates example Shows how to use Watch mechanism and Update config at runtime
demos/demo3x command
demo3x: Static YAML config basic usage example Shows how to load YAML format config with NewYamlStatic
demo3x: Static YAML config basic usage example Shows how to load YAML format config with NewYamlStatic

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL