github.com/jdolitsky/cnab-go@v0.7.1-beta1/bundle/replacement/utils.go (about)

     1  package replacement
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // Abstraction over map to permit generic traversal and substitution
     8  type docmap interface {
     9  	get(key string) (interface{}, bool)
    10  	set(key string, value interface{})
    11  	asInstance(value interface{}) (docmap, bool)
    12  }
    13  
    14  func parseSelector(selector string) []string {
    15  	return strings.Split(selector, ".")
    16  }
    17  
    18  func replaceIn(dict docmap, selectorPath []string, value string) error {
    19  	entry, ok := dict.get(selectorPath[0])
    20  	if !ok {
    21  		return ErrSelectorNotFound
    22  	}
    23  
    24  	if len(selectorPath) == 1 {
    25  		dict.set(selectorPath[0], value)
    26  		return nil
    27  	}
    28  
    29  	entryDict, ok := dict.asInstance(entry)
    30  	if !ok {
    31  		return ErrSelectorNotFound // Because we have reached a terminal with some of the selectorPath to go
    32  	}
    33  	rest := selectorPath[1:]
    34  
    35  	return replaceIn(entryDict, rest, value)
    36  }