-
Notifications
You must be signed in to change notification settings - Fork 23
/
metadata_incremental.go
91 lines (85 loc) · 2.43 KB
/
metadata_incremental.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
package util
import (
"context"
"errors"
"strings"
"github.com/jpfielding/gorets/pkg/metadata"
"github.com/jpfielding/gorets/pkg/rets"
)
// IncrementalCompact loads rets.CompcatMetadata in pieces
type IncrementalCompact rets.CompactMetadata
type cmgetter func(string, string) (*rets.CompactMetadata, error)
// Load retrieve the RETS Compact metadata from the server
func (ic *IncrementalCompact) Load(ctx context.Context, sess rets.Requester, url string) error {
// extract an id'd subesection of metadata
get := func(id, mtype string) (*rets.CompactMetadata, error) {
if id == "" {
id = "0"
}
params := rets.MetadataParams{
Format: "COMPACT",
MType: mtype,
ID: id,
}
req := rets.MetadataRequest{
URL: url,
MetadataParams: params,
}
reader, er := rets.MetadataStream(rets.MetadataResponse(ctx, sess, req))
if er != nil {
return nil, er
}
return rets.ParseMetadataCompactResult(reader)
}
msys, err := get("", metadata.MISystem.Name)
if err != nil {
return err
}
ic.Response = msys.Response
ic.MSystem = msys.MSystem
ic.Elements = map[string][]rets.CompactData{}
// fmt.Printf("compact system: %v\n", compact)
cds, err := ic.extractChildren(get, []string{}, metadata.MISystem)
if err != nil {
return err
}
for _, cd := range cds {
// extract children from this element and put in the system elem
ic.Elements[cd.Element] = append(ic.Elements[cd.Element], cd)
}
return nil
}
func (ic *IncrementalCompact) extractChildren(get cmgetter, path []string, mi metadata.MetaInfo) ([]rets.CompactData, error) {
var tmp []rets.CompactData
for _, child := range mi.Child {
cm, err := get(strings.Join(path, ":"), child.Name)
if err != nil {
return tmp, err
}
// errors
switch cm.Response.Code {
case rets.StatusOK:
case rets.StatusUnknownMetadataType, rets.StatusNoMetadataFound:
continue
default:
return tmp, errors.New(cm.Response.Text)
}
for _, cdata := range cm.Elements[child.Name] {
tmp = append(tmp, cdata)
// recurse on each member of this cdata
for _, each := range cdata.Entries() {
var data map[string]string = each
// fmt.Printf("compact system: %v\n", compact)
cds, err := ic.extractChildren(get, append(path, child.ID(data)), child)
if err != nil {
return tmp, err
}
for _, cd := range cds {
// extract children from this element and put in the system elem
tmp = append(tmp, cd)
}
}
}
}
return tmp, nil
}