1package dag
2
3import (
4 "encoding/json"
5 "fmt"
6
7 "github.com/MichaelMure/git-bug/entity"
8 "github.com/MichaelMure/git-bug/identity"
9 "github.com/MichaelMure/git-bug/repository"
10)
11
12// This file contains an example dummy entity to be used in the tests
13
14/*
15 Operations
16*/
17
18type op1 struct {
19 author identity.Interface
20
21 OperationType int `json:"type"`
22 Field1 string `json:"field_1"`
23}
24
25func newOp1(author identity.Interface, field1 string) *op1 {
26 return &op1{author: author, OperationType: 1, Field1: field1}
27}
28
29func (o op1) Id() entity.Id {
30 data, _ := json.Marshal(o)
31 return entity.DeriveId(data)
32}
33
34func (o op1) Author() identity.Interface {
35 return o.author
36}
37
38func (o op1) Validate() error { return nil }
39
40type op2 struct {
41 author identity.Interface
42
43 OperationType int `json:"type"`
44 Field2 string `json:"field_2"`
45}
46
47func newOp2(author identity.Interface, field2 string) *op2 {
48 return &op2{author: author, OperationType: 2, Field2: field2}
49}
50
51func (o op2) Id() entity.Id {
52 data, _ := json.Marshal(o)
53 return entity.DeriveId(data)
54}
55
56func (o op2) Author() identity.Interface {
57 return o.author
58}
59
60func (o op2) Validate() error { return nil }
61
62func unmarshaler(author identity.Interface, raw json.RawMessage) (Operation, error) {
63 var t struct {
64 OperationType int `json:"type"`
65 }
66
67 if err := json.Unmarshal(raw, &t); err != nil {
68 return nil, err
69 }
70
71 switch t.OperationType {
72 case 1:
73 op := &op1{}
74 err := json.Unmarshal(raw, &op)
75 op.author = author
76 return op, err
77 case 2:
78 op := &op2{}
79 err := json.Unmarshal(raw, &op)
80 op.author = author
81 return op, err
82 default:
83 return nil, fmt.Errorf("unknown operation type %v", t.OperationType)
84 }
85}
86
87/*
88 Identities + repo + definition
89*/
90
91func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Interface, Definition) {
92 repo := repository.NewMockRepo()
93
94 id1, err := identity.NewIdentity(repo, "name1", "email1")
95 if err != nil {
96 panic(err)
97 }
98 err = id1.Commit(repo)
99 if err != nil {
100 panic(err)
101 }
102 id2, err := identity.NewIdentity(repo, "name2", "email2")
103 if err != nil {
104 panic(err)
105 }
106 err = id2.Commit(repo)
107 if err != nil {
108 panic(err)
109 }
110
111 resolver := identityResolverFunc(func(id entity.Id) (identity.Interface, error) {
112 switch id {
113 case id1.Id():
114 return id1, nil
115 case id2.Id():
116 return id2, nil
117 default:
118 return nil, identity.ErrIdentityNotExist
119 }
120 })
121
122 def := Definition{
123 typename: "foo",
124 namespace: "foos",
125 operationUnmarshaler: unmarshaler,
126 identityResolver: resolver,
127 formatVersion: 1,
128 }
129
130 return repo, id1, id2, def
131}
132
133type identityResolverFunc func(id entity.Id) (identity.Interface, error)
134
135func (fn identityResolverFunc) ResolveIdentity(id entity.Id) (identity.Interface, error) {
136 return fn(id)
137}