From 4388e4bab99d905b5999965b87a74794f4796e75 Mon Sep 17 00:00:00 2001 From: Kelsey Hightower Date: Wed, 24 Feb 2016 00:42:53 -0800 Subject: [PATCH] getv accepts a default value The `getv` template function does not accept a default value. The workaround is to write templates like this: {{if exists "/example/app/option"}} option = {{getv "/example/app/option"}}{{else}}default_value{{end}} Resolve the issue by accepting an optional default value. Templates can now be written like this: value: {{getv "/key" "default_value"}} This change is backwards compatible with the current `getv` function. If the key is missing and a default value is given the missing key error will be suppressed and the default value will be returned. If the key is missing and no default value is given the `getv` function will return an error and template processing will halt. Fixes #219 --- Godeps/Godeps.json | 5 ++- docs/templates.md | 9 ++++- .../coreos/etcd/pkg/pathutil/path_test.go | 38 ------------------- .../github.com/kelseyhightower/memkv/store.go | 10 ++++- 4 files changed, 21 insertions(+), 41 deletions(-) delete mode 100644 vendor/github.com/coreos/etcd/pkg/pathutil/path_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 613f6f67b..8b15ffb69 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,6 +1,9 @@ { "ImportPath": "github.com/kelseyhightower/confd", "GoVersion": "go1.6", + "Packages": [ + "backends/.." + ], "Deps": [ { "ImportPath": "github.com/BurntSushi/toml", @@ -119,7 +122,7 @@ }, { "ImportPath": "github.com/kelseyhightower/memkv", - "Rev": "32a4556de2a1aab8ea4c8600f5ea24db3fbd8908" + "Rev": "71620c547230ad53777a00e2ebd3bbab8353370e" }, { "ImportPath": "github.com/mitchellh/mapstructure", diff --git a/docs/templates.md b/docs/templates.md index bd1da8921..a6374d36b 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -52,12 +52,19 @@ Returns all KVPair, []KVPair, where key matches its argument. Returns an error i ### getv -Returns the value as a string where key matches its argument. Returns an error if key is not found. +Returns the value as a string where key matches its argument or an optional default value. +Returns an error if key is not found and no default value given. ``` value: {{getv "/key"}} ``` +#### With a default value + +``` +value: {{getv "/key" "default_value"}} +``` + ### getvs Returns all values, []string, where key matches its argument. Returns an error if key is not found. diff --git a/vendor/github.com/coreos/etcd/pkg/pathutil/path_test.go b/vendor/github.com/coreos/etcd/pkg/pathutil/path_test.go deleted file mode 100644 index 6d3d803cf..000000000 --- a/vendor/github.com/coreos/etcd/pkg/pathutil/path_test.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2015 CoreOS, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package pathutil - -import "testing" - -func TestCanonicalURLPath(t *testing.T) { - tests := []struct { - p string - wp string - }{ - {"/a", "/a"}, - {"", "/"}, - {"a", "/a"}, - {"//a", "/a"}, - {"/a/.", "/a"}, - {"/a/..", "/"}, - {"/a/", "/a/"}, - {"/a//", "/a/"}, - } - for i, tt := range tests { - if g := CanonicalURLPath(tt.p); g != tt.wp { - t.Errorf("#%d: canonical path = %s, want %s", i, g, tt.wp) - } - } -} diff --git a/vendor/github.com/kelseyhightower/memkv/store.go b/vendor/github.com/kelseyhightower/memkv/store.go index 210b906ae..23fd83e4b 100644 --- a/vendor/github.com/kelseyhightower/memkv/store.go +++ b/vendor/github.com/kelseyhightower/memkv/store.go @@ -79,9 +79,17 @@ func (s Store) Get(key string) (KVPair, error) { // GetValue gets the value associated with key. If there are no values // associated with key, GetValue returns "", ErrNotExist. -func (s Store) GetValue(key string) (string, error) { +func (s Store) GetValue(key string, v ...string) (string, error) { + defaultValue := "" + if len(v) > 0 { + defaultValue = v[0] + } + kv, err := s.Get(key) if err != nil { + if defaultValue != "" { + return defaultValue, nil + } return "", err } return kv.Value, nil