move implementations to external repos (#17)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-08-25 13:44:41 +03:00
committed by GitHub
parent c4a303190a
commit 0f4b1435d9
238 changed files with 151 additions and 37364 deletions

View File

@@ -1,52 +0,0 @@
package mud
import (
"github.com/google/uuid"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/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{}),
}
}

View File

@@ -1,104 +0,0 @@
// Package mud is the micro data model implementation
package mud
import (
"github.com/unistack-org/micro/v3/codec/json"
"github.com/unistack-org/micro/v3/model"
"github.com/unistack-org/micro/v3/store"
"github.com/unistack-org/micro/v3/store/memory"
memsync "github.com/unistack-org/micro/v3/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,
}
}