Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: remove special characters from url #97

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions controllers/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,25 @@ env {
require.EqualValues(t, expected, buf.String())
}

func TestTemplate_Func_RemoveSpecialChars(t *testing.T) {
var buf bytes.Buffer
tmpl, err := tmpl.New("specialChars").Parse(`
{{- removeSpecialChars .URL -}}
{{- removeSpecialChars .Text -}}
`)
require.NoError(t, err)
var textWithSpecialChars = "a\tb\rc\n"
err = tmpl.ExecuteTemplate(&buf, `specialChars`, struct {
Text string
URL *url.URL
}{
Text: textWithSpecialChars,
URL: &url.URL{},
})
require.NoError(t, err)
require.EqualValues(t, "abc", buf.String())
}

func conditionReconciling() *dbv1alpha1.AtlasSchema {
return &dbv1alpha1.AtlasSchema{
ObjectMeta: objmeta(),
Expand Down
12 changes: 12 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"io/fs"
"regexp"
"strings"
"testing/fstest"
"text/template"
Expand Down Expand Up @@ -58,6 +59,17 @@ var (
b.WriteRune(']')
return b.String()
},
"removeSpecialChars": func(s interface{}) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return error for invalid type

r := regexp.MustCompile("[\t\r\n]")
switch s := s.(type) {
case string:
return r.ReplaceAllString(s, "")
case fmt.Stringer:
return r.ReplaceAllString(s.String(), "")
default:
return ""
}
},
}).
ParseFS(tmpls, "templates/*.tmpl"),
)
Expand Down
2 changes: 1 addition & 1 deletion controllers/templates/atlas_migration.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data "remote_dir" "this" {
{{- end }}
env {
name = atlas.env
url = "{{ .URL }}"
url = "{{ removeSpecialChars .URL }}"
migration {
{{- if .Cloud.HasRemoteDir }}
dir = data.remote_dir.this.url
Expand Down
4 changes: 2 additions & 2 deletions controllers/templates/atlas_schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ lint {
{{- end }}
env {
name = atlas.env
src = "{{ .Source }}"
url = "{{ .URL }}"
src = "{{ removeSpecialChars .Source }}"
url = "{{ removeSpecialChars .URL }}"
dev = "{{ .DevURL }}"
schemas = {{ slides .Schemas }}
exclude = {{ slides .Exclude }}
Expand Down