golang二维表配置解决方案

初始化表格

this.GameRule = new(lo.TableGameRule)
    core.LocalData.InitInterface("GameRule", this.GameRule, []string{"Id"})


item := LocalData.Item.GetByIndexes("Id", lo.Tid)

---------------------------------------------------

目录A放置任意多的csv文件,格式如下。可excel导出

Shop,编号,道具ID,价格,数量,描述
TYPE,int32,int32,int64,int64,string
JSON,,,,,-
SERVER,Id,Tid,Price,Count,Info
CLIENT,,,,,
,1,1001,2,1,1瓶啤酒
,2,1001,5,3,3瓶啤酒
,3,1001,15,10,10瓶啤酒
,10,1002,5,1,1瓶解酒茶
,20,1003,1,1,1个喇叭
END,21,1003,10,12,12个喇叭

调用下列代码即可导出到对应目录,可写作工具调用

/**
go语言导出LO,通过Csv
*/
package tools

import (
	"game/util"
	"strings"
)

var _csvFile string
var _outFile string
var _demoStr string
var _packageString string

func ExportGolo(p1 string, p2 string) {
	_csvFile = p1
	_outFile = p2
	readDemoString()
	export(_csvFile)
}

func readDemoString() {
	_demoStr = util.GetStringByFile("demo.txt")
	pathArgs := strings.Split(_outFile, "/")
	_packageString = pathArgs[len(pathArgs)-2]
}

func export(file string) {
	paths := util.GetFilelist(file, []string{".csv", ".CSV"})
	for _, subFile := range paths {
		exportOne(subFile)
	}
}

/*
	TestGame,编号,名称,赔率,,,
	TYPE,int32,string,float32,,,
	JSON,o,o,-,,,
	SERVER,Id,Name,Rate,,,
	CLIENT,Id,Name,,,,
	,1,张飞,1.1,,,
	,2,关羽,1.12,,,
	END,3,刘备,1.133,,,
*/
func exportOne(target string) {
	str := util.GetStringByFileNP(target)
	args := strings.Split(str, "\r\n")
	infos := strings.Split(args[0], ",")
	types := strings.Split(args[1], ",")
	jsons := strings.Split(args[2], ",")
	servers := strings.Split(args[3], ",")
	outStr := _demoStr
	//设置包
	outStr = strings.Replace(outStr, "[$package]", _packageString, -1)
	//设置名称
	outStr = strings.Replace(outStr, "[$VO]", infos[0]+"LO", -1)
	//设置表格
	outStr = strings.Replace(outStr, "[$TABLE]", "Table"+infos[0], -1)
	i := 1
	//设置属性
	pps := ""
	//			for(i=1;i<servers.length;i++){
	for i = 1; i < len(servers); i++ {
		if servers[i] != "" {
			if pps == "" {
				pps += "	" + servers[i] + "   " + types[i] + "   "
			} else {
				pps += "\r\n	" + servers[i] + "   " + types[i] + "   "
			}
			if jsons[i] == "" {
				pps += "`json:\"" + strings.ToLower(servers[i][0:1]) + util.SubStr(servers[i], 1, -1) + "\"`"
			} else if jsons[i] == "o" {
				pps += "`json:\"" + strings.ToLower(servers[i][0:1]) + util.SubStr(servers[i], 1, -1) + ",omitempty\"`"
			} else if jsons[i] == "-" {
				pps += "`json:\"-\"`"
			}
		}
	}
	outStr = strings.Replace(outStr, "[$PP]", pps, -1)
	//设置负值部分
	swc := ""
	for i = 1; i < len(servers); i++ {
		if servers[i] != "" {
			if swc == "" {
				swc += "			case \"" + servers[i] + "\":"
				swc += "\r\n				m[util.AnyToString(value." + servers[i] + ")] = value"
			} else {
				swc += "\r\n			case \"" + servers[i] + "\":"
				swc += "\r\n				m[util.AnyToString(value." + servers[i] + ")] = value"
			}
		}
	}
	outStr = strings.Replace(outStr, "[$SWITCH]", swc, -1)
	//			var opath:String = _outFile.nativePath+"/"+infos[0]+"_LO.go";
	opath := _outFile + infos[0] + "_LO.go"
	//<user code>内容截获
	ost := ""
	if util.FileIsExistNP(opath) {
		oldStr := util.GetStringByFileNP(opath)
		startIndex := strings.Index(oldStr, "//<user code>")
		endIndex := strings.Index(oldStr, "//</user code>") + len("//</user code>")
		if startIndex >= 0 && endIndex >= 0 {
			ost = util.SubString(oldStr, startIndex, endIndex)
		}
	}
	outStr = strings.Replace(outStr, "[$USER]", ost, -1)
	//将设置好的内容输出到对应的文件中
	util.FileWriteStringNP(opath, outStr)
	util.Log("export golo", opath)
}

