allow setting nested metadata

This commit is contained in:
Alex Yatskov 2017-12-28 13:10:50 -08:00
parent 93fc38132b
commit c38395490d
3 changed files with 70 additions and 5 deletions

View File

@ -127,12 +127,11 @@ func (f *file) ModTime() time.Time {
}
func (f *file) Value(key string) (interface{}, bool) {
value, ok := f.Meta[key]
return value, ok
return getDelimValue(f.Meta, key)
}
func (f *file) SetValue(key string, value interface{}) {
f.Meta[key] = value
func (f *file) SetValue(key string, value interface{}) bool {
return setDelimValue(f.Meta, key, value)
}
func (f *file) CopyValues(src File) {

View File

@ -51,7 +51,7 @@ type File interface {
ModTime() time.Time
Value(key string) (interface{}, bool)
SetValue(key string, value interface{})
SetValue(key string, value interface{}) bool
CopyValues(src File)
Read(p []byte) (int, error)

66
util.go
View File

@ -25,6 +25,8 @@ package goldsmith
import (
"os"
"path/filepath"
"reflect"
"strings"
)
type fileInfo struct {
@ -55,3 +57,67 @@ func scanDir(root string, infos chan fileInfo) {
return nil
})
}
func setDelimValue(container interface{}, path string, data interface{}) bool {
containerVal := reflect.Indirect(reflect.ValueOf(container))
segments := strings.Split(path, ".")
segmentHead := segments[0]
if len(segments) > 1 {
var fieldVal reflect.Value
switch containerVal.Kind() {
case reflect.Map:
fieldVal = containerVal.MapIndex(reflect.ValueOf(segmentHead))
case reflect.Struct:
fieldVal = containerVal.FieldByName(segmentHead)
if fieldVal.CanAddr() {
fieldVal = fieldVal.Addr()
}
}
if fieldVal.IsValid() && fieldVal.CanInterface() {
pathRest := strings.Join(segments[1:], ".")
return setDelimValue(fieldVal.Interface(), pathRest, data)
}
} else {
switch containerVal.Kind() {
case reflect.Map:
containerVal.SetMapIndex(reflect.ValueOf(segmentHead), reflect.ValueOf(data))
return true
case reflect.Struct:
fieldVal := containerVal.FieldByName(segmentHead)
if fieldVal.CanSet() {
fieldVal.Set(reflect.ValueOf(data))
return true
}
}
}
return false
}
func getDelimValue(container interface{}, path string) (interface{}, bool) {
containerVal := reflect.Indirect(reflect.ValueOf(container))
segments := strings.Split(path, ".")
segmentHead := segments[0]
var fieldVal reflect.Value
switch containerVal.Kind() {
case reflect.Map:
fieldVal = containerVal.MapIndex(reflect.ValueOf(segmentHead))
case reflect.Struct:
fieldVal = containerVal.FieldByName(segmentHead)
}
if fieldVal.IsValid() && fieldVal.CanInterface() {
if len(segments) > 1 {
return getDelimValue(fieldVal.Interface(), strings.Join(segments[1:], "."))
}
return fieldVal.Interface(), true
}
return nil, false
}