Skip to content

Commit

Permalink
Partially handle case of not found tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Oct 27, 2023
1 parent bd13e11 commit 3da9dd4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
11 changes: 8 additions & 3 deletions cmd/revad/pkg/config/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func applyTemplateString(l Lookuper, p setter, v reflect.Value) error {
return err
}
if val == nil {
val = ""
// key was not found, just keep it but stop further parsing
break
}

new, err := replaceTemplate(s, tmpl, val)
Expand Down Expand Up @@ -175,7 +176,8 @@ func applyTemplateInterface(l Lookuper, p setter, v reflect.Value) error {
return err
}
if val == nil {
val = ""
// key was not found, just keep it but stop further parsing
break
}

new, err := replaceTemplate(s, tmpl, val)
Expand Down Expand Up @@ -214,7 +216,10 @@ func replaceTemplate(original, tmpl string, val any) (any, error) {
if !ok {
return nil, fmt.Errorf("value %v cannot be converted as string in the template %s", val, original)
}
return strings.Replace(original, tmpl, s, 1), nil
if s != "" {
return strings.Replace(original, tmpl, s, 1), nil
}
return original, nil
}

func convertToString(val any) (string, bool) {
Expand Down
18 changes: 10 additions & 8 deletions cmd/revad/pkg/config/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ func TestApplyTemplate(t *testing.T) {
Config: map[string]any{
"drivers": map[string]any{
"sql": map[string]any{
"db_username": "{{ vars.db_username }}",
"db_password": "{{ vars.db_password }}",
"key": "value",
"port": "{{ vars.port }}",
"db_username": "{{ vars.db_username }}",
"db_password": "{{ vars.db_password }}",
"key": "value",
"port": "{{ vars.port }}",
"user_and_token": "{{ vars.db_username }} and {{.Token}}",
},
},
},
Expand All @@ -125,10 +126,11 @@ func TestApplyTemplate(t *testing.T) {
assert.ErrorIs(t, err, nil)
assert.Equal(t, "localhost:1901", cfg2.Shared.GatewaySVC)
assert.Equal(t, map[string]any{
"db_username": "root",
"db_password": "secretpassword",
"key": "value",
"port": 1000,
"db_username": "root",
"db_password": "secretpassword",
"key": "value",
"port": 1000,
"user_and_token": "root and {{.Token}}",
}, cfg2.GRPC.Services["authregistry"][0].Config["drivers"].(map[string]any)["sql"])
assert.Equal(t, map[string]any{
"db_host": "http://localhost:1000",
Expand Down

0 comments on commit 3da9dd4

Please sign in to comment.