+35
-8
atproto/lexicon/resolve.go
+35
-8
atproto/lexicon/resolve.go
···
2
2
3
3
import (
4
4
"context"
5
+
"encoding/json"
5
6
"fmt"
6
7
"log/slog"
7
8
···
19
20
// Calling code should usually use ResolvingCatalog, which handles basic caching and validation of the Lexicon language itself.
20
21
func ResolveLexiconData(ctx context.Context, dir identity.Directory, nsid syntax.NSID) (map[string]any, error) {
21
22
23
+
record, err := resolveLexiconJSON(ctx, dir, nsid)
24
+
if err != nil {
25
+
return nil, err
26
+
}
27
+
28
+
d, err := data.UnmarshalJSON(*record)
29
+
if err != nil {
30
+
return nil, fmt.Errorf("fetched Lexicon schema record was invalid: %w", err)
31
+
}
32
+
return d, nil
33
+
}
34
+
35
+
// Low-level routine for resolving an NSID to `SchemaFile`.
36
+
//
37
+
// Same as `ResolveLexiconData`, but returns a parsed `SchemaFile` struct.
38
+
func ResolveLexiconSchemaFile(ctx context.Context, dir identity.Directory, nsid syntax.NSID) (*SchemaFile, error) {
39
+
record, err := resolveLexiconJSON(ctx, dir, nsid)
40
+
if err != nil {
41
+
return nil, err
42
+
}
43
+
44
+
var sf SchemaFile
45
+
if err := json.Unmarshal(*record, &sf); err != nil {
46
+
return nil, fmt.Errorf("fetched Lexicon schema record was invalid: %w", err)
47
+
}
48
+
return &sf, nil
49
+
}
50
+
51
+
// internal helper for fetching lexicon record as JSON bytes
52
+
func resolveLexiconJSON(ctx context.Context, dir identity.Directory, nsid syntax.NSID) (*json.RawMessage, error) {
22
53
baseDir := identity.BaseDirectory{}
23
54
did, err := baseDir.ResolveNSID(ctx, nsid)
24
55
if err != nil {
···
32
63
}
33
64
34
65
aturi := syntax.ATURI(fmt.Sprintf("at://%s/com.atproto.lexicon.schema/%s", did, nsid))
35
-
record, err := fetchRecord(ctx, *ident, aturi)
66
+
msg, err := fetchRecordJSON(ctx, *ident, aturi)
36
67
if err != nil {
37
68
return nil, err
38
69
}
39
-
return record, nil
70
+
return msg, err
40
71
}
41
72
42
-
func fetchRecord(ctx context.Context, ident identity.Identity, aturi syntax.ATURI) (map[string]any, error) {
73
+
func fetchRecordJSON(ctx context.Context, ident identity.Identity, aturi syntax.ATURI) (*json.RawMessage, error) {
43
74
44
75
slog.Debug("fetching record", "did", ident.DID.String(), "collection", aturi.Collection().String(), "rkey", aturi.RecordKey().String())
45
76
xrpcc := xrpc.Client{
···
53
84
if nil == resp.Value {
54
85
return nil, fmt.Errorf("empty record in response")
55
86
}
56
-
record, err := data.UnmarshalJSON(*resp.Value)
57
-
if err != nil {
58
-
return nil, fmt.Errorf("fetched record was invalid data: %w", err)
59
-
}
60
87
61
-
return record, nil
88
+
return resp.Value, nil
62
89
}