Skip to content

Commit

Permalink
api: remove special characters from url (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored Oct 2, 2023
1 parent 75e3488 commit 9d8092d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
26 changes: 26 additions & 0 deletions controllers/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,32 @@ 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 .Text -}}
{{- removeSpecialChars .URL -}}
`)
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())
// invalid data type
err = tmpl.ExecuteTemplate(&buf, `specialChars`, struct {
Text int
}{
Text: 0,
})
require.ErrorContains(t, err, "unsupported type int")
}

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, error) {
r := regexp.MustCompile("[\t\r\n]")
switch s := s.(type) {
case string:
return r.ReplaceAllString(s, ""), nil
case fmt.Stringer:
return r.ReplaceAllString(s.String(), ""), nil
default:
return "", fmt.Errorf("unsupported type %T", s)
}
},
}).
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
6 changes: 3 additions & 3 deletions controllers/templates/atlas_schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ lint {
{{- end }}
env {
name = atlas.env
src = "{{ .Source }}"
url = "{{ .URL }}"
dev = "{{ .DevURL }}"
src = "{{ removeSpecialChars .Source }}"
url = "{{ removeSpecialChars .URL }}"
dev = "{{ removeSpecialChars .DevURL }}"
schemas = {{ slides .Schemas }}
exclude = {{ slides .Exclude }}
}

0 comments on commit 9d8092d

Please sign in to comment.