42 lines
558 B
Go
42 lines
558 B
Go
![]() |
package data
|
||
|
|
||
|
import (
|
||
|
json "github.com/json-iterator/go"
|
||
|
)
|
||
|
|
||
|
type _ID_ interface {
|
||
|
int | string
|
||
|
}
|
||
|
|
||
|
type _TD_[ID _ID_] interface {
|
||
|
id() ID
|
||
|
}
|
||
|
|
||
|
type table[ID _ID_, TD _TD_[ID]] struct {
|
||
|
l []TD
|
||
|
m map[ID]TD
|
||
|
}
|
||
|
|
||
|
func (this *table[ID, TD]) load(buf []byte) error {
|
||
|
var err = json.Unmarshal(buf, &this.l)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
this.m = map[ID]TD{}
|
||
|
for _, d := range this.l {
|
||
|
this.m[d.id()] = d
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (this *table[ID, TD]) List() []TD {
|
||
|
return this.l
|
||
|
}
|
||
|
|
||
|
func (this *table[ID, TD]) Get(id ID) (d TD) {
|
||
|
d = this.m[id]
|
||
|
return
|
||
|
}
|