Example #1
0
func MakeRef(sources fallbackmap.Deep, rules *template.Rules) template.Rule {
	return func(path []interface{}, node interface{}) (interface{}, interface{}) {
		key := interface{}(nil)
		if len(path) > 0 {
			key = path[len(path)-1]
		}

		argInterface, ok := singleKey(node, "Ref")
		if !ok {
			return key, node //passthru
		}

		var argString string
		if argString, ok = argInterface.(string); !ok {
			return key, node //passthru
		}

		var refpath []string
		for _, part := range deepalias.Split(argString) {
			refpath = append(refpath, part)
		}

		var newNode interface{}
		newNode, ok = sources.Get(refpath)
		if ok {
			var newKey interface{}
			newKey, newNode = template.Walk(path, newNode, rules)
			return newKey, newNode
		}

		return key, node //passthru (ref not found)
	}
}
Example #2
0
func testGetNil(i fallbackmap.Deep, path []string, t *testing.T) {
	v, ok := i.Get(path)

	if v != nil {
		t.Fatalf("getting of %v returned nil", path)
	}

	if ok {
		t.Fatalf("getting of %v returned a result", path)
	}
}
Example #3
0
func testGetString(i fallbackmap.Deep, path []string, value string, t *testing.T) {
	v, ok := i.Get(path)
	if !ok {
		t.Fatalf("getting of %v did not return a result", path)
	}

	vs, ok := v.(string)
	if !ok {
		t.Fatalf("getting of %v did not return an string (got %v instead)", path, v)
	}

	if vs != value {
		t.Fatalf("getting of %v did not return expected result (%v)", path, value)
	}
}
Example #4
0
func testGetInt(i fallbackmap.Deep, path []string, value int, t *testing.T) {
	v, ok := i.Get(path)
	if !ok {
		t.Fatalf("getting of %v did not return a result", path)
	}

	vi, ok := v.(int)
	if !ok {
		t.Fatalf("getting of %v did not return an int (got %v instead)", path, v)
	}

	if vi != value {
		t.Fatalf("getting of %v did not return expected result (%v)", path, value)
	}
}
Example #5
0
func DeAlias(path []string, deep fallbackmap.Deep) ([]string, bool) {
	did_translate := false
	translated := []string{}
	for _, component := range path {
		if strings.HasPrefix(component, "[") && strings.HasSuffix(component, "]") {
			alias_path := strings.Split(component[1:len(component)-1], ".")
			dereferenced_component, found := deep.Get(alias_path)
			if found {
				dereferenced_component_string, ok := dereferenced_component.(string)
				if ok {
					component = dereferenced_component_string
					did_translate = true
				}
			}
		}

		translated = append(translated, component)
	}

	return translated, did_translate
}