github.com/0chain/gosdk@v1.17.11/zboxcore/fileref/list.go (about) 1 package fileref 2 3 import ( 4 "github.com/0chain/errors" 5 "github.com/mitchellh/mapstructure" 6 ) 7 8 type ListResult struct { 9 AllocationRoot string `json:"allocation_root"` 10 Meta map[string]interface{} `json:"meta_data"` 11 Entities []map[string]interface{} `json:"list"` 12 } 13 14 func (lr *ListResult) GetDirTree(allocationID string) (*Ref, error) { 15 if lr.Meta == nil { 16 return nil, errors.New("invalid_list_path", "badly formatted list result, nil meta") 17 } 18 reftype := lr.Meta["type"].(string) 19 if reftype == DIRECTORY { 20 rootRef := &Ref{Type: DIRECTORY} 21 rootRef.AllocationID = allocationID 22 var md mapstructure.Metadata 23 config := &mapstructure.DecoderConfig{ 24 Metadata: &md, 25 Result: rootRef, 26 TagName: "mapstructure", 27 } 28 decoder, err := mapstructure.NewDecoder(config) 29 if err != nil { 30 return nil, err 31 } 32 err = decoder.Decode(lr.Meta) 33 if err != nil { 34 return nil, err 35 } 36 err = lr.populateChildren(rootRef) 37 if err != nil { 38 return nil, err 39 } 40 return rootRef, nil 41 } 42 return nil, errors.New("invalid_list_path", "Invalid list path. list was not for a directory") 43 } 44 45 func (lr *ListResult) populateChildren(ref *Ref) error { 46 for _, rpc := range lr.Entities { 47 reftype := rpc["type"].(string) 48 var childEntity RefEntity 49 if reftype == DIRECTORY { 50 dref := &Ref{Type: DIRECTORY} 51 dref.AllocationID = ref.AllocationID 52 childEntity = dref 53 } else { 54 fref := &FileRef{} 55 fref.Type = FILE 56 fref.AllocationID = ref.AllocationID 57 childEntity = fref 58 } 59 var md mapstructure.Metadata 60 config := &mapstructure.DecoderConfig{ 61 Metadata: &md, 62 Result: childEntity, 63 TagName: "mapstructure", 64 } 65 decoder, err := mapstructure.NewDecoder(config) 66 if err != nil { 67 return err 68 } 69 err = decoder.Decode(rpc) 70 if err != nil { 71 return err 72 } 73 ref.Children = append(ref.Children, childEntity) 74 } 75 return nil 76 }