-
Notifications
You must be signed in to change notification settings - Fork 39
/
converters.go
67 lines (55 loc) · 1.74 KB
/
converters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package v2
import (
"encoding/json"
"io/ioutil"
"net/http"
coredocumentpb "github.com/centrifuge/centrifuge-protobufs/gen/go/coredocument"
"github.com/centrifuge/go-centrifuge/documents"
"github.com/centrifuge/go-centrifuge/httpapi/coreapi"
"github.com/centrifuge/go-centrifuge/jobs"
"github.com/centrifuge/go-centrifuge/utils/byteutils"
)
func toDocumentsPayload(req DocumentRequest, docID []byte) (payload documents.UpdatePayload, err error) {
cp, err := coreapi.ToDocumentsCreatePayload(req)
if err != nil {
return payload, err
}
return documents.UpdatePayload{CreatePayload: cp, DocumentID: docID}, nil
}
func toDocumentResponse(doc documents.Model, tokenRegistry documents.TokenRegistry, jobID jobs.JobID) (coreapi.DocumentResponse, error) {
resp, err := coreapi.GetDocumentResponse(doc, tokenRegistry, jobID)
if err != nil {
return resp, err
}
resp.Header.Status = string(doc.GetStatus())
return resp, err
}
// unmarshalBody unmarshals req.Body to val.
// val should always be a pointer to the struct.
func unmarshalBody(r *http.Request, val interface{}) error {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return json.Unmarshal(data, val)
}
func toClientRole(r *coredocumentpb.Role) Role {
return Role{
ID: r.RoleKey,
Collaborators: byteutils.ToHexByteSlice(r.Collaborators),
}
}
func toClientRule(r *coredocumentpb.TransitionRule) TransitionRule {
return TransitionRule{
RuleID: r.RuleKey,
Roles: byteutils.ToHexByteSlice(r.Roles),
Field: r.Field,
Action: coredocumentpb.TransitionAction_name[int32(r.Action)],
}
}
func toClientRules(rules []*coredocumentpb.TransitionRule) (tr TransitionRules) {
for _, r := range rules {
tr.Rules = append(tr.Rules, toClientRule(r))
}
return tr
}