aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation_pack.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-15 13:15:00 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-15 13:15:00 +0200
commit7bec0b1f134d213e7505fc2ac03ffea26f2193cc (patch)
treee263cccd84406843eacbc6bd184acdacb25a49d1 /bug/operation_pack.go
parentb478cd1bcb4756b20f7f4b15fcf81f23e1a60a02 (diff)
downloadgit-bug-7bec0b1f134d213e7505fc2ac03ffea26f2193cc.tar.gz
bug: add a data validation process to avoid merging incorrect operations
Diffstat (limited to 'bug/operation_pack.go')
-rw-r--r--bug/operation_pack.go17
1 files changed, 15 insertions, 2 deletions
diff --git a/bug/operation_pack.go b/bug/operation_pack.go
index fe26952f..03d538d5 100644
--- a/bug/operation_pack.go
+++ b/bug/operation_pack.go
@@ -7,6 +7,7 @@ import (
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/git"
+ "github.com/pkg/errors"
)
const formatVersion = 1
@@ -24,8 +25,10 @@ type OperationPack struct {
commitHash git.Hash
}
+// hold the different operation type to instantiate to parse JSON
var operations map[OperationType]reflect.Type
+// Register will register a new type of Operation to be able to parse the corresponding JSON
func Register(t OperationType, op interface{}) {
if operations == nil {
operations = make(map[OperationType]reflect.Type)
@@ -96,8 +99,18 @@ func (opp *OperationPack) IsEmpty() bool {
}
// IsValid tell if the OperationPack is considered valid
-func (opp *OperationPack) IsValid() bool {
- return !opp.IsEmpty()
+func (opp *OperationPack) Validate() error {
+ if opp.IsEmpty() {
+ return fmt.Errorf("empty")
+ }
+
+ for _, op := range opp.Operations {
+ if err := op.Validate(); err != nil {
+ return errors.Wrap(err, "op")
+ }
+ }
+
+ return nil
}
// Write will serialize and store the OperationPack as a git blob and return