-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
90 lines (80 loc) · 2.15 KB
/
interface.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package document
import (
"encoding/json"
"strings"
"time"
)
// SchemaLocation is where we host our document schema
const (
SchemaBase = "https://IpsoVeritas.github.io/schemas"
SchemaLocation = SchemaBase + "/v0"
)
// Document describes the base types on a document
type Document interface {
GetType() string
GetTimestamp() time.Time
GetCertificate() string
GetRaw() []byte
SetRaw([]byte)
}
func Marshal(doc Document) []byte {
docBytes, _ := json.Marshal(doc)
return docBytes
}
func Unmarshal(data []byte) (Document, error) {
base := &Base{}
if err := json.Unmarshal(data, &base); err != nil {
return base, err
}
base.SetRaw(data)
var typ Document
switch strings.Split(base.Type, "#")[0] {
case SchemaLocation + "/base.json":
return base, nil
case SchemaLocation + "/action.json":
typ = &Action{}
case SchemaLocation + "/action-descriptor.json":
typ = &ActionDescriptor{}
case SchemaLocation + "/certificate.json":
typ = &Certificate{}
case SchemaLocation + "/controller-binding.json":
typ = &ControllerBinding{}
case SchemaLocation + "/controller-descriptor.json":
typ = &ControllerDescriptor{}
case SchemaLocation + "/fact.json":
typ = &Fact{}
case SchemaLocation + "/mandate.json":
typ = &Mandate{}
case SchemaLocation + "/mandate-token.json":
typ = &MandateToken{}
case SchemaLocation + "/message.json":
typ = &Message{}
case SchemaLocation + "/multipart.json":
typ = &Multipart{}
case SchemaLocation + "/realm-descriptor.json":
typ = &RealmDescriptor{}
case SchemaLocation + "/receipt.json":
typ = &Receipt{}
case SchemaLocation + "/revocation-checksum.json":
typ = &RevocationChecksum{}
case SchemaLocation + "/revocation-request.json":
typ = &RevocationRequest{}
case SchemaLocation + "/revocation.json":
typ = &Revocation{}
case SchemaLocation + "/scope-request.json":
typ = &ScopeRequest{}
case SchemaLocation + "/signature-request.json":
typ = &SignatureRequest{}
default:
return base, nil
}
if err := json.Unmarshal(data, &typ); err != nil {
return base, err
}
typ.SetRaw(data)
return typ, nil
}
func GetType(data []byte) (string, error) {
b, err := Unmarshal(data)
return b.GetType(), err
}