Skip to content

Commit

Permalink
Add Restore API
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Mar 8, 2022
1 parent a19cddc commit 0f4b410
Show file tree
Hide file tree
Showing 25 changed files with 1,636 additions and 326 deletions.
24 changes: 22 additions & 2 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func fromCheckpoint(pbCheckpoint *api.Checkpoint) change.Checkpoint {
func FromChanges(pbChanges []*api.Change) ([]*change.Change, error) {
var changes []*change.Change
for _, pbChange := range pbChanges {
changeID, err := fromChangeID(pbChange.Id)
changeID, err := FromChangeID(pbChange.Id)
if err != nil {
return nil, err
}
Expand All @@ -115,7 +115,8 @@ func FromChanges(pbChanges []*api.Change) ([]*change.Change, error) {
return changes, nil
}

func fromChangeID(id *api.ChangeID) (change.ID, error) {
// FromChangeID converts the given Protobuf formats to model format.
func FromChangeID(id *api.ChangeID) (change.ID, error) {
actorID, err := time.ActorIDFromBytes(id.ActorId)
if err != nil {
return change.InitialID, err
Expand Down Expand Up @@ -210,6 +211,8 @@ func FromOperations(pbOps []*api.Operation) ([]operations.Operation, error) {
op, err = fromStyle(decoded.Style)
case *api.Operation_Increase_:
op, err = fromIncrease(decoded.Increase)
case *api.Operation_Snapshot_:
op, err = fromSnapshot(decoded.Snapshot)
default:
return nil, ErrUnsupportedOperation
}
Expand Down Expand Up @@ -452,6 +455,23 @@ func fromIncrease(pbInc *api.Operation_Increase) (*operations.Increase, error) {
), nil
}

func fromSnapshot(pbSnapshot *api.Operation_Snapshot) (*operations.Snapshot, error) {
executedAt, err := fromTimeTicket(pbSnapshot.ExecutedAt)
if err != nil {
return nil, err
}

root, err := BytesToObject(pbSnapshot.Snapshot)
if err != nil {
return nil, err
}

return operations.NewSnapshot(
root,
executedAt,
), nil
}

func fromCreatedAtMapByActor(
pbCreatedAtMapByActor map[string]*api.TimeTicket,
) (map[string]*time.Ticket, error) {
Expand Down
16 changes: 16 additions & 0 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func ToOperations(ops []operations.Operation) ([]*api.Operation, error) {
pbOperation.Body, err = toStyle(op)
case *operations.Increase:
pbOperation.Body, err = toIncrease(op)
case *operations.Snapshot:
pbOperation.Body, err = toSnapshot(op)
default:
return nil, ErrUnsupportedOperation
}
Expand Down Expand Up @@ -334,6 +336,20 @@ func toIncrease(increase *operations.Increase) (*api.Operation_Increase_, error)
}, nil
}

func toSnapshot(snapshot *operations.Snapshot) (*api.Operation_Snapshot_, error) {
bytes, err := ObjectToBytes(snapshot.Root())
if err != nil {
return nil, err
}

return &api.Operation_Snapshot_{
Snapshot: &api.Operation_Snapshot{
Snapshot: bytes,
ExecutedAt: ToTimeTicket(snapshot.ExecutedAt()),
},
}, nil
}

func toJSONElementSimple(elem json.Element) (*api.JSONElementSimple, error) {
switch elem := elem.(type) {
case *json.Object:
Expand Down
Loading

0 comments on commit 0f4b410

Please sign in to comment.