Skip to content

Commit

Permalink
Generic string interpolation (#888)
Browse files Browse the repository at this point in the history
This implements a general string interpolation package that we intend to
use in the configuration system to allow arbitray interpolation in YAML
values based on environment variables.

This supports variables in the form `${foo}` and `${foo:default}`
anywhere in a string.
  • Loading branch information
abhinav authored Apr 11, 2017
1 parent 9a896c1 commit 9f8be5d
Show file tree
Hide file tree
Showing 10 changed files with 913 additions and 1 deletion.
18 changes: 17 additions & 1 deletion build/deps.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ EXTRA_GO_BIN_DEPS = \
GLIDE_VERSION := 0.12.3
THRIFT_VERSION := 1.0.0-dev
PROTOC_VERSION := 3.2.0
RAGEL_VERSION := 6.9

GLIDE_OS := $(UNAME_OS)
THRIFT_OS := $(UNAME_OS)
Expand Down Expand Up @@ -51,9 +52,24 @@ THRIFT = $(BIN)/thrift
PROTOC_LIB = $(LIB)/protoc-$(PROTOC_VERSION)
PROTOC_ZIP = $(PROTOC_LIB)/protoc.zip
PROTOC = $(BIN)/protoc
GEN_BINS = $(THRIFT) $(PROTOC)
RAGEL_LIB = $(LIB)/ragel-$(RAGEL_VERSION)
RAGEL_TAR = $(RAGEL_LIB)/ragel.tar.gz
RAGEL = $(BIN)/ragel

GEN_BINS = $(THRIFT) $(PROTOC) $(RAGEL)
EXTRA_BINS = $(GLIDE)

$(RAGEL_TAR):
@mkdir -p $(RAGEL_LIB)
curl -L "https://www.colm.net/files/ragel/ragel-$(RAGEL_VERSION).tar.gz" > $(RAGEL_TAR)

$(RAGEL): $(RAGEL_TAR)
@mkdir -p $(BIN)
@cd $(RAGEL_LIB); tar xzf $(RAGEL_TAR)
@cd $(RAGEL_LIB)/ragel-$(RAGEL_VERSION); ./configure --prefix=$(RAGEL_LIB) --disable-manual
@cd $(RAGEL_LIB)/ragel-$(RAGEL_VERSION); make install
@cp $(RAGEL_LIB)/bin/ragel $(RAGEL)

$(GLIDE_TAR):
@mkdir -p $(GLIDE_LIB)
curl -L "https://github.com/Masterminds/glide/releases/download/v$(GLIDE_VERSION)/glide-v$(GLIDE_VERSION)-$(GLIDE_OS)-$(GLIDE_ARCH).tar.gz" > $(GLIDE_TAR)
Expand Down
29 changes: 29 additions & 0 deletions internal/interpolate/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package interpolate provides a generic mechanism to interpolate variables
// into strings.
//
// Variables are specified in the form "${foo}". Values for the different
// variables are supplied by a VariableResolver function provided at
// render-time. Variable names can also be in the form "${foo:bar}" where
// everything after the ":" is the default value for that variable if the
// VariableResolver did not have a value for that variable.
package interpolate
58 changes: 58 additions & 0 deletions internal/interpolate/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package interpolate

import "fmt"

var (
resolver = mapResolver(map[string]string{"name": "Inigo Montoya"})
emptyResolver = mapResolver(map[string]string{})
)

func Example() {
s, err := Parse("My name is ${name}")
if err != nil {
panic(err)
}

out, err := s.Render(resolver)
if err != nil {
panic(err)
}

fmt.Println(out)
// Output: My name is Inigo Montoya
}

func Example_default() {
s, err := Parse("My name is ${name:what}")
if err != nil {
panic(err)
}

out, err := s.Render(emptyResolver)
if err != nil {
panic(err)
}

fmt.Println(out)
// Output: My name is what
}
Loading

0 comments on commit 9f8be5d

Please sign in to comment.