Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansu committed Mar 2, 2021
1 parent c4684a5 commit 3f7bf70
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"num": 10,
"dropType": 2,
"dropValue": 2,
"desc": "ddddf11fds11"
"desc": "desc1111222"
},
{
"dropId": 1002,
Expand Down
22 changes: 21 additions & 1 deletion _examples/test1_data_config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/cherry-game/cherry"
"github.com/cherry-game/cherry/_examples/test1_handler/mocks"
cherryConst "github.com/cherry-game/cherry/const"
"github.com/cherry-game/cherry/data_config"
"github.com/cherry-game/cherry/handler"
"github.com/cherry-game/cherry/logger"
Expand All @@ -20,11 +21,30 @@ func app() {
handlers := cherryHandler.NewComponent()

dataConfig := cherryDataConfig.NewComponent()
dataConfig.Register(new(mocks.DropConfig))

dataConfig.Register("dropConfig", "dropOneConfig")

testApp.Startup(
handlers,
dataConfig,
)
cherryLogger.Infow("test", "key", "itemId", "value", 2)

go getDropConfig(testApp)
}

func getDropConfig(app *cherry.Application) {
component := app.Find(cherryConst.DataConfigComponent).(*cherryDataConfig.DataConfigComponent)

var list []mocks.DropConfig
component.Get("dropConfig", &list)
cherryLogger.Warnf("%p", list)

var list1 []mocks.DropConfig
component.Get("dropConfig", &list1)
cherryLogger.Warnf("%p", list1)

var one mocks.DropOneConfig
component.Get("dropOneConfig", &one)
cherryLogger.Warnf("%p", one)
}
12 changes: 6 additions & 6 deletions _examples/test1_handler/mocks/drop_config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package mocks

type DropConfig struct {
dropId int `json:dropId`
itemType int `json:itemType`
itemId int `json:itemId`
num int `json:num`
dropType int `json:dropType`
dropValue int `json:dropValue`
DropId int `json:"dropId"`
ItemType int `json:"itemType"`
ItemId int `json:"itemId"`
Num int `json:"num"`
DropType int `json:"dropType"`
DropValue int `json:"dropValue"`
}

func (d *DropConfig) Name() string {
Expand Down
22 changes: 22 additions & 0 deletions _examples/test1_handler/mocks/drop_one_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mocks

type DropOneConfig struct {
DropId int `json:"dropId"`
ItemType int `json:"itemType"`
ItemId int `json:"itemId"`
Num int `json:"num"`
DropType int `json:"dropType"`
DropValue int `json:"dropValue"`
}

func (d *DropOneConfig) Name() string {
return "dropOneConfig"
}

func (d *DropOneConfig) Init() {

}

func (d *DropOneConfig) Reload() {

}
2 changes: 1 addition & 1 deletion application.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (a *Application) Startup(components ...cherryInterfaces.IComponent) {
}

cherryLogger.Infof("[nodeId = %s] application is running. startTime = %s", a.NodeId(), a.StartTime())
cherryLogger.Info("-----------------------------------------")
cherryLogger.Info("-------------------------------------------------")
}

func (a *Application) Shutdown(beforeStopFn ...func()) {
Expand Down
1 change: 1 addition & 0 deletions const/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
const (
HandlerComponent = "handler_component"
SessionComponent = "session_component"
DataConfigComponent = "data_config_component"
ORMComponent = "db_orm_component"
QueueComponent = "db_queue_component"
ConnectorPomeloComponent = "connector_pomelo_component"
Expand Down
41 changes: 6 additions & 35 deletions data_config/data_config.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
package cherryDataConfig

type (
IDataConfig interface {
Register(file IConfigFile) // 注册文件
GetFiles() []IConfigFile // 获取注册的文件列表
Get(fileName string) interface{} // 获取原始的数据
Load(fileName string, data []byte) // 加载数据流
}

// IDataSource 配置文件数据源
IDataSource interface {
Name() string // 数据源名称
Init(dataConfig IDataConfig) // 函数初始化时
Stop() // 停止
}

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

// IConfigFile 配置文件接口
IConfigFile interface {
Name() string // 配置名称
Init() // 配置序列化后,执行该函数
Reload() // 配置重加载后,先执行Init(),再执行该函数
}
)

var (
parserMap = make(map[string]IParser) //文件格式解析器
sourceMap = make(map[string]IDataSource) //配置文件数据源
parserMap = make(map[string]IParser) //文件格式解析器
dataSourceMap = make(map[string]IDataSource) //数据配置数据源
)

func init() {
// default
RegisterParser(new(JsonParser))
RegisterDataSource(new(FileSource))
RegisterSource(new(FileSource))
}

func GetParser(name string) IParser {
Expand All @@ -48,9 +19,9 @@ func RegisterParser(parser IParser) {
}

func GetDataSource(name string) IDataSource {
return sourceMap[name]
return dataSourceMap[name]
}

func RegisterDataSource(source IDataSource) {
sourceMap[source.Name()] = source
func RegisterSource(dataSource IDataSource) {
dataSourceMap[dataSource.Name()] = dataSource
}
115 changes: 74 additions & 41 deletions data_config/data_config_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,129 @@ package cherryDataConfig

import (
"fmt"
cherryUtils "github.com/cherry-game/cherry/extend/utils"
"github.com/cherry-game/cherry/const"
"github.com/cherry-game/cherry/extend/utils"
"github.com/cherry-game/cherry/interfaces"
cherryLogger "github.com/cherry-game/cherry/logger"
"github.com/cherry-game/cherry/logger"
"github.com/cherry-game/cherry/profile"
"github.com/goinggo/mapstructure"
"reflect"
"sync"
)

type DataConfigComponent struct {
cherryInterfaces.BaseComponent
sync.Mutex
register []IConfigFile
configFiles map[string]interface{}
source IDataSource
parser IParser
parserExtName string
cherryInterfaces.BaseComponent
configNames []string
registerMaps map[string]interface{}
dataSource IDataSource
parser IParser
}

func NewComponent() *DataConfigComponent {
return &DataConfigComponent{}
return &DataConfigComponent{
registerMaps: make(map[string]interface{}),
configNames: []string{},
}
}

//Name unique components name
// Name unique components name
func (d *DataConfigComponent) Name() string {
return "data_config_component"
return cherryConst.DataConfigComponent
}

func (d *DataConfigComponent) Init() {
d.configFiles = make(map[string]interface{})

// read data_config node in profile-x.json
// read data_config node in profile-{env}.json
configNode := cherryProfile.Config("data_config")
if configNode.LastError() != nil {
panic(fmt.Sprintf("not found `data_config` node in `%s` file.", cherryProfile.FilePath()))
}

// get data source
sourceName := configNode.Get("data_source").ToString()
d.source = GetDataSource(sourceName)
if d.source == nil {
d.dataSource = GetDataSource(sourceName)
if d.dataSource == nil {
panic(fmt.Sprintf("data source not found. sourceName = %s", sourceName))
}

// get file parser
// get parser
parserName := configNode.Get("parser").ToString()
d.parser = GetParser(parserName)
if d.parser == nil {
panic(fmt.Sprintf("parser not found. sourceName = %s", parserName))
panic(fmt.Sprintf("parser not found. parserName = %s", parserName))
}

cherryUtils.Try(func() {
d.source.Init(d)
d.dataSource.Init(d)

for _, name := range d.configNames {
data, found := d.GetBytes(name)
if !found {
cherryLogger.Warnf("configName = %s not found.", name)
continue
}

var val interface{}
err := d.parser.Unmarshal(data, &val)
if err != nil {
cherryLogger.Warnf("unmarshal data error=%v, configName=%s", err, name)
continue
}

d.registerMaps[name] = val
}
}, func(errString string) {
cherryLogger.Warn(errString)
})
}

func (d *DataConfigComponent) Stop() {
if d.source != nil {
d.source.Stop()
if d.dataSource != nil {
d.dataSource.Stop()
}
}

func (d *DataConfigComponent) Register(file IConfigFile) {
d.register = append(d.register, file)
}
func (d *DataConfigComponent) Register(configNames ...string) {
if len(configNames) < 1 {
return
}

func (d *DataConfigComponent) GetFiles() []IConfigFile {
return d.register
for _, name := range configNames {
if name != "" {
d.configNames = append(d.configNames, name)
}
}
}

func (d *DataConfigComponent) Get(fileName string) interface{} {
return d.configFiles[fileName]
func (d *DataConfigComponent) GetBytes(configName string) (data []byte, found bool) {
data, err := d.dataSource.ReadData(configName)
if err != nil {
cherryLogger.Warn(err)
return nil, false
}

return data, true
}

func (d *DataConfigComponent) Load(fileName string, data []byte) {
cherryUtils.Try(func() {
func (d *DataConfigComponent) Get(configName string, val interface{}) {
typ := reflect.TypeOf(val)
if typ.Kind() != reflect.Ptr {
cherryLogger.Warnf("val must ptr type. configName={}", configName)
return
}

var v interface{}
err := d.parser.Unmarshal(data, &v)
if err != nil {
result, found := d.registerMaps[configName]
if found {
if err := mapstructure.Decode(result, val); err != nil {
cherryLogger.Warn(err)
return
}
}
}

defer d.Unlock()
d.Lock()

d.configFiles[fileName] = &v
func (d *DataConfigComponent) GetParser() IParser {
return d.parser
}

}, func(errString string) {
cherryLogger.Warn(errString)
})
func (d *DataConfigComponent) GetDataSource() IDataSource {
return d.dataSource
}
Loading

0 comments on commit 3f7bf70

Please sign in to comment.