1package board
 2
 3import (
 4	"encoding/json"
 5	"fmt"
 6
 7	"github.com/MichaelMure/git-bug/entity"
 8	"github.com/MichaelMure/git-bug/entity/dag"
 9)
10
11// OperationType is an operation type identifier
12type OperationType dag.OperationType
13
14const (
15	_ dag.OperationType = iota
16	CreateOp
17	SetTitleOp
18	SetDescriptionOp
19	AddItemEntityOp
20	AddItemDraftOp
21	MoveItemOp
22	RemoveItemOp
23
24	// TODO: change columns?
25)
26
27type Operation interface {
28	dag.Operation
29	// Apply the operation to a Snapshot to create the final state
30	Apply(snapshot *Snapshot)
31}
32
33func operationUnmarshaller(raw json.RawMessage, resolvers entity.Resolvers) (dag.Operation, error) {
34	var t struct {
35		OperationType dag.OperationType `json:"type"`
36	}
37
38	if err := json.Unmarshal(raw, &t); err != nil {
39		return nil, err
40	}
41
42	var op dag.Operation
43
44	switch t.OperationType {
45	case CreateOp:
46		op = &CreateOperation{}
47	case SetTitleOp:
48		op = &SetTitleOperation{}
49	case SetDescriptionOp:
50		op = &SetDescriptionOperation{}
51	case AddItemDraftOp:
52		op = &AddItemDraftOperation{}
53	case AddItemEntityOp:
54		op = &AddItemEntityOperation{}
55	default:
56		panic(fmt.Sprintf("unknown operation type %v", t.OperationType))
57	}
58
59	err := json.Unmarshal(raw, &op)
60	if err != nil {
61		return nil, err
62	}
63
64	switch op := op.(type) {
65	case *AddItemEntityOperation:
66		// TODO: resolve entity
67		op.item = struct{}{}
68	}
69
70	return op, nil
71}