+12
atproto/data/data.go
+12
atproto/data/data.go
···
7
7
cbor "github.com/ipfs/go-ipld-cbor"
8
8
)
9
9
10
+
// Checks that generic data (object) complies with the atproto data model.
10
11
func Validate(obj map[string]any) error {
11
12
_, err := parseObject(obj)
12
13
return err
13
14
}
14
15
16
+
// Parses generic data (object) in JSON, validating against the atproto data model at the same time.
17
+
//
18
+
// The standard library's MarshalJSON can be used to invert this function.
15
19
func UnmarshalJSON(b []byte) (map[string]any, error) {
16
20
var rawObj map[string]any
17
21
err := json.Unmarshal(b, &rawObj)
···
25
29
return out, nil
26
30
}
27
31
32
+
// Parses generic data (object) in CBOR (specifically, IPLD dag-cbor), validating against the atproto data model at the same time.
28
33
func UnmarshalCBOR(b []byte) (map[string]any, error) {
29
34
var rawObj map[string]any
30
35
err := cbor.DecodeInto(b, &rawObj)
···
38
43
return out, nil
39
44
}
40
45
46
+
// Serializes generic atproto data (object) to DAG-CBOR bytes
47
+
//
48
+
// Does not re-validate that data conforms to atproto data model, but does handle Blob, Bytes, and CIDLink as expected.
41
49
func MarshalCBOR(obj map[string]any) ([]byte, error) {
42
50
return cbor.DumpObject(forCBOR(obj))
43
51
}
44
52
53
+
// helper to get generic data in the correct "shape" for serialization with ipfs/go-ipld-cbor
45
54
func forCBOR(obj map[string]any) map[string]any {
55
+
// NOTE: a faster version might mutate the map in-place instead of copying (many allocations)?
46
56
out := make(map[string]any, len(obj))
47
57
for k, val := range obj {
48
58
switch v := val.(type) {
···
69
79
return out
70
80
}
71
81
82
+
// recursive helper for forCBOR
72
83
func forCBORArray(arr []any) []any {
84
+
// NOTE: a faster version might mutate the array in-place instead of copying (many allocations)?
73
85
out := make([]any, len(arr))
74
86
for i, val := range arr {
75
87
switch v := val.(type) {
+15
-1
atproto/data/doc.go
+15
-1
atproto/data/doc.go
···
1
1
/*
2
-
Package data supports serializaiton and deserialization of atproto data model content
2
+
Package data supports schema-less serializaiton and deserialization of atproto data
3
+
4
+
Some restrictions from the data model include:
5
+
- string sizes
6
+
- array and object element counts
7
+
- the "shape" of $bytes and $blob data objects
8
+
- $type must contain a non-empty string
9
+
10
+
Details are specified at https://atproto.com/specs/data-model
11
+
12
+
This package includes types (CIDLink, Bytes, Blob) which are represent the corresponding atproto data model types. These implement JSON and CBOR marshaling in (with whyrusleeping/cbor-gen) the expected way.
13
+
14
+
Can parse generic atproto records (or other objects) in JSON or CBOR format in to map[string]interface{}, while validating atproto-specific constraints on data (eg, that cid-link objects have only a single field).
15
+
16
+
Has a helper for serializing generic data (map[string]interface{}) to CBOR, which handles converting JSON-style object types (like $link and $bytes) as needed. There is no "MarshalJSON" method; simply use the standard library's `encoding/json`.
3
17
*/
4
18
package data