cherryDataConfig

package module
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 15 Imported by: 1

README

data-config组件

  • 自定义数据源
  • 读取数据
  • 热更新数据

Install

Prerequisites
  • GO >= 1.17
Using go get
go get github.com/cherry-game/cherry/components/data-config@latest

Quick Start

import cherryDataConfig "github.com/cherry-game/cherry/components/data-config"
package demo
import (
	"github.com/cherry-game/cherry"
	cherryDataConfig "github.com/cherry-game/cherry/components/data-config"
)

// RegisterComponent 注册struct到data-config
func RegisterComponent() {
	dataConfig := cherryDataConfig.NewComponent()
	dataConfig.Register(
		&DropList,
		&DropOne,
	)

	//data-config组件注册到cherry引擎
	cherry.RegisterComponent(dataConfig)
}

example

Documentation

Index

Constants

View Source
const (
	Name = "data_config_component"
)

Variables

This section is empty.

Functions

func RegisterParser

func RegisterParser(parser IDataParser)

func RegisterSource

func RegisterSource(dataSource IDataSource)

Types

type Component

type Component struct {
	sync.RWMutex
	cfacade.Component
	// contains filtered or unexported fields
}

func New added in v1.3.1

func New() *Component

func (*Component) GetBytes

func (d *Component) GetBytes(configName string) (data []byte, found bool)

func (*Component) GetDataSource

func (d *Component) GetDataSource() IDataSource

func (*Component) GetIConfig added in v1.3.1

func (d *Component) GetIConfig(name string) IConfig

func (*Component) GetParser

func (d *Component) GetParser() IDataParser

func (*Component) Init

func (d *Component) Init()

func (*Component) Name

func (d *Component) Name() string

Name unique components name

func (*Component) OnStop

func (d *Component) OnStop()

func (*Component) Register

func (d *Component) Register(configs ...IConfig)

type ConfigChangeFn

type ConfigChangeFn func(configName string, data []byte)

ConfigChangeFn 数据变更时触发该函数

type IConfig

type IConfig interface {
	Name() string                                      // 配置名称
	Init()                                             // 结构体初始化
	OnLoad(maps interface{}, reload bool) (int, error) // 配置序列化后,执行该函数 (size,error)
	OnAfterLoad(reload bool)                           // 所有配置加载后再执行该函数
}

IConfig 配置接口

type IDataConfig

type IDataConfig interface {
	Register(configFile ...IConfig)                       // 注册映射文件
	GetBytes(configName string) (data []byte, found bool) // 获取原始的数据
	GetParser() IDataParser                               // 当前参数配置的数据格式解析器
	GetDataSource() IDataSource                           // 当前参数配置的获取数据源
}

IDataConfig 数据配置接口

type IDataParser

type IDataParser interface {
	TypeName() string                           // 注册名称
	Unmarshal(text []byte, v interface{}) error // 文件格式解析器
}

IDataParser 数据格式解析接口

func GetParser

func GetParser(name string) IDataParser

type IDataSource

type IDataSource interface {
	Name() string                                           // 数据源名称
	Init(dataConfig IDataConfig)                            // 函数初始化时
	ReadBytes(configName string) (data []byte, error error) // 获取数据流
	OnChange(fn ConfigChangeFn)                             // 数据变更时
	Stop()                                                  // 停止
}

IDataSource 配置文件数据源

func GetDataSource

func GetDataSource(name string) IDataSource

type ParserJson

type ParserJson struct {
}

func (*ParserJson) TypeName

func (j *ParserJson) TypeName() string

func (*ParserJson) Unmarshal

func (j *ParserJson) Unmarshal(data []byte, v interface{}) error

type SourceFile

type SourceFile struct {
	// contains filtered or unexported fields
}

SourceFile 本地读取数据配置文件

func (*SourceFile) Init

func (f *SourceFile) Init(_ IDataConfig)

func (*SourceFile) Name

func (f *SourceFile) Name() string

func (*SourceFile) OnChange

func (f *SourceFile) OnChange(fn ConfigChangeFn)

func (*SourceFile) ReadBytes

func (f *SourceFile) ReadBytes(configName string) ([]byte, error)

func (*SourceFile) Stop

func (f *SourceFile) Stop()

type SourceRedis

type SourceRedis struct {
	// contains filtered or unexported fields
}

SourceRedis redis方式获取数据配置

从profile-x.json中获取data_config的属性配置, 如果"data_source"的值为"redis",则启用redis方式读取数据配置. 通过redis的订阅机制来触发哪个配置有变更,则进行重新加载处理. 程序启动后,会订阅“subscribeKey”,当有变更时,则执行加载.

func (*SourceRedis) Init

func (r *SourceRedis) Init(_ IDataConfig)

func (*SourceRedis) Name

func (r *SourceRedis) Name() string

func (*SourceRedis) OnChange

func (r *SourceRedis) OnChange(fn ConfigChangeFn)

func (*SourceRedis) ReadBytes

func (r *SourceRedis) ReadBytes(configName string) ([]byte, error)

func (*SourceRedis) Stop

func (r *SourceRedis) Stop()

Jump to

Keyboard shortcuts

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