-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1290 from DataDog/mdgreenfield/default-delimiters
Allow overriding the default delimiter
- Loading branch information
Showing
6 changed files
with
248 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package config | ||
|
||
// DefaultDelims is used to configure the default delimiters used for all templates | ||
type DefaultDelims struct { | ||
// Left is the left delimiter for templating | ||
Left *string `mapstructure:"left"` | ||
|
||
// Right is the right delimiter for templating | ||
Right *string `mapstructure:"right"` | ||
} | ||
|
||
// DefaultDefaultDelims returns the default DefaultDelims | ||
func DefaultDefaultDelims() *DefaultDelims { | ||
return &DefaultDelims{} | ||
} | ||
|
||
// Copy returns a copy of the DefaultDelims | ||
func (c *DefaultDelims) Copy() *DefaultDelims { | ||
if c == nil { | ||
return nil | ||
} | ||
|
||
return &DefaultDelims{ | ||
Left: c.Left, | ||
Right: c.Right, | ||
} | ||
} | ||
|
||
// Merge merges the DefaultDelims | ||
func (c *DefaultDelims) Merge(o *DefaultDelims) *DefaultDelims { | ||
if c == nil { | ||
if o == nil { | ||
return nil | ||
} | ||
return o.Copy() | ||
} | ||
|
||
if o == nil { | ||
return c.Copy() | ||
} | ||
|
||
r := c.Copy() | ||
|
||
if o.Left != nil { | ||
r.Left = o.Left | ||
} | ||
|
||
if o.Right != nil { | ||
r.Right = o.Right | ||
} | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestDefaultDelims_Copy(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
a *DefaultDelims | ||
}{ | ||
{ | ||
"nil", | ||
nil, | ||
}, | ||
{ | ||
"empty", | ||
&DefaultDelims{}, | ||
}, | ||
{ | ||
"copy", | ||
&DefaultDelims{ | ||
Left: String("<<"), | ||
Right: String(">>"), | ||
}, | ||
}, | ||
} | ||
|
||
for i, tc := range cases { | ||
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { | ||
r := tc.a.Copy() | ||
if !reflect.DeepEqual(tc.a, r) { | ||
t.Errorf("\nexp: %#v\nact: %#v", tc.a, r) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDefaultDelims_Merge(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
a *DefaultDelims | ||
b *DefaultDelims | ||
r *DefaultDelims | ||
}{ | ||
{ | ||
"nil_a", | ||
nil, | ||
&DefaultDelims{}, | ||
&DefaultDelims{}, | ||
}, | ||
{ | ||
"nil_b", | ||
&DefaultDelims{}, | ||
nil, | ||
&DefaultDelims{}, | ||
}, | ||
{ | ||
"nil_both", | ||
nil, | ||
nil, | ||
nil, | ||
}, | ||
{ | ||
"empty", | ||
&DefaultDelims{}, | ||
&DefaultDelims{}, | ||
&DefaultDelims{}, | ||
}, | ||
{ | ||
"left_delim_l", | ||
&DefaultDelims{Left: String("<<")}, | ||
&DefaultDelims{}, | ||
&DefaultDelims{Left: String("<<")}, | ||
}, | ||
{ | ||
"left_delim_r", | ||
&DefaultDelims{}, | ||
&DefaultDelims{Left: String("<<")}, | ||
&DefaultDelims{Left: String("<<")}, | ||
}, | ||
{ | ||
"left_delim_r2", | ||
&DefaultDelims{Left: String(">>")}, | ||
&DefaultDelims{Left: String("<<")}, | ||
&DefaultDelims{Left: String("<<")}, | ||
}, | ||
{ | ||
"right_delim_l", | ||
&DefaultDelims{Right: String(">>")}, | ||
&DefaultDelims{}, | ||
&DefaultDelims{Right: String(">>")}, | ||
}, | ||
{ | ||
"right_delim_r", | ||
&DefaultDelims{}, | ||
&DefaultDelims{Right: String(">>")}, | ||
&DefaultDelims{Right: String(">>")}, | ||
}, | ||
{ | ||
"right_delim_r2", | ||
&DefaultDelims{Right: String("<<")}, | ||
&DefaultDelims{Right: String(">>")}, | ||
&DefaultDelims{Right: String(">>")}, | ||
}, | ||
} | ||
|
||
for i, tc := range cases { | ||
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { | ||
r := tc.a.Merge(tc.b) | ||
if !reflect.DeepEqual(tc.r, r) { | ||
t.Errorf("\nexp: %#v\nact: %#v", tc.r, r) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters