Beispiel #1
0
func (lazy LazyMap) Get(path []string) (value interface{}, has_key bool) {
	if len(path) == 0 {
		return lazy, true
	}

	processed, ok := lazy.processed[strings.Join(path, ".")]
	if ok {
		value, has_key = processed.Get(path)
		if has_key {
			lazy.processed[strings.Join(path, ".")] = fallbackmap.NewDeepSingle(path, value)
			return value, true
		}

		lazy.processed[strings.Join(path, ".")] = fallbackmap.DeepNil
		return nil, false
	}

	value, has_key = lazy.deep.Get(path)
	if has_key {
		var newKey interface{}
		newKey, value = template.Walk([]interface{}{path[len(path)-1]}, value, lazy.rules)
		if newKey == nil {
			return nil, false
		}
		if newKey == path[len(path)-1] {
			lazy.processed[strings.Join(path, ".")] = fallbackmap.NewDeepSingle(path, value)
			return value, true
		}

		return nil, false
	}

	if len(path) == 1 {
		return nil, false
	}

	head := path[:len(path)-1]
	_, has_key = lazy.Get(head)
	if has_key {
		value, has_key = lazy.processed[strings.Join(head, ".")].Get(path)
		if has_key {
			lazy.processed[strings.Join(path, ".")] = fallbackmap.NewDeepSingle(path, value)
			return value, true
		}
	}

	return nil, false
}
Beispiel #2
0
func TestFnHasRef_Basic(t *testing.T) {
	deep := fallbackmap.NewDeepSingle([]string{"BoundValue"}, "aValue")

	hasRef := MakeFnHasRef(&deep)
	inputs := map[string]bool{
		"UnboundValue": false,
		"BoundValue":   true,
	}

	for key, expected := range inputs {
		input := interface{}(map[string]interface{}{"Fn::HasRef": key})
		newKey, newNode := hasRef([]interface{}{"x", "y"}, input)

		if newKey != "y" {
			t.Fatalf("HasRef modified the path (%v instead of %v)", newKey, "y")
		}

		if !reflect.DeepEqual(newNode, expected) {
			t.Fatalf("HasRef of %v did not return %#v (returned %#v instead)", key, expected, newNode)
		}
	}
}