From 3d454d9dc8ba2409046c0938618a70864e6eb8ef Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 25 Jul 2022 13:16:16 +0200 Subject: entity/dag: proper base operation for simplified implementation - reduce boilerplace necessary to implement an operation - consolidate what an operation is in the core, which in turn pave the way for a generic cache layer mechanism - avoid the previously complex unmarshalling process - support operation metadata from the core - simplified testing --- entity/dag/common_test.go | 77 ++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 41 deletions(-) (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index c2177683..774acba8 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -18,78 +18,73 @@ import ( Operations */ -type op1 struct { - author identity.Interface +const ( + _ OperationType = iota + Op1 + Op2 +) - OperationType int `json:"type"` - Field1 string `json:"field_1"` - Files []repository.Hash `json:"files"` +type op1 struct { + OpBase + Field1 string `json:"field_1"` + Files []repository.Hash `json:"files"` } func newOp1(author identity.Interface, field1 string, files ...repository.Hash) *op1 { - return &op1{author: author, OperationType: 1, Field1: field1, Files: files} + return &op1{OpBase: NewOpBase(Op1, author, 0), Field1: field1, Files: files} } -func (o *op1) Id() entity.Id { - data, _ := json.Marshal(o) - return entity.DeriveId(data) +func (op *op1) Id() entity.Id { + return IdOperation(op, &op.OpBase) } -func (o *op1) Validate() error { return nil } +func (op *op1) Validate() error { return nil } -func (o *op1) Author() identity.Interface { - return o.author -} - -func (o *op1) GetFiles() []repository.Hash { - return o.Files +func (op *op1) GetFiles() []repository.Hash { + return op.Files } type op2 struct { - author identity.Interface - - OperationType int `json:"type"` - Field2 string `json:"field_2"` + OpBase + Field2 string `json:"field_2"` } func newOp2(author identity.Interface, field2 string) *op2 { - return &op2{author: author, OperationType: 2, Field2: field2} + return &op2{OpBase: NewOpBase(Op2, author, 0), Field2: field2} } -func (o *op2) Id() entity.Id { - data, _ := json.Marshal(o) - return entity.DeriveId(data) +func (op *op2) Id() entity.Id { + return IdOperation(op, &op.OpBase) } -func (o *op2) Validate() error { return nil } - -func (o *op2) Author() identity.Interface { - return o.author -} +func (op *op2) Validate() error { return nil } -func unmarshaler(author identity.Interface, raw json.RawMessage, resolver identity.Resolver) (Operation, error) { +func unmarshaler(raw json.RawMessage, resolver identity.Resolver) (Operation, error) { var t struct { - OperationType int `json:"type"` + OperationType OperationType `json:"type"` } if err := json.Unmarshal(raw, &t); err != nil { return nil, err } + var op Operation + switch t.OperationType { - case 1: - op := &op1{} - err := json.Unmarshal(raw, &op) - op.author = author - return op, err - case 2: - op := &op2{} - err := json.Unmarshal(raw, &op) - op.author = author - return op, err + case Op1: + op = &op1{} + case Op2: + op = &op2{} default: return nil, fmt.Errorf("unknown operation type %v", t.OperationType) } + + err := json.Unmarshal(raw, &op) + if err != nil { + return nil, err + } + + return op, nil } /* -- cgit