set.go 529 B

1234567891011121314151617181920212223242526
  1. package mxj
  2. import (
  3. "strings"
  4. )
  5. // Sets the value for the path
  6. func (mv Map) SetValueForPath(value interface{}, path string) error {
  7. pathAry := strings.Split(path, ".")
  8. parentPathAry := pathAry[0 : len(pathAry)-1]
  9. parentPath := strings.Join(parentPathAry, ".")
  10. val, err := mv.ValueForPath(parentPath)
  11. if err != nil {
  12. return err
  13. }
  14. if val == nil {
  15. return nil // we just ignore the request if there's no val
  16. }
  17. key := pathAry[len(pathAry)-1]
  18. cVal := val.(map[string]interface{})
  19. cVal[key] = value
  20. return nil
  21. }