导出的文件为go格式,下面的代码为自动生成的。

//由工具产生,不能修改
package lo

import (
	"game/core"
	"game/util"
	"reflect"
	"strconv"
)

type ShopLO struct {
	Id   int32   `json:"id"`
	Tid   int32   `json:"tid"`
	Price   int64   `json:"price"`
	Count   int64   `json:"count"`
	Info   string   `json:"-"`
}

type TableShop struct {
	table   *core.LocalTable
	datas   []*ShopLO
	indexes map[string](map[string]*ShopLO)
}

func (this *TableShop) Init(table *core.LocalTable) {
	this.table = table
	if this.datas == nil {
		this.datas = make([]*ShopLO, 0, this.table.Len)
	}
}

func (this *TableShop) Insert(values []string) {
	n := &ShopLO{}
	mutable := reflect.ValueOf(n).Elem()
	for i := 0; i < len(values); i++ {
		if this.table.Heads[i] == "" || values[i] == "" {
			continue
		}
		key := this.table.Heads[i]
		value := mutable.FieldByName(key)
		switch value.Type().Name() {
		case "int8", "int16", "int32", "int64", "int":
			v, e := strconv.Atoi(values[i])
			if e == nil {
				value.SetInt(int64(v))
			} else {
				util.LogError(e)
			}
		case "string":
			value.SetString(values[i])
		case "float32", "float64":
			f, e := strconv.ParseFloat(values[i], 64)
			if e == nil {
				value.SetFloat(f)
			} else {
				util.LogError(e)
			}
		case "bool":
			b, e := strconv.ParseBool(values[i])
			if e == nil {
				value.SetBool(b)
			} else {
				util.LogError(e)
			}
		}
	}
	this.datas = append(this.datas, n)
}

func (this *TableShop) Build() {
	if this.table.Indexes == nil || len(this.table.Indexes) == 0 {
		return
	}
	this.indexes = make(map[string](map[string]*ShopLO))
	for i := 0; i < len(this.table.Indexes); i++ {
		m := make(map[string]*ShopLO, len(this.datas))
		this.indexes[this.table.Indexes[i]] = m
		for j := 0; j < len(this.datas); j++ {
			value := this.datas[j]
			switch this.table.Indexes[i] {
			case "Id":
				m[util.AnyToString(value.Id)] = value
			case "Tid":
				m[util.AnyToString(value.Tid)] = value
			case "Price":
				m[util.AnyToString(value.Price)] = value
			case "Count":
				m[util.AnyToString(value.Count)] = value
			case "Info":
				m[util.AnyToString(value.Info)] = value
			}
		}
	}
}

func (this *TableShop) GetByIndexes(key string, v interface{}) *ShopLO {
	if this.indexes[key] != nil {
		return this.indexes[key][util.AnyToString(v)]
	}
	return nil
}

func (this *TableShop) GetByIndexesLite(key string, v string) *ShopLO {
	if this.indexes[key] != nil {
		return this.indexes[key][v]
	}
	return nil
}

//<user code>
func (this *TableShop) GetAll() []*ShopLO {
	return this.datas
}

//</user code>

自管理的代码添加在user code标签内部

工具中用到的模版文件如下

demo.txt

