github.com/divyam234/rclone@v1.64.1/fs/metadata.go (about) 1 package fs 2 3 import "context" 4 5 // Metadata represents Object metadata in a standardised form 6 // 7 // See docs/content/metadata.md for the interpretation of the keys 8 type Metadata map[string]string 9 10 // MetadataHelp represents help for a bit of system metadata 11 type MetadataHelp struct { 12 Help string 13 Type string 14 Example string 15 ReadOnly bool 16 } 17 18 // MetadataInfo is help for the whole metadata for this backend. 19 type MetadataInfo struct { 20 System map[string]MetadataHelp 21 Help string 22 } 23 24 // Set k to v on m 25 // 26 // If m is nil, then it will get made 27 func (m *Metadata) Set(k, v string) { 28 if *m == nil { 29 *m = make(Metadata, 1) 30 } 31 (*m)[k] = v 32 } 33 34 // Merge other into m 35 // 36 // If m is nil, then it will get made 37 func (m *Metadata) Merge(other Metadata) { 38 for k, v := range other { 39 if *m == nil { 40 *m = make(Metadata, len(other)) 41 } 42 (*m)[k] = v 43 } 44 } 45 46 // MergeOptions gets any Metadata from the options passed in and 47 // stores it in m (which may be nil). 48 // 49 // If there is no m then metadata will be nil 50 func (m *Metadata) MergeOptions(options []OpenOption) { 51 for _, opt := range options { 52 if metadataOption, ok := opt.(MetadataOption); ok { 53 m.Merge(Metadata(metadataOption)) 54 } 55 } 56 } 57 58 // GetMetadata from an ObjectInfo 59 // 60 // If the object has no metadata then metadata will be nil 61 func GetMetadata(ctx context.Context, o ObjectInfo) (metadata Metadata, err error) { 62 do, ok := o.(Metadataer) 63 if !ok { 64 return nil, nil 65 } 66 return do.Metadata(ctx) 67 } 68 69 // GetMetadataOptions from an ObjectInfo and merge it with any in options 70 // 71 // If --metadata isn't in use it will return nil 72 // 73 // If the object has no metadata then metadata will be nil 74 func GetMetadataOptions(ctx context.Context, o ObjectInfo, options []OpenOption) (metadata Metadata, err error) { 75 ci := GetConfig(ctx) 76 if !ci.Metadata { 77 return nil, nil 78 } 79 metadata, err = GetMetadata(ctx, o) 80 if err != nil { 81 return nil, err 82 } 83 metadata.MergeOptions(options) 84 return metadata, nil 85 }