Go load tabular data with JSON

Support hot reload reload, but there will be some problems, the following notes are written

package table

import (
    "runtime/debug"
)

// IntArray int type array 
type IntArray [] int

// FloatArray Float32 type array 
type FloatArray []float32

// StringArray string type array 
type StringArray [] string

type iTable interface {
    load() error
    reload() error
}

var (
    tableList []iTable

    //MFCity 表格
    MFCity = &MFCityTable{file: "../data/mfcity.json"}

    //CityBattleCreature 表格
    CityBattleCreature = &CityBattleCreatureTable{file: "../data/cityBattleCreature.json"}
)

func init() {
    tableList = []iTable{
        MFCity,
        CityBattleCreature,
    }
}

// Load loads all tables 
func Load() {
     for _, v := range tableList {
         if e := v.load(); nil != e {
            panic(e.Error())
        }
    }
}

// Reload reloads all tables
 // Description:
 // 1. Reload table will not reduce the number of records. For example, table A originally had 100 records, and then changed it to 99 records. Reload is still 100 records.
 // 2. Reload does not It will change the length of the array, only the value can be changed, [1,2,3] and then the table is changed to [2,2], after Reload it is actually [2,2,3] 
func Reload() {
     // The intermediate processing is unpredictable The error must be recovered 
    defer func() {
         if err := recover(); nil != err {
            log.Error("[Table.Reload] %s", debug.Stack())
        }
    }()

    for _, v := range tableList {
        if e := v.reload(); nil != e {
            log.Error(e.Error())
        }
    }
}


// DeepCopy deep copy
 // To pass in two pointers, do not pass values 
​​func DeepCopy(dst, src interface {}) error {
     var buf bytes.Buffer
     if err := gob.NewEncoder(&buf).Encode(src); err != nil {
         return err
    }
    return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
}

 

form code

package table

import (
    "runtime/debug"
)

// MFCityData single data 
type MFCityData struct {
    ID         int        `json:"id"`
    City       int        `json:"city"`
    Lv         IntArray   `json:"lv"`
    TaskCommon []IntArray `json:"taskCommon"`
}

//MFCityTable 表格
type MFCityTable struct {
    file    string
    DataMap map[int]MFCityData
}

//load 加载
func (table *MFCityTable) load() error {
    if nil == table.DataMap {
        table.DataMap = make(map[int]MFCityData)
    }

    temp := make([]MFCityData, 0)
    if err := util.LoadJSONConfig(table.file, &temp); nil != err {
        return err
    }

    for _, v := range temp {
        table.DataMap [v.ID] = v
    }

    return nil
}

// reload re-table
 // reload will not do decrement, only increment and change 
func (table * MFCityTable) reload() error {

    // Unpredictable errors in intermediate processing must be recovered 
    defer func() {
         if err := recover(); nil != err {
            log.Error("[MFCityTable.reload] %s", debug.Stack())
        }
    }()

    temp := make([]MFCityData, 0)
    if err := util.LoadJSONConfig(table.file, &temp); nil != err {
        return err
    }

    for _, v := range temp {
         // The existing value needs to be modified, the new one needs to be added directly 
        if data, ok := table.DataMap[v.ID]; ok {
            DeepCopy(&data, &v)
        } else {
            table.DataMap [v.ID] = v
        }
    }

    return nil
}

// GetByID finds 
func by ID (table *MFCityTable) GetByID(id int ) (*MFCityData, bool ) {
    v, ok := table.DataMap[id]
    return &v, ok
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325094428&siteId=291194637