Skip to content

Commit

Permalink
[fix] use string and []byte to store resource
Browse files Browse the repository at this point in the history
  • Loading branch information
robotLJW committed Dec 27, 2021
1 parent 0bb606b commit ae74124
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 18 deletions.
10 changes: 10 additions & 0 deletions discovery/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package discovery

import "encoding/json"

type ModifySchemasRequest struct {
ServiceId string `protobuf:"bytes,1,opt,name=serviceId" json:"serviceId,omitempty"`
Schemas []*Schema `protobuf:"bytes,2,rep,name=schemas" json:"schemas,omitempty"`
Expand All @@ -28,6 +30,14 @@ type Schema struct {
Schema string `protobuf:"bytes,3,opt,name=schema" json:"schema,omitempty"`
}

func (s *Schema) GetType() string {
return "schema"
}

func (s *Schema) GetMarshalValue() ([]byte, error) {
return json.Marshal(s)
}

type ModifySchemasResponse struct {
Response *Response `protobuf:"bytes,1,opt,name=response" json:"-"`
}
Expand Down
10 changes: 10 additions & 0 deletions discovery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package discovery

import "encoding/json"

type MicroService struct {
ServiceId string `protobuf:"bytes,1,opt,name=serviceId" json:"serviceId,omitempty" bson:"service_id"`
AppId string `protobuf:"bytes,2,opt,name=appId" json:"appId,omitempty" bson:"app"`
Expand All @@ -38,6 +40,14 @@ type MicroService struct {
Framework *FrameWork `protobuf:"bytes,18,opt,name=framework" json:"framework,omitempty"`
}

func (m *MicroService) GetType() string {
return "service"
}

func (m *MicroService) GetMarshalValue() ([]byte, error) {
return json.Marshal(m)
}

// 删除服务请求
type DelServicesRequest struct {
ServiceIds []string `protobuf:"bytes,1,rep,name=serviceIds" json:"serviceIds,omitempty"`
Expand Down
13 changes: 12 additions & 1 deletion discovery/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package discovery

import "fmt"
import (
"encoding/json"
"fmt"
)

const (
MS_UP string = "UP"
Expand Down Expand Up @@ -379,6 +382,14 @@ type ConsumerDependency struct {
Override bool `protobuf:"varint,3,opt,name=override" json:"override,omitempty"`
}

func (c *ConsumerDependency) GetType() string {
return "dependency"
}

func (c *ConsumerDependency) GetMarshalValue() ([]byte, error) {
return json.Marshal(c)
}

func (cd *ConsumerDependency) String() string {
return fmt.Sprintf("&{%v %v %v}", cd.Consumer, cd.Providers, cd.Override)
}
Expand Down
10 changes: 10 additions & 0 deletions rbac/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package rbac

import "encoding/json"

type AccountResponse struct {
Total int64 `json:"total,omitempty"`
Accounts []*Account `json:"data,omitempty"`
Expand All @@ -36,6 +38,14 @@ type Account struct {
UpdateTime string `json:"updateTime,omitempty"`
}

func (a *Account) GetType() string {
return "account"
}

func (a *Account) GetMarshalValue() ([]byte, error) {
return json.Marshal(a)
}

func (a *Account) Check() error {
if a.Name == a.Password {
return ErrSameAsName
Expand Down
10 changes: 10 additions & 0 deletions rbac/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package rbac

import "encoding/json"

type RoleResponse struct {
Total int64 `json:"total,omitempty"`
Roles []*Role `json:"data,omitempty"`
Expand All @@ -29,3 +31,11 @@ type Role struct {
CreateTime string `json:"createTime,omitempty"`
UpdateTime string `json:"updateTime,omitempty"`
}

func (r *Role) GetType() string {
return "role"
}

func (r *Role) GetMarshalValue() ([]byte, error) {
return json.Marshal(r)
}
28 changes: 19 additions & 9 deletions sync/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@ const (
DoneStatus = "done"
)

// NewTask return task with action and datatype
func NewTask(domain, project, action, dataType string) (*Task, error) {
type Resource interface {
GetType() string
GetMarshalValue() ([]byte, error)
}

// NewTask return task with domain, project action and resource
func NewTask(domain, project, action string, resource Resource) (*Task, error) {
taskId, err := uuid.NewV4()
if err != nil {
return nil, err
}
resourceValue, err := resource.GetMarshalValue()
if err != nil {
return nil, err
}
return &Task{
TaskID: taskId.String(),
Action: action,
DataType: dataType,
Domain: domain,
Project: project,
Timestamp: time.Now().UnixNano(),
Status: PendingStatus,
ID: taskId.String(),
Domain: domain,
Project: project,
ResourceType: resource.GetType(),
Resource: resourceValue,
Action: action,
Timestamp: time.Now().UnixNano(),
Status: PendingStatus,
}, nil
}
16 changes: 8 additions & 8 deletions sync/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const (

// Task is db struct to store sync task
type Task struct {
TaskID string `json:"task_id" bson:"task_id"`
Action string `json:"action" bson:"action"`
DataType string `json:"data_type" bson:"data_type"`
Domain string `json:"domain" bson:"domain"`
Project string `json:"project" bson:"project"`
Data interface{} `json:"data" bson:"data"`
Timestamp int64 `json:"timestamp" bson:"timestamp"`
Status string `json:"status" bson:"status"`
ID string `json:"id" bson:"id"`
Domain string `json:"domain" bson:"domain"`
Project string `json:"project" bson:"project"`
ResourceType string `json:"resource_type" bson:"resource_type"`
Resource []byte `json:"resource" bson:"resource"`
Action string `json:"action" bson:"action"`
Timestamp int64 `json:"timestamp" bson:"timestamp"`
Status string `json:"status" bson:"status"`
}

// Tombstone is db struct to store the deleted resource information
Expand Down

0 comments on commit ae74124

Please # to comment.