-
Notifications
You must be signed in to change notification settings - Fork 26
/
list.go
76 lines (72 loc) · 1.85 KB
/
list.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
package fileref
import (
"github.com/0chain/errors"
"github.com/mitchellh/mapstructure"
)
type ListResult struct {
AllocationRoot string `json:"allocation_root"`
Meta map[string]interface{} `json:"meta_data"`
Entities []map[string]interface{} `json:"list"`
}
func (lr *ListResult) GetDirTree(allocationID string) (*Ref, error) {
if lr.Meta == nil {
return nil, errors.New("invalid_list_path", "badly formatted list result, nil meta")
}
reftype := lr.Meta["type"].(string)
if reftype == DIRECTORY {
rootRef := &Ref{Type: DIRECTORY}
rootRef.AllocationID = allocationID
var md mapstructure.Metadata
config := &mapstructure.DecoderConfig{
Metadata: &md,
Result: rootRef,
TagName: "mapstructure",
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return nil, err
}
err = decoder.Decode(lr.Meta)
if err != nil {
return nil, err
}
err = lr.populateChildren(rootRef)
if err != nil {
return nil, err
}
return rootRef, nil
}
return nil, errors.New("invalid_list_path", "Invalid list path. list was not for a directory")
}
func (lr *ListResult) populateChildren(ref *Ref) error {
for _, rpc := range lr.Entities {
reftype := rpc["type"].(string)
var childEntity RefEntity
if reftype == DIRECTORY {
dref := &Ref{Type: DIRECTORY}
dref.AllocationID = ref.AllocationID
childEntity = dref
} else {
fref := &FileRef{}
fref.Type = FILE
fref.AllocationID = ref.AllocationID
childEntity = fref
}
var md mapstructure.Metadata
config := &mapstructure.DecoderConfig{
Metadata: &md,
Result: childEntity,
TagName: "mapstructure",
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
err = decoder.Decode(rpc)
if err != nil {
return err
}
ref.Children = append(ref.Children, childEntity)
}
return nil
}