The start of mud - the micro data model (#1811)

* The start of mud - the micro data model

* add comments
This commit is contained in:
Asim Aslam 2020-07-09 12:11:32 +01:00 committed by GitHub
parent e5db6ea8a7
commit 58d6726380
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 158 additions and 0 deletions

View File

@ -39,6 +39,8 @@ type Entity interface {
} }
type Options struct { type Options struct {
// Database to write to
Database string
// for serialising // for serialising
Codec codec.Marshaler Codec codec.Marshaler
// for locking // for locking

52
model/mud/entity.go Normal file
View File

@ -0,0 +1,52 @@
package mud
import (
"github.com/google/uuid"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/model"
)
type mudEntity struct {
id string
name string
value interface{}
codec codec.Marshaler
attributes map[string]interface{}
}
func (m *mudEntity) Attributes() map[string]interface{} {
return m.attributes
}
func (m *mudEntity) Id() string {
return m.id
}
func (m *mudEntity) Name() string {
return m.name
}
func (m *mudEntity) Value() interface{} {
return m.value
}
func (m *mudEntity) Read(v interface{}) error {
switch m.value.(type) {
case []byte:
b := m.value.([]byte)
return m.codec.Unmarshal(b, v)
default:
v = m.value
}
return nil
}
func newEntity(name string, value interface{}, codec codec.Marshaler) model.Entity {
return &mudEntity{
id: uuid.New().String(),
name: name,
value: value,
codec: codec,
attributes: make(map[string]interface{}),
}
}

104
model/mud/mud.go Normal file
View File

@ -0,0 +1,104 @@
// Package mud is the micro data model implementation
package mud
import (
"github.com/micro/go-micro/v2/codec/json"
"github.com/micro/go-micro/v2/model"
"github.com/micro/go-micro/v2/store"
"github.com/micro/go-micro/v2/store/memory"
memsync "github.com/micro/go-micro/v2/sync/memory"
)
type mudModel struct {
options model.Options
}
func (m *mudModel) Init(opts ...model.Option) error {
for _, o := range opts {
o(&m.options)
}
return nil
}
func (m *mudModel) NewEntity(name string, value interface{}) model.Entity {
// TODO: potentially pluralise name for tables
return newEntity(name, value, m.options.Codec)
}
func (m *mudModel) Create(e model.Entity) error {
// lock on the name of entity
if err := m.options.Sync.Lock(e.Name()); err != nil {
return err
}
// TODO: deal with the error
defer m.options.Sync.Unlock(e.Name())
// TODO: potentially add encode to entity?
v, err := m.options.Codec.Marshal(e.Value())
if err != nil {
return err
}
// TODO: include metadata and set database
return m.options.Store.Write(&store.Record{
Key: e.Id(),
Value: v,
}, store.WriteTo(m.options.Database, e.Name()))
}
func (m *mudModel) Read(opts ...model.ReadOption) ([]model.Entity, error) {
var options model.ReadOptions
for _, o := range opts {
o(&options)
}
// TODO: implement the options that allow querying
return nil, nil
}
func (m *mudModel) Update(e model.Entity) error {
// TODO: read out the record first, update the fields and store
// lock on the name of entity
if err := m.options.Sync.Lock(e.Name()); err != nil {
return err
}
// TODO: deal with the error
defer m.options.Sync.Unlock(e.Name())
// TODO: potentially add encode to entity?
v, err := m.options.Codec.Marshal(e.Value())
if err != nil {
return err
}
// TODO: include metadata and set database
return m.options.Store.Write(&store.Record{
Key: e.Id(),
Value: v,
}, store.WriteTo(m.options.Database, e.Name()))
}
func (m *mudModel) Delete(opts ...model.DeleteOption) error {
var options model.DeleteOptions
for _, o := range opts {
o(&options)
}
// TODO: implement the options that allow deleting
return nil
}
func (m *mudModel) String() string {
return "mud"
}
func NewModel(opts ...model.Option) model.Model {
options := model.Options{
Codec: new(json.Marshaler),
Sync: memsync.NewSync(),
Store: memory.NewStore(),
}
return &mudModel{
options: options,
}
}