/
iscnrecord.go
106 lines (101 loc) · 2.97 KB
/
iscnrecord.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package types
import (
"encoding/json"
"fmt"
"net/url"
"time"
)
func ValidateFingerprints(fingerprints []string) error {
usedFingerprint := map[string]struct{}{}
for _, fingerprint := range fingerprints {
_, ok := usedFingerprint[fingerprint]
if ok {
return fmt.Errorf("repeated fingerprint entry")
}
usedFingerprint[fingerprint] = struct{}{}
u, err := url.ParseRequestURI(fingerprint)
if err != nil {
return fmt.Errorf("invalid fingerprint URL %s: %w", fingerprint, err)
}
if u.Scheme == "" {
return fmt.Errorf("empty fingerprint URL scheme in fingerprint %s", fingerprint)
}
}
return nil
}
func (record *IscnRecord) Validate() error {
err := ValidateFingerprints(record.ContentFingerprints)
if err != nil {
return fmt.Errorf("invalid content fingerprints: %w", err)
}
for _, stakeholder := range record.Stakeholders {
err := stakeholder.Validate()
if err != nil {
return err
}
}
err = record.ContentMetadata.Validate()
if err != nil {
return err
}
return nil
}
type IscnRecordJsonLdInfo struct {
Id IscnId
Timestamp time.Time
ParentIpld *CID
}
func (record *IscnRecord) ToJsonLd(info *IscnRecordJsonLdInfo) ([]byte, error) {
stakeholders := []interface{}{}
for _, stakeholder := range record.Stakeholders {
normalizedStakeholder, err := stakeholder.Normalize()
if err != nil {
return nil, err
}
stakeholders = append(stakeholders, normalizedStakeholder)
}
normalizedContentMetadata, err := record.ContentMetadata.Normalize()
if err != nil {
return nil, err
}
fingerprints := record.ContentFingerprints
if fingerprints == nil {
// hack: nil could be used for empty array in Go, but will be converted into `null` instead of `[]` when marshalling to JSON
// so we set it back to an empty array before marshalling, so the field will not be `null`
fingerprints = []string{}
}
recordMap := map[string]interface{}{
"@context": map[string]interface{}{
"@vocab": "http://iscn.io/",
"recordParentIPLD": map[string]interface{}{
"@container": "@index",
},
"stakeholders": map[string]interface{}{
"@context": map[string]interface{}{
"@vocab": "http://schema.org/",
"entity": "http://iscn.io/entity",
"rewardProportion": "http://iscn.io/rewardProportion",
"contributionType": "http://iscn.io/contributionType",
"footprint": "http://iscn.io/footprint",
},
},
"contentMetadata": map[string]interface{}{
"@context": nil,
},
},
"@type": "Record",
"@id": info.Id.String(),
"recordTimestamp": info.Timestamp.UTC().Format("2006-01-02T15:04:05-07:00"),
"recordVersion": info.Id.Version,
"recordNotes": record.RecordNotes,
"contentFingerprints": fingerprints,
"stakeholders": stakeholders,
"contentMetadata": normalizedContentMetadata,
}
if info.ParentIpld != nil {
recordMap["recordParentIPLD"] = map[string]string{
"/": info.ParentIpld.String(),
}
}
return json.Marshal(recordMap)
}