Recursive record type for DB (#144)

This commit is contained in:
Janos Dobronszki
2021-06-08 16:03:59 +01:00
committed by GitHub
parent 01380190aa
commit dc4a493805
7 changed files with 171 additions and 89 deletions

View File

@@ -14,6 +14,7 @@ import (
gorm2 "github.com/micro/services/pkg/gorm"
"github.com/micro/services/pkg/tenant"
"github.com/patrickmn/go-cache"
"google.golang.org/protobuf/types/known/structpb"
"gorm.io/datatypes"
"gorm.io/gorm"
)
@@ -37,7 +38,7 @@ type Db struct {
// Call is a single request handler called via client.Call or the generated client code
func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateResponse) error {
if len(req.Record) == 0 {
if len(req.Record.AsMap()) == 0 {
return errors.BadRequest("db.create", "missing record")
}
tenantId, ok := tenant.FromContext(ctx)
@@ -60,11 +61,7 @@ func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateRe
c.Set(req.Table, true, 0)
}
m := map[string]interface{}{}
err = json.Unmarshal([]byte(req.Record), &m)
if err != nil {
return err
}
m := req.Record.AsMap()
if _, ok := m[idKey].(string); !ok {
m[idKey] = uuid.New().String()
}
@@ -85,7 +82,7 @@ func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateRe
}
func (e *Db) Update(ctx context.Context, req *db.UpdateRequest, rsp *db.UpdateResponse) error {
if len(req.Record) == 0 {
if len(req.Record.AsMap()) == 0 {
return errors.BadRequest("db.update", "missing record")
}
tenantId, ok := tenant.FromContext(ctx)
@@ -98,11 +95,7 @@ func (e *Db) Update(ctx context.Context, req *db.UpdateRequest, rsp *db.UpdateRe
return err
}
m := map[string]interface{}{}
err = json.Unmarshal([]byte(req.Record), &m)
if err != nil {
return err
}
m := req.Record.AsMap()
// where ID is specified do a single update record update
id, ok := m[idKey].(string)
@@ -182,7 +175,8 @@ func (e *Db) Read(ctx context.Context, req *db.ReadRequest, rsp *db.ReadResponse
if err != nil {
return err
}
ret := []map[string]interface{}{}
rsp.Records = []*structpb.Struct{}
for _, rec := range recs {
m, err := rec.Data.MarshalJSON()
if err != nil {
@@ -191,10 +185,15 @@ func (e *Db) Read(ctx context.Context, req *db.ReadRequest, rsp *db.ReadResponse
ma := map[string]interface{}{}
json.Unmarshal(m, &ma)
ma[idKey] = rec.ID
ret = append(ret, ma)
m, _ = json.Marshal(ma)
s := &structpb.Struct{}
err = s.UnmarshalJSON(m)
if err != nil {
return err
}
rsp.Records = append(rsp.Records, s)
}
bs, _ := json.Marshal(ret)
rsp.Records = string(bs)
return nil
}