Skip to content

Commit

Permalink
Merge pull request #5 from GreenmaskIO/hot_fixes_v0.1.3
Browse files Browse the repository at this point in the history
Json transformer and connection string fixes
  • Loading branch information
wwoytenko committed Feb 7, 2024
2 parents ec383ad + c235dd8 commit e69af71
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
16 changes: 15 additions & 1 deletion internal/db/postgres/pgdump/pgdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,21 @@ func (o *Options) GetPgDSN() (string, error) {
return o.DbName, nil
}

return fmt.Sprintf("host=%s port=%d user=%s dbname=%s", o.Host, o.Port, o.UserName, o.DbName), nil
var parts []string
if o.Host != "" {
parts = append(parts, fmt.Sprintf("host=%s", o.Host))
}
if o.Port != 5432 {
parts = append(parts, fmt.Sprintf("port=%d", o.Port))
}
if o.UserName != "" {
parts = append(parts, fmt.Sprintf("port=%d", o.Port))
}
if o.DbName != "" {
parts = append(parts, fmt.Sprintf("dbname=%d", o.Port))
}

return strings.Join(parts, " "), nil
}

func (o *Options) GetParams() []string {
Expand Down
19 changes: 16 additions & 3 deletions internal/db/postgres/pgrestore/pgrestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,24 @@ type Options struct {
}

func (o *Options) GetPgDSN() (string, error) {
//return "host=localhost port=5432 user=postgres dbname=postgres", nil
if strings.Contains(o.DbName, "=") {
if strings.HasPrefix(o.DbName, "postgresql://") || strings.Contains(o.DbName, "=") {
return o.DbName, nil
}
return fmt.Sprintf("host=%s port=%d user=%s dbname=%s", o.Host, o.Port, o.UserName, o.DbName), nil

var parts []string
if o.Host != "" {
parts = append(parts, fmt.Sprintf("host=%s", o.Host))
}
if o.Port != 5432 {
parts = append(parts, fmt.Sprintf("port=%d", o.Port))
}
if o.UserName != "" {
parts = append(parts, fmt.Sprintf("port=%d", o.Port))
}
if o.DbName != "" {
parts = append(parts, fmt.Sprintf("dbname=%d", o.Port))
}
return strings.Join(parts, " "), nil
}

func (o *Options) GetParams() []string {
Expand Down
12 changes: 6 additions & 6 deletions internal/db/postgres/transformers/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ var jsonSetOpt = &sjson.Options{
}

type Operation struct {
Operation string `mapstructure:"operation"`
Value interface{} `mapstructure:"value,omitempty"`
ValueTemplate string `mapstructure:"value_template,omitempty"`
Path string `mapstructure:"path"`
ErrorNotExist bool `mapstructure:"error_not_exist"`
tmpl *template.Template
Operation string `mapstructure:"operation" json:"operation"`
Value interface{} `mapstructure:"value,omitempty" json:"value"`
ValueTemplate string `mapstructure:"value_template,omitempty" json:"value_template"`
Path string `mapstructure:"path" json:"path"`
ErrorNotExist bool `mapstructure:"error_not_exist" json:"error_not_exist"`
tmpl *template.Template `json:"tmpl,omitempty"`
}

func (o *Operation) Apply(inp []byte, tctx *JsonContext, buf *bytes.Buffer) ([]byte, error) {
Expand Down
35 changes: 35 additions & 0 deletions internal/db/postgres/transformers/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,38 @@ func TestJsonTransformer_Transform_null(t *testing.T) {
resValue := string(res.Data)
require.JSONEq(t, expected, resValue)
}

func TestJsonTransformer_structure_tags_encoding_regression(t *testing.T) {
// The unexpected behavior described in issue https://github.com/GreenmaskIO/greenmask/issues/4
// The problem was that the Operation object did not have an appropriate json tag on the fields
rawData := []byte(`
[
{
"operation": "set",
"path": "name",
"value_template": "template_tes",
"value": "value_test",
"error_not_exist": true
}
]
`)

expected := &Operation{
Operation: "set",
Path: "name",
ValueTemplate: "template_tes",
Value: "value_test",
ErrorNotExist: true,
}

var ops []*Operation
err := json.Unmarshal(rawData, &ops)
require.NoError(t, err)
require.Len(t, ops, 1)
op := ops[0]
assert.Equal(t, expected.Operation, op.Operation)
assert.Equal(t, expected.Path, op.Path)
assert.Equal(t, expected.ValueTemplate, op.ValueTemplate)
assert.Equal(t, expected.Value, op.Value)
assert.Equal(t, expected.ErrorNotExist, op.ErrorNotExist)
}

0 comments on commit e69af71

Please sign in to comment.