Skip to content

Commit

Permalink
Merge pull request #139 from hairyhenderson/indent-func
Browse files Browse the repository at this point in the history
Adding new indent function
  • Loading branch information
hairyhenderson committed May 12, 2017
2 parents ebb865e + 617e2e9 commit 8d5f38e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Gomplate is an alternative that will let you process templates which also includ
- [`trim`](#trim)
- [`urlParse`](#urlparse)
- [`has`](#has)
- [`indent`](#indent)
- [`json`](#json)
- [`jsonArray`](#jsonarray)
- [`yaml`](#yaml)
Expand Down Expand Up @@ -459,6 +460,27 @@ $ gomplate -d vault:///secret/foo < input.tmpl
The secret is 'foo: bar'
```

#### `indent`

Indents a given string with the given indentation pattern. If the input string has multiple lines, each line will be indented.

##### Example

This function can be especially useful when adding YAML snippets into other YAML documents, where indentation is important:

_`input.tmpl`:_
```
foo:
{{ `{"bar": {"baz": 2}}` | json | toYAML | indent " " }}
```

```console
$ gomplate -f input.tmpl
foo:
bar:
baz: 2
```

#### `json`

Converts a JSON string into an object. Only works for JSON Objects (not Arrays or other valid JSON types). This can be used to access properties of JSON objects.
Expand Down
1 change: 1 addition & 0 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
"yaml": typeconv.YAML,
"yamlArray": typeconv.YAMLArray,
"slice": typeconv.Slice,
"indent": typeconv.indent,
"join": typeconv.Join,
"toJSON": typeconv.ToJSON,
"toJSONPretty": typeconv.toJSONPretty,
Expand Down
8 changes: 8 additions & 0 deletions test/integration/typeconv_funcs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ function teardown () {
\"hello\": \"world\"
}" ]]
}

@test "indent" {
gomplate -i '{{ indent " " "hello world" }}{{ "hello\nmultiline\nworld" | indent " " }}'
[ "$status" -eq 0 ]
[[ "${output}" == " hello world hello
multiline
world" ]]
}
15 changes: 15 additions & 0 deletions typeconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ func (t *TypeConv) Slice(args ...interface{}) []interface{} {
return args
}

// Indent - indent each line of the string with the given indent string
func (t *TypeConv) indent(indent, s string) string {
var res []byte
bol := true
for i := 0; i < len(s); i++ {
c := s[i]
if bol && c != '\n' {
res = append(res, indent...)
}
res = append(res, c)
bol = c == '\n'
}
return string(res)
}

// Join concatenates the elements of a to create a single string.
// The separator string sep is placed between elements in the resulting string.
//
Expand Down
13 changes: 13 additions & 0 deletions typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,16 @@ func TestHas(t *testing.T) {
assert.False(t, ty.Has(in, "bar"))
assert.True(t, ty.Has(in["baz"], "qux"))
}

func TestIndent(t *testing.T) {
ty := new(TypeConv)
actual := "hello\nworld\n!"
expected := " hello\n world\n !"
assert.Equal(t, expected, ty.indent(" ", actual))

assert.Equal(t, "\n", ty.indent(" ", "\n"))

assert.Equal(t, " foo\n", ty.indent(" ", "foo\n"))

assert.Equal(t, " foo", ty.indent(" ", "foo"))
}

0 comments on commit 8d5f38e

Please sign in to comment.