-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract.go
73 lines (67 loc) · 1.94 KB
/
extract.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
package getdoc
import (
"bytes"
"context"
"fmt"
"path"
"github.com/gotd/getdoc/dl"
)
// Downloader abstracts documentation fetching.
type Downloader interface {
Get(ctx context.Context, layer int, key string) ([]byte, error)
}
// Extract uses Downloader to extract documentation.
func Extract(ctx context.Context, d Downloader) (*Doc, error) {
return ExtractLayer(ctx, dl.NoLayer, d)
}
// ExtractLayer uses Downloader to extract documentation of specified layer.
func ExtractLayer(ctx context.Context, layer int, d Downloader) (*Doc, error) {
data, err := d.Get(ctx, layer, "schema")
if err != nil {
return nil, fmt.Errorf("failed to get schema: %w", err)
}
index, err := ParseIndex(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("failed to extract index: %w", err)
}
doc := &Doc{
Index: *index,
Methods: map[string]Method{},
Types: map[string]Type{},
Constructors: map[string]Constructor{},
Errors: map[string]Error{},
}
for _, category := range index.Categories {
for _, v := range category.Values {
data, err := d.Get(ctx, index.Layer, path.Join(category.Name, v))
if err != nil {
return nil, fmt.Errorf("fetch(%s/%s) failed: %w", category.Name, v, err)
}
reader := bytes.NewReader(data)
switch category.Name {
case CategoryConstructor:
t, err := ParseConstructor(reader)
if err != nil {
return nil, fmt.Errorf("parse(%s/%s) failed: %w", category.Name, v, err)
}
doc.Constructors[t.Name] = *t
case CategoryType:
t, err := ParseType(reader)
if err != nil {
return nil, fmt.Errorf("parse(%s/%s) failed: %w", category.Name, v, err)
}
doc.Types[t.Name] = *t
case CategoryMethod:
t, err := ParseMethod(reader)
if err != nil {
return nil, fmt.Errorf("parse(%s/%s) failed: %w", category.Name, v, err)
}
doc.Methods[t.Name] = *t
for _, e := range t.Errors {
doc.Errors[e.Type] = e
}
}
}
}
return doc, nil
}