//由工具产生,不能修改
package [$package]

import (
	"game/core"
	"game/util"
	"reflect"
	"strconv"
)

type [$VO] struct {
[$PP]
}

type [$TABLE] struct {
	table   *core.LocalTable
	datas   []*[$VO]
	indexes map[string](map[string]*[$VO])
}

func (this *[$TABLE]) Init(table *core.LocalTable) {
	this.table = table
	if this.datas == nil {
		this.datas = make([]*[$VO], 0, this.table.Len)
	}
}

func (this *[$TABLE]) Insert(values []string) {
	n := &[$VO]{}
	mutable := reflect.ValueOf(n).Elem()
	for i := 0; i < len(values); i++ {
		if this.table.Heads[i] == "" || values[i] == "" {
			continue
		}
		key := this.table.Heads[i]
		value := mutable.FieldByName(key)
		switch value.Type().Name() {
		case "int8", "int16", "int32", "int64", "int":
			v, e := strconv.Atoi(values[i])
			if e == nil {
				value.SetInt(int64(v))
			} else {
				util.LogError(e)
			}
		case "string":
			value.SetString(values[i])
		case "float32", "float64":
			f, e := strconv.ParseFloat(values[i], 64)
			if e == nil {
				value.SetFloat(f)
			} else {
				util.LogError(e)
			}
		case "bool":
			b, e := strconv.ParseBool(values[i])
			if e == nil {
				value.SetBool(b)
			} else {
				util.LogError(e)
			}
		}
	}
	this.datas = append(this.datas, n)
}

func (this *[$TABLE]) Build() {
	if this.table.Indexes == nil || len(this.table.Indexes) == 0 {
		return
	}
	this.indexes = make(map[string](map[string]*[$VO]))
	for i := 0; i < len(this.table.Indexes); i++ {
		m := make(map[string]*[$VO], len(this.datas))
		this.indexes[this.table.Indexes[i]] = m
		for j := 0; j < len(this.datas); j++ {
			value := this.datas[j]
			switch this.table.Indexes[i] {
[$SWITCH]
			}
		}
	}
}

func (this *[$TABLE]) GetByIndexes(key string, v interface{}) *[$VO] {
	if this.indexes[key] != nil {
		return this.indexes[key][util.AnyToString(v)]
	}
	return nil
}

func (this *[$TABLE]) GetByIndexesLite(key string, v string) *[$VO] {
	if this.indexes[key] != nil {
		return this.indexes[key][v]
	}
	return nil
}

[$USER]

LocalDataCenter.go

//策划数据提供中心
package data

import (
	"20180329_dice_ice/lo"
	"game/core"
	"game/util"
)

var LocalData *LocalDataCenter

/*----------exp--------------
testTable := new(TableTestGame)
core.LocalData.InitInterface("TestGame", testTable, []string{"Id", "Name"})
*/
type LocalDataCenter struct {
	GameRule *lo.TableGameRule
	Item     *lo.TableItem
	Shop     *lo.TableShop
	Room     *lo.TableRoom
}

//初始化基础数据
func (this *LocalDataCenter) Init() {
	this.GameRule = new(lo.TableGameRule)
	core.LocalData.InitInterface("GameRule", this.GameRule, []string{"Id"})
	this.Item = new(lo.TableItem)
	core.LocalData.InitInterface("Item", this.Item, []string{"Id"})
	this.Shop = new(lo.TableShop)
	core.LocalData.InitInterface("Shop", this.Shop, []string{"Id"})
	this.Room = new(lo.TableRoom)
	core.LocalData.InitInterface("Room", this.Room, []string{"Id"})
}

func InitLocalData(path string) {
	LocalData = &LocalDataCenter{}
	core.InitLocalDataManager(path)
	util.Log("基础数据库开启,初始化表格")
	LocalData.Init()
}


阅读更多
个人分类: golang
想对作者说点什么? 我来说一句

authorware 创建二维表

2010年12月13日 46KB 下载

没有更多推荐了,返回首页

不良信息举报

golang二维表配置解决方案

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