-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
80 lines (72 loc) · 1.76 KB
/
parse.go
File metadata and controls
80 lines (72 loc) · 1.76 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
package tap
import (
"encoding/json"
"fmt"
didcomm "github.com/Notabene-id/go-didcomm"
)
// TAPBody is the interface implemented by all TAP message body types.
type TAPBody interface {
TAPType() string
}
// IsTAPMessage returns true if the DIDComm message type is a known TAP type.
func IsTAPMessage(msg *didcomm.Message) bool {
for _, t := range AllTypes() {
if msg.Type == t {
return true
}
}
return false
}
// ParseBody unmarshals a DIDComm message body into the appropriate typed TAP body struct
// based on the message type.
func ParseBody(msg *didcomm.Message) (TAPBody, error) {
var body TAPBody
switch msg.Type {
case TypeTransfer:
body = &TransferBody{}
case TypePayment:
body = &PaymentBody{}
case TypeRFQ:
body = &RFQBody{}
case TypeQuote:
body = &QuoteBody{}
case TypeLock:
body = &LockBody{}
case TypeAuthorize:
body = &AuthorizeBody{}
case TypeAuthorizationRequired:
body = &AuthorizationRequiredBody{}
case TypeSettle:
body = &SettleBody{}
case TypeReject:
body = &RejectBody{}
case TypeCancel:
body = &CancelBody{}
case TypeRevert:
body = &RevertBody{}
case TypeCapture:
body = &CaptureBody{}
case TypeUpdateAgent:
body = &UpdateAgentBody{}
case TypeUpdateParty:
body = &UpdatePartyBody{}
case TypeAddAgents:
body = &AddAgentsBody{}
case TypeReplaceAgent:
body = &ReplaceAgentBody{}
case TypeRemoveAgent:
body = &RemoveAgentBody{}
case TypeConfirmRelationship:
body = &ConfirmRelationshipBody{}
case TypeUpdatePolicies:
body = &UpdatePoliciesBody{}
case TypeConnect:
body = &ConnectBody{}
default:
return nil, fmt.Errorf("%w: %s", ErrUnknownMessageType, msg.Type)
}
if err := json.Unmarshal(msg.Body, body); err != nil {
return nil, fmt.Errorf("%w: %w", ErrInvalidBody, err)
}
return body, nil
